Revert ABI-breaking enum bt_ctf_string_encoding change
[babeltrace.git] / formats / ctf / ir / field-types.c
1 /*
2 * field-types.c
3 *
4 * Babeltrace CTF IR - Event Types
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/field-path-internal.h>
31 #include <babeltrace/ctf-ir/fields-internal.h>
32 #include <babeltrace/ctf-ir/utils.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/ctf-ir/clock.h>
35 #include <babeltrace/ctf-ir/clock-internal.h>
36 #include <babeltrace/ctf-writer/writer-internal.h>
37 #include <babeltrace/object-internal.h>
38 #include <babeltrace/ref.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/endian.h>
41 #include <float.h>
42 #include <inttypes.h>
43 #include <stdlib.h>
44
45 struct range_overlap_query {
46 union {
47 uint64_t _unsigned;
48 int64_t _signed;
49 } range_start;
50
51 union {
52 uint64_t _unsigned;
53 int64_t _signed;
54 } range_end;
55 int overlaps;
56 GQuark mapping_name;
57 };
58
59 static
60 void bt_ctf_field_type_destroy(struct bt_object *);
61 static
62 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *);
63 static
64 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *);
65 static
66 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *);
67 static
68 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *);
69 static
70 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *);
71 static
72 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *);
73 static
74 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *);
75 static
76 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *);
77
78 static
79 void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = {
80 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy,
81 [CTF_TYPE_ENUM] =
82 bt_ctf_field_type_enumeration_destroy,
83 [CTF_TYPE_FLOAT] =
84 bt_ctf_field_type_floating_point_destroy,
85 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy,
86 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy,
87 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy,
88 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy,
89 [CTF_TYPE_STRING] = bt_ctf_field_type_string_destroy,
90 };
91
92 static
93 void generic_field_type_freeze(struct bt_ctf_field_type *);
94 static
95 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *);
96 static
97 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *);
98 static
99 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *);
100 static
101 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *);
102 static
103 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *);
104 static
105 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *);
106
107 static
108 type_freeze_func const type_freeze_funcs[] = {
109 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_freeze,
110 [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze,
111 [CTF_TYPE_FLOAT] = generic_field_type_freeze,
112 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze,
113 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze,
114 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze,
115 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze,
116 [CTF_TYPE_STRING] = generic_field_type_freeze,
117 };
118
119 static
120 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *,
121 struct metadata_context *);
122 static
123 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *,
124 struct metadata_context *);
125 static
126 int bt_ctf_field_type_floating_point_serialize(
127 struct bt_ctf_field_type *, struct metadata_context *);
128 static
129 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *,
130 struct metadata_context *);
131 static
132 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *,
133 struct metadata_context *);
134 static
135 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *,
136 struct metadata_context *);
137 static
138 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *,
139 struct metadata_context *);
140 static
141 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *,
142 struct metadata_context *);
143
144 static
145 type_serialize_func const type_serialize_funcs[] = {
146 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize,
147 [CTF_TYPE_ENUM] =
148 bt_ctf_field_type_enumeration_serialize,
149 [CTF_TYPE_FLOAT] =
150 bt_ctf_field_type_floating_point_serialize,
151 [CTF_TYPE_STRUCT] =
152 bt_ctf_field_type_structure_serialize,
153 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize,
154 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize,
155 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize,
156 [CTF_TYPE_STRING] = bt_ctf_field_type_string_serialize,
157 };
158
159 static
160 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *,
161 int byte_order, int set_native);
162 static
163 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *,
164 int byte_order, int set_native);
165 static
166 void bt_ctf_field_type_floating_point_set_byte_order(
167 struct bt_ctf_field_type *, int byte_order, int set_native);
168 static
169 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *,
170 int byte_order, int set_native);
171 static
172 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *,
173 int byte_order, int set_native);
174 static
175 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *,
176 int byte_order, int set_native);
177 static
178 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *,
179 int byte_order, int set_native);
180
181 /* The set_native flag only set the byte order if it is set to native */
182 static
183 void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *,
184 int byte_order, int set_native) = {
185 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_set_byte_order,
186 [CTF_TYPE_ENUM] =
187 bt_ctf_field_type_enumeration_set_byte_order,
188 [CTF_TYPE_FLOAT] =
189 bt_ctf_field_type_floating_point_set_byte_order,
190 [CTF_TYPE_STRUCT] =
191 bt_ctf_field_type_structure_set_byte_order,
192 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_set_byte_order,
193 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_set_byte_order,
194 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order,
195 [CTF_TYPE_STRING] = NULL,
196 };
197
198 static
199 struct bt_ctf_field_type *bt_ctf_field_type_integer_copy(
200 struct bt_ctf_field_type *);
201 static
202 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
203 struct bt_ctf_field_type *);
204 static
205 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy(
206 struct bt_ctf_field_type *);
207 static
208 struct bt_ctf_field_type *bt_ctf_field_type_structure_copy(
209 struct bt_ctf_field_type *);
210 static
211 struct bt_ctf_field_type *bt_ctf_field_type_variant_copy(
212 struct bt_ctf_field_type *);
213 static
214 struct bt_ctf_field_type *bt_ctf_field_type_array_copy(
215 struct bt_ctf_field_type *);
216 static
217 struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy(
218 struct bt_ctf_field_type *);
219 static
220 struct bt_ctf_field_type *bt_ctf_field_type_string_copy(
221 struct bt_ctf_field_type *);
222
223 static
224 struct bt_ctf_field_type *(* const type_copy_funcs[])(
225 struct bt_ctf_field_type *) = {
226 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_copy,
227 [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_copy,
228 [CTF_TYPE_FLOAT] = bt_ctf_field_type_floating_point_copy,
229 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_copy,
230 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_copy,
231 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_copy,
232 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_copy,
233 [CTF_TYPE_STRING] = bt_ctf_field_type_string_copy,
234 };
235
236 static
237 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *,
238 struct bt_ctf_field_type *);
239 static
240 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *,
241 struct bt_ctf_field_type *);
242 static
243 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *,
244 struct bt_ctf_field_type *);
245 static
246 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *,
247 struct bt_ctf_field_type *);
248 static
249 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *,
250 struct bt_ctf_field_type *);
251 static
252 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *,
253 struct bt_ctf_field_type *);
254 static
255 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *,
256 struct bt_ctf_field_type *);
257 static
258 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *,
259 struct bt_ctf_field_type *);
260
261 static
262 int (* const type_compare_funcs[])(struct bt_ctf_field_type *,
263 struct bt_ctf_field_type *) = {
264 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_compare,
265 [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_compare,
266 [CTF_TYPE_FLOAT] = bt_ctf_field_type_floating_point_compare,
267 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_compare,
268 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_compare,
269 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_compare,
270 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_compare,
271 [CTF_TYPE_STRING] = bt_ctf_field_type_string_compare,
272 };
273
274 static
275 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *);
276 static
277 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *);
278 static
279 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *);
280 static
281 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *);
282 static
283 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *);
284 static
285 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *);
286
287 static
288 int (* const type_validate_funcs[])(struct bt_ctf_field_type *) = {
289 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_validate,
290 [CTF_TYPE_FLOAT] = NULL,
291 [CTF_TYPE_STRING] = NULL,
292 [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_validate,
293 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_validate,
294 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_validate,
295 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_validate,
296 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_validate,
297 };
298
299 static
300 void destroy_enumeration_mapping(struct enumeration_mapping *mapping)
301 {
302 g_free(mapping);
303 }
304
305 static
306 void destroy_structure_field(struct structure_field *field)
307 {
308 bt_put(field->type);
309 g_free(field);
310 }
311
312 static
313 void check_ranges_overlap(gpointer element, gpointer query)
314 {
315 struct enumeration_mapping *mapping = element;
316 struct range_overlap_query *overlap_query = query;
317
318 if (mapping->range_start._signed <= overlap_query->range_end._signed
319 && overlap_query->range_start._signed <=
320 mapping->range_end._signed) {
321 overlap_query->overlaps = 1;
322 overlap_query->mapping_name = mapping->string;
323 }
324
325 overlap_query->overlaps |=
326 mapping->string == overlap_query->mapping_name;
327 }
328
329 static
330 void check_ranges_overlap_unsigned(gpointer element, gpointer query)
331 {
332 struct enumeration_mapping *mapping = element;
333 struct range_overlap_query *overlap_query = query;
334
335 if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned
336 && overlap_query->range_start._unsigned <=
337 mapping->range_end._unsigned) {
338 overlap_query->overlaps = 1;
339 overlap_query->mapping_name = mapping->string;
340 }
341
342 overlap_query->overlaps |=
343 mapping->string == overlap_query->mapping_name;
344 }
345
346 static
347 gint compare_enumeration_mappings_signed(struct enumeration_mapping **a,
348 struct enumeration_mapping **b)
349 {
350 return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1;
351 }
352
353 static
354 gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a,
355 struct enumeration_mapping **b)
356 {
357 return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1;
358 }
359
360 static
361 void bt_ctf_field_type_init(struct bt_ctf_field_type *type, int init_bo)
362 {
363 enum ctf_type_id type_id = type->declaration->id;
364
365 assert(type && (type_id > CTF_TYPE_UNKNOWN) &&
366 (type_id < NR_CTF_TYPES));
367
368 bt_object_init(type, bt_ctf_field_type_destroy);
369 type->freeze = type_freeze_funcs[type_id];
370 type->serialize = type_serialize_funcs[type_id];
371
372 if (init_bo) {
373 int ret = bt_ctf_field_type_set_byte_order(type,
374 BT_CTF_BYTE_ORDER_NATIVE);
375 assert(!ret);
376 }
377
378 type->declaration->alignment = 1;
379 }
380
381 static
382 int add_structure_field(GPtrArray *fields,
383 GHashTable *field_name_to_index,
384 struct bt_ctf_field_type *field_type,
385 const char *field_name)
386 {
387 int ret = 0;
388 GQuark name_quark = g_quark_from_string(field_name);
389 struct structure_field *field;
390
391 /* Make sure structure does not contain a field of the same name */
392 if (g_hash_table_lookup_extended(field_name_to_index,
393 GUINT_TO_POINTER(name_quark), NULL, NULL)) {
394 ret = -1;
395 goto end;
396 }
397
398 field = g_new0(struct structure_field, 1);
399 if (!field) {
400 ret = -1;
401 goto end;
402 }
403
404 bt_get(field_type);
405 field->name = name_quark;
406 field->type = field_type;
407 g_hash_table_insert(field_name_to_index,
408 (gpointer) (unsigned long) name_quark,
409 (gpointer) (unsigned long) fields->len);
410 g_ptr_array_add(fields, field);
411 end:
412 return ret;
413 }
414
415 static
416 void bt_ctf_field_type_destroy(struct bt_object *obj)
417 {
418 struct bt_ctf_field_type *type;
419 enum ctf_type_id type_id;
420
421 type = container_of(obj, struct bt_ctf_field_type, base);
422 type_id = type->declaration->id;
423 if (type_id <= CTF_TYPE_UNKNOWN ||
424 type_id >= NR_CTF_TYPES) {
425 return;
426 }
427
428 type_destroy_funcs[type_id](type);
429 }
430
431 static
432 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *type)
433 {
434 int ret = 0;
435
436 struct bt_ctf_field_type_integer *integer =
437 container_of(type, struct bt_ctf_field_type_integer,
438 parent);
439
440 if (integer->mapped_clock && integer->declaration.signedness) {
441 ret = -1;
442 goto end;
443 }
444
445 end:
446 return ret;
447 }
448
449 static
450 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type)
451 {
452 int ret = 0;
453
454 struct bt_ctf_field_type_enumeration *enumeration =
455 container_of(type, struct bt_ctf_field_type_enumeration,
456 parent);
457 struct bt_ctf_field_type *container_type =
458 bt_ctf_field_type_enumeration_get_container_type(type);
459
460 if (!container_type) {
461 ret = -1;
462 goto end;
463 }
464
465 ret = bt_ctf_field_type_validate(container_type);
466 if (ret) {
467 goto end;
468 }
469
470 /* Ensure enum has entries */
471 ret = enumeration->entries->len ? 0 : -1;
472
473 end:
474 BT_PUT(container_type);
475 return ret;
476 }
477
478 static
479 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *type)
480 {
481 int ret = 0;
482 struct bt_ctf_field_type *element_type = NULL;
483 struct bt_ctf_field_type_sequence *sequence =
484 container_of(type, struct bt_ctf_field_type_sequence,
485 parent);
486
487 /* Length field name should be set at this point */
488 if (sequence->length_field_name->len == 0) {
489 ret = -1;
490 goto end;
491 }
492
493 element_type = bt_ctf_field_type_sequence_get_element_type(type);
494 if (!element_type) {
495 ret = -1;
496 goto end;
497 }
498
499 ret = bt_ctf_field_type_validate(element_type);
500
501 end:
502 BT_PUT(element_type);
503
504 return ret;
505 }
506
507 static
508 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *type)
509 {
510 int ret = 0;
511 struct bt_ctf_field_type *element_type = NULL;
512
513 element_type = bt_ctf_field_type_array_get_element_type(type);
514 if (!element_type) {
515 ret = -1;
516 goto end;
517 }
518
519 ret = bt_ctf_field_type_validate(element_type);
520
521 end:
522 BT_PUT(element_type);
523
524 return ret;
525 }
526
527 static
528 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *type)
529 {
530 int ret = 0;
531 struct bt_ctf_field_type *child_type = NULL;
532 int field_count = bt_ctf_field_type_structure_get_field_count(type);
533 int i;
534
535 if (field_count < 0) {
536 ret = -1;
537 goto end;
538 }
539
540 for (i = 0; i < field_count; ++i) {
541 ret = bt_ctf_field_type_structure_get_field(type,
542 NULL, &child_type, i);
543 if (ret) {
544 goto end;
545 }
546
547 ret = bt_ctf_field_type_validate(child_type);
548 if (ret) {
549 goto end;
550 }
551
552 BT_PUT(child_type);
553 }
554
555 end:
556 BT_PUT(child_type);
557
558 return ret;
559 }
560
561 static
562 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
563 {
564 int ret = 0;
565 int field_count;
566 struct bt_ctf_field_type *child_type = NULL;
567 struct bt_ctf_field_type_variant *variant =
568 container_of(type, struct bt_ctf_field_type_variant,
569 parent);
570 int i;
571 int tag_mappings_count;
572
573 if (variant->tag_name->len == 0 || !variant->tag) {
574 ret = -1;
575 goto end;
576 }
577
578 tag_mappings_count =
579 bt_ctf_field_type_enumeration_get_mapping_count(
580 (struct bt_ctf_field_type *) variant->tag);
581
582 if (tag_mappings_count != variant->fields->len) {
583 ret = -1;
584 goto end;
585 }
586
587 for (i = 0; i < tag_mappings_count; ++i) {
588 const char *label;
589 int64_t range_start, range_end;
590 struct bt_ctf_field_type *ft;
591
592 ret = bt_ctf_field_type_enumeration_get_mapping(
593 (struct bt_ctf_field_type *) variant->tag,
594 i, &label, &range_start, &range_end);
595 if (ret) {
596 goto end;
597 }
598 if (!label) {
599 ret = -1;
600 goto end;
601 }
602
603 ft = bt_ctf_field_type_variant_get_field_type_by_name(
604 type, label);
605 if (!ft) {
606 ret = -1;
607 goto end;
608 }
609
610 BT_PUT(ft);
611 }
612
613 field_count = bt_ctf_field_type_variant_get_field_count(type);
614 if (field_count < 0) {
615 ret = -1;
616 goto end;
617 }
618
619 for (i = 0; i < field_count; ++i) {
620 ret = bt_ctf_field_type_variant_get_field(type,
621 NULL, &child_type, i);
622 if (ret) {
623 goto end;
624 }
625
626 ret = bt_ctf_field_type_validate(child_type);
627 if (ret) {
628 goto end;
629 }
630
631 BT_PUT(child_type);
632 }
633
634 end:
635 BT_PUT(child_type);
636
637 return ret;
638 }
639
640 /*
641 * This function validates a given field type without considering
642 * where this field type is located. It only validates the properties
643 * of the given field type and the properties of its children if
644 * applicable.
645 */
646 BT_HIDDEN
647 int bt_ctf_field_type_validate(struct bt_ctf_field_type *type)
648 {
649 int ret = 0;
650 enum ctf_type_id id = bt_ctf_field_type_get_type_id(type);
651
652 if (!type) {
653 ret = -1;
654 goto end;
655 }
656
657 if (type->valid) {
658 /* Already marked as valid */
659 goto end;
660 }
661
662 if (type_validate_funcs[id]) {
663 ret = type_validate_funcs[id](type);
664 }
665
666 if (!ret && type->frozen) {
667 /* Field type is valid */
668 type->valid = 1;
669 }
670
671 end:
672 return ret;
673 }
674
675 struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size)
676 {
677 struct bt_ctf_field_type_integer *integer =
678 g_new0(struct bt_ctf_field_type_integer, 1);
679
680 if (!integer || size == 0 || size > 64) {
681 return NULL;
682 }
683
684 integer->parent.declaration = &integer->declaration.p;
685 integer->parent.declaration->id = CTF_TYPE_INTEGER;
686 integer->declaration.len = size;
687 integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL;
688 integer->declaration.encoding =
689 (enum ctf_string_encoding) BT_CTF_STRING_ENCODING_NONE;
690 bt_ctf_field_type_init(&integer->parent, TRUE);
691 return &integer->parent;
692 }
693
694 BT_HIDDEN
695 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type)
696 {
697 int ret = 0;
698 struct bt_ctf_field_type_integer *integer;
699
700 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
701 ret = -1;
702 goto end;
703 }
704
705 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
706 ret = (int) integer->declaration.len;
707 end:
708 return ret;
709 }
710
711 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type)
712 {
713 int ret = 0;
714 struct bt_ctf_field_type_integer *integer;
715
716 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
717 ret = -1;
718 goto end;
719 }
720
721 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
722 ret = integer->declaration.signedness;
723 end:
724 return ret;
725 }
726
727 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type,
728 int is_signed)
729 {
730 int ret = 0;
731 struct bt_ctf_field_type_integer *integer;
732
733 if (!type || type->frozen ||
734 type->declaration->id != CTF_TYPE_INTEGER) {
735 ret = -1;
736 goto end;
737 }
738
739 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
740 integer->declaration.signedness = !!is_signed;
741 end:
742 return ret;
743 }
744
745 BT_HIDDEN
746 enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base(
747 struct bt_ctf_field_type *type)
748 {
749 enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN;
750 struct bt_ctf_field_type_integer *integer;
751
752 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
753 goto end;
754 }
755
756 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
757 ret = integer->declaration.base;
758 end:
759 return ret;
760 }
761
762 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type,
763 enum bt_ctf_integer_base base)
764 {
765 int ret = 0;
766
767 if (!type || type->frozen ||
768 type->declaration->id != CTF_TYPE_INTEGER) {
769 ret = -1;
770 goto end;
771 }
772
773 switch (base) {
774 case BT_CTF_INTEGER_BASE_BINARY:
775 case BT_CTF_INTEGER_BASE_OCTAL:
776 case BT_CTF_INTEGER_BASE_DECIMAL:
777 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
778 {
779 struct bt_ctf_field_type_integer *integer = container_of(type,
780 struct bt_ctf_field_type_integer, parent);
781 integer->declaration.base = base;
782 break;
783 }
784 default:
785 ret = -1;
786 }
787 end:
788 return ret;
789 }
790
791 BT_HIDDEN
792 enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding(
793 struct bt_ctf_field_type *type)
794 {
795 enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN;
796 struct bt_ctf_field_type_integer *integer;
797
798 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
799 goto end;
800 }
801
802 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
803 ret = (enum bt_ctf_string_encoding) integer->declaration.encoding;
804 end:
805 return ret;
806 }
807
808 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type,
809 enum ctf_string_encoding encoding)
810 {
811 int ret = 0;
812 struct bt_ctf_field_type_integer *integer;
813
814 if (!type || type->frozen ||
815 (type->declaration->id != CTF_TYPE_INTEGER) ||
816 (encoding < CTF_STRING_NONE) ||
817 (encoding >= CTF_STRING_UNKNOWN)) {
818 ret = -1;
819 goto end;
820 }
821
822 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
823 integer->declaration.encoding = encoding;
824 end:
825 return ret;
826 }
827
828 BT_HIDDEN
829 struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock(
830 struct bt_ctf_field_type *type)
831 {
832 struct bt_ctf_field_type_integer *integer;
833 struct bt_ctf_clock *clock = NULL;
834
835 if (!type) {
836 goto end;
837 }
838
839 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
840 clock = integer->mapped_clock;
841 bt_get(clock);
842 end:
843 return clock;
844 }
845
846 BT_HIDDEN
847 int bt_ctf_field_type_integer_set_mapped_clock(
848 struct bt_ctf_field_type *type,
849 struct bt_ctf_clock *clock)
850 {
851 struct bt_ctf_field_type_integer *integer;
852 int ret = 0;
853
854 if (!type || type->frozen) {
855 ret = -1;
856 goto end;
857 }
858
859 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
860 bt_put(integer->mapped_clock);
861 bt_get(clock);
862 integer->mapped_clock = clock;
863 end:
864 return ret;
865 }
866
867 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(
868 struct bt_ctf_field_type *integer_container_type)
869 {
870 struct bt_ctf_field_type_enumeration *enumeration = NULL;
871
872 if (!integer_container_type) {
873 goto error;
874 }
875
876 if (integer_container_type->declaration->id != CTF_TYPE_INTEGER) {
877 goto error;
878 }
879
880 enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1);
881 if (!enumeration) {
882 goto error;
883 }
884
885 enumeration->parent.declaration = &enumeration->declaration.p;
886 enumeration->parent.declaration->id = CTF_TYPE_ENUM;
887 bt_get(integer_container_type);
888 enumeration->container = integer_container_type;
889 enumeration->entries = g_ptr_array_new_with_free_func(
890 (GDestroyNotify)destroy_enumeration_mapping);
891 bt_ctf_field_type_init(&enumeration->parent, FALSE);
892 return &enumeration->parent;
893 error:
894 g_free(enumeration);
895 return NULL;
896 }
897
898 BT_HIDDEN
899 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type(
900 struct bt_ctf_field_type *type)
901 {
902 struct bt_ctf_field_type *container_type = NULL;
903 struct bt_ctf_field_type_enumeration *enumeration_type;
904
905 if (!type) {
906 goto end;
907 }
908
909 if (type->declaration->id != CTF_TYPE_ENUM) {
910 goto end;
911 }
912
913 enumeration_type = container_of(type,
914 struct bt_ctf_field_type_enumeration, parent);
915 container_type = enumeration_type->container;
916 bt_get(container_type);
917 end:
918 return container_type;
919 }
920
921 int bt_ctf_field_type_enumeration_add_mapping(
922 struct bt_ctf_field_type *type, const char *string,
923 int64_t range_start, int64_t range_end)
924 {
925 int ret = 0;
926 GQuark mapping_name;
927 struct enumeration_mapping *mapping;
928 struct bt_ctf_field_type_enumeration *enumeration;
929 struct range_overlap_query query;
930 char *escaped_string;
931
932 if (!type || (type->declaration->id != CTF_TYPE_ENUM) ||
933 type->frozen ||
934 (range_end < range_start)) {
935 ret = -1;
936 goto end;
937 }
938
939 if (!string || strlen(string) == 0) {
940 ret = -1;
941 goto end;
942 }
943
944 escaped_string = g_strescape(string, NULL);
945 if (!escaped_string) {
946 ret = -1;
947 goto end;
948 }
949
950 mapping_name = g_quark_from_string(escaped_string);
951 query = (struct range_overlap_query) {
952 .range_start._signed = range_start,
953 .range_end._signed = range_end,
954 .mapping_name = mapping_name,
955 .overlaps = 0 };
956 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
957 parent);
958
959 /* Check that the range does not overlap with one already present */
960 g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query);
961 if (query.overlaps) {
962 ret = -1;
963 goto error_free;
964 }
965
966 mapping = g_new(struct enumeration_mapping, 1);
967 if (!mapping) {
968 ret = -1;
969 goto error_free;
970 }
971
972 *mapping = (struct enumeration_mapping) {
973 .range_start._signed = range_start,
974 .range_end._signed = range_end, .string = mapping_name};
975 g_ptr_array_add(enumeration->entries, mapping);
976 g_ptr_array_sort(enumeration->entries,
977 (GCompareFunc)compare_enumeration_mappings_signed);
978 error_free:
979 free(escaped_string);
980 end:
981 return ret;
982 }
983
984 BT_HIDDEN
985 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
986 struct bt_ctf_field_type *type, const char *string,
987 uint64_t range_start, uint64_t range_end)
988 {
989 int ret = 0;
990 GQuark mapping_name;
991 struct enumeration_mapping *mapping;
992 struct bt_ctf_field_type_enumeration *enumeration;
993 struct range_overlap_query query;
994 char *escaped_string;
995
996 if (!type || (type->declaration->id != CTF_TYPE_ENUM) ||
997 type->frozen ||
998 (range_end < range_start)) {
999 ret = -1;
1000 goto end;
1001 }
1002
1003 if (!string || strlen(string) == 0) {
1004 ret = -1;
1005 goto end;
1006 }
1007
1008 escaped_string = g_strescape(string, NULL);
1009 if (!escaped_string) {
1010 ret = -1;
1011 goto end;
1012 }
1013
1014 mapping_name = g_quark_from_string(escaped_string);
1015 query = (struct range_overlap_query) {
1016 .range_start._unsigned = range_start,
1017 .range_end._unsigned = range_end,
1018 .mapping_name = mapping_name,
1019 .overlaps = 0 };
1020 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
1021 parent);
1022
1023 /* Check that the range does not overlap with one already present */
1024 g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned,
1025 &query);
1026 if (query.overlaps) {
1027 ret = -1;
1028 goto error_free;
1029 }
1030
1031 mapping = g_new(struct enumeration_mapping, 1);
1032 if (!mapping) {
1033 ret = -1;
1034 goto error_free;
1035 }
1036
1037 *mapping = (struct enumeration_mapping) {
1038 .range_start._unsigned = range_start,
1039 .range_end._unsigned = range_end, .string = mapping_name};
1040 g_ptr_array_add(enumeration->entries, mapping);
1041 g_ptr_array_sort(enumeration->entries,
1042 (GCompareFunc)compare_enumeration_mappings_unsigned);
1043 error_free:
1044 free(escaped_string);
1045 end:
1046 return ret;
1047 }
1048
1049 BT_HIDDEN
1050 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1051 struct bt_ctf_field_type_enumeration *enumeration_type,
1052 uint64_t value)
1053 {
1054 const char *name = NULL;
1055 struct range_overlap_query query =
1056 (struct range_overlap_query) {
1057 .range_start._unsigned = value,
1058 .range_end._unsigned = value,
1059 .overlaps = 0 };
1060
1061 g_ptr_array_foreach(enumeration_type->entries,
1062 check_ranges_overlap_unsigned,
1063 &query);
1064 if (!query.overlaps) {
1065 goto end;
1066 }
1067
1068 name = g_quark_to_string(query.mapping_name);
1069 end:
1070 return name;
1071 }
1072
1073 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1074 struct bt_ctf_field_type_enumeration *enumeration_type,
1075 int64_t value)
1076 {
1077 const char *name = NULL;
1078 struct range_overlap_query query =
1079 (struct range_overlap_query) {
1080 .range_start._signed = value,
1081 .range_end._signed = value,
1082 .overlaps = 0 };
1083
1084 g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap,
1085 &query);
1086 if (!query.overlaps) {
1087 goto end;
1088 }
1089
1090 name = g_quark_to_string(query.mapping_name);
1091 end:
1092 return name;
1093 }
1094
1095 int bt_ctf_field_type_enumeration_get_mapping_count(
1096 struct bt_ctf_field_type *type)
1097 {
1098 int ret = 0;
1099 struct bt_ctf_field_type_enumeration *enumeration;
1100
1101 if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
1102 ret = -1;
1103 goto end;
1104 }
1105
1106 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
1107 parent);
1108 ret = (int) enumeration->entries->len;
1109 end:
1110 return ret;
1111 }
1112
1113 static inline
1114 struct enumeration_mapping *get_enumeration_mapping(
1115 struct bt_ctf_field_type *type, int index)
1116 {
1117 struct enumeration_mapping *mapping = NULL;
1118 struct bt_ctf_field_type_enumeration *enumeration;
1119
1120 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
1121 parent);
1122 if (index >= enumeration->entries->len) {
1123 goto end;
1124 }
1125
1126 mapping = g_ptr_array_index(enumeration->entries, index);
1127 end:
1128 return mapping;
1129 }
1130
1131 BT_HIDDEN
1132 int bt_ctf_field_type_enumeration_get_mapping(
1133 struct bt_ctf_field_type *type, int index,
1134 const char **string, int64_t *range_start, int64_t *range_end)
1135 {
1136 struct enumeration_mapping *mapping;
1137 int ret = 0;
1138
1139 if (!type || index < 0 || !string || !range_start || !range_end ||
1140 (type->declaration->id != CTF_TYPE_ENUM)) {
1141 ret = -1;
1142 goto end;
1143 }
1144
1145 mapping = get_enumeration_mapping(type, index);
1146 if (!mapping) {
1147 ret = -1;
1148 goto end;
1149 }
1150
1151 *string = g_quark_to_string(mapping->string);
1152 *range_start = mapping->range_start._signed;
1153 *range_end = mapping->range_end._signed;
1154 end:
1155 return ret;
1156 }
1157
1158 BT_HIDDEN
1159 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1160 struct bt_ctf_field_type *type, int index,
1161 const char **string, uint64_t *range_start, uint64_t *range_end)
1162 {
1163 struct enumeration_mapping *mapping;
1164 int ret = 0;
1165
1166 if (!type || index < 0 || !string || !range_start || !range_end ||
1167 (type->declaration->id != CTF_TYPE_ENUM)) {
1168 ret = -1;
1169 goto end;
1170 }
1171
1172 mapping = get_enumeration_mapping(type, index);
1173 if (!mapping) {
1174 ret = -1;
1175 goto end;
1176 }
1177
1178 *string = g_quark_to_string(mapping->string);
1179 *range_start = mapping->range_start._unsigned;
1180 *range_end = mapping->range_end._unsigned;
1181 end:
1182 return ret;
1183 }
1184
1185 BT_HIDDEN
1186 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1187 struct bt_ctf_field_type *type, const char *name)
1188 {
1189 GQuark name_quark;
1190 struct bt_ctf_field_type_enumeration *enumeration;
1191 int i, ret = 0;
1192
1193 if (!type || !name ||
1194 (type->declaration->id != CTF_TYPE_ENUM)) {
1195 ret = -1;
1196 goto end;
1197 }
1198
1199 name_quark = g_quark_try_string(name);
1200 if (!name_quark) {
1201 ret = -1;
1202 goto end;
1203 }
1204
1205 enumeration = container_of(type,
1206 struct bt_ctf_field_type_enumeration, parent);
1207 for (i = 0; i < enumeration->entries->len; i++) {
1208 struct enumeration_mapping *mapping =
1209 get_enumeration_mapping(type, i);
1210
1211 if (mapping->string == name_quark) {
1212 ret = i;
1213 goto end;
1214 }
1215 }
1216
1217 ret = -1;
1218 end:
1219 return ret;
1220 }
1221
1222 BT_HIDDEN
1223 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1224 struct bt_ctf_field_type *type, int64_t value)
1225 {
1226 struct bt_ctf_field_type_enumeration *enumeration;
1227 int i, ret = 0;
1228
1229 if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
1230 ret = -1;
1231 goto end;
1232 }
1233
1234 enumeration = container_of(type,
1235 struct bt_ctf_field_type_enumeration, parent);
1236 for (i = 0; i < enumeration->entries->len; i++) {
1237 struct enumeration_mapping *mapping =
1238 get_enumeration_mapping(type, i);
1239
1240 if (value >= mapping->range_start._signed &&
1241 value <= mapping->range_end._signed) {
1242 ret = i;
1243 goto end;
1244 }
1245 }
1246
1247 ret = -1;
1248 end:
1249 return ret;
1250 }
1251
1252 BT_HIDDEN
1253 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1254 struct bt_ctf_field_type *type, uint64_t value)
1255 {
1256 struct bt_ctf_field_type_enumeration *enumeration;
1257 int i, ret = 0;
1258
1259 if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
1260 ret = -1;
1261 goto end;
1262 }
1263
1264 enumeration = container_of(type,
1265 struct bt_ctf_field_type_enumeration, parent);
1266 for (i = 0; i < enumeration->entries->len; i++) {
1267 struct enumeration_mapping *mapping =
1268 get_enumeration_mapping(type, i);
1269
1270 if (value >= mapping->range_start._unsigned &&
1271 value <= mapping->range_end._unsigned) {
1272 ret = i;
1273 goto end;
1274 }
1275 }
1276
1277 ret = -1;
1278 end:
1279 return ret;
1280 }
1281
1282 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void)
1283 {
1284 struct bt_ctf_field_type_floating_point *floating_point =
1285 g_new0(struct bt_ctf_field_type_floating_point, 1);
1286
1287 if (!floating_point) {
1288 goto end;
1289 }
1290
1291 floating_point->declaration.sign = &floating_point->sign;
1292 floating_point->declaration.mantissa = &floating_point->mantissa;
1293 floating_point->declaration.exp = &floating_point->exp;
1294 floating_point->sign.len = 1;
1295 floating_point->parent.declaration = &floating_point->declaration.p;
1296 floating_point->parent.declaration->id = CTF_TYPE_FLOAT;
1297 floating_point->declaration.exp->len =
1298 sizeof(float) * CHAR_BIT - FLT_MANT_DIG;
1299 floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1;
1300 floating_point->sign.p.alignment = 1;
1301 floating_point->mantissa.p.alignment = 1;
1302 floating_point->exp.p.alignment = 1;
1303
1304 bt_ctf_field_type_init(&floating_point->parent, TRUE);
1305 end:
1306 return floating_point ? &floating_point->parent : NULL;
1307 }
1308
1309 BT_HIDDEN
1310 int bt_ctf_field_type_floating_point_get_exponent_digits(
1311 struct bt_ctf_field_type *type)
1312 {
1313 int ret = 0;
1314 struct bt_ctf_field_type_floating_point *floating_point;
1315
1316 if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) {
1317 ret = -1;
1318 goto end;
1319 }
1320
1321 floating_point = container_of(type,
1322 struct bt_ctf_field_type_floating_point, parent);
1323 ret = (int) floating_point->declaration.exp->len;
1324 end:
1325 return ret;
1326 }
1327
1328 int bt_ctf_field_type_floating_point_set_exponent_digits(
1329 struct bt_ctf_field_type *type,
1330 unsigned int exponent_digits)
1331 {
1332 int ret = 0;
1333 struct bt_ctf_field_type_floating_point *floating_point;
1334
1335 if (!type || type->frozen ||
1336 (type->declaration->id != CTF_TYPE_FLOAT)) {
1337 ret = -1;
1338 goto end;
1339 }
1340
1341 floating_point = container_of(type,
1342 struct bt_ctf_field_type_floating_point, parent);
1343 if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) &&
1344 (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) &&
1345 (exponent_digits !=
1346 sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) {
1347 ret = -1;
1348 goto end;
1349 }
1350
1351 floating_point->declaration.exp->len = exponent_digits;
1352 end:
1353 return ret;
1354 }
1355
1356 BT_HIDDEN
1357 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1358 struct bt_ctf_field_type *type)
1359 {
1360 int ret = 0;
1361 struct bt_ctf_field_type_floating_point *floating_point;
1362
1363 if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) {
1364 ret = -1;
1365 goto end;
1366 }
1367
1368 floating_point = container_of(type,
1369 struct bt_ctf_field_type_floating_point, parent);
1370 ret = (int) floating_point->mantissa.len + 1;
1371 end:
1372 return ret;
1373 }
1374
1375 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1376 struct bt_ctf_field_type *type,
1377 unsigned int mantissa_digits)
1378 {
1379 int ret = 0;
1380 struct bt_ctf_field_type_floating_point *floating_point;
1381
1382 if (!type || type->frozen ||
1383 (type->declaration->id != CTF_TYPE_FLOAT)) {
1384 ret = -1;
1385 goto end;
1386 }
1387
1388 floating_point = container_of(type,
1389 struct bt_ctf_field_type_floating_point, parent);
1390
1391 if ((mantissa_digits != FLT_MANT_DIG) &&
1392 (mantissa_digits != DBL_MANT_DIG) &&
1393 (mantissa_digits != LDBL_MANT_DIG)) {
1394 ret = -1;
1395 goto end;
1396 }
1397
1398 floating_point->declaration.mantissa->len = mantissa_digits - 1;
1399 end:
1400 return ret;
1401 }
1402
1403 struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void)
1404 {
1405 struct bt_ctf_field_type_structure *structure =
1406 g_new0(struct bt_ctf_field_type_structure, 1);
1407
1408 if (!structure) {
1409 goto error;
1410 }
1411
1412 structure->parent.declaration = &structure->declaration.p;
1413 structure->parent.declaration->id = CTF_TYPE_STRUCT;
1414 structure->fields = g_ptr_array_new_with_free_func(
1415 (GDestroyNotify)destroy_structure_field);
1416 structure->field_name_to_index = g_hash_table_new(NULL, NULL);
1417 bt_ctf_field_type_init(&structure->parent, TRUE);
1418 return &structure->parent;
1419 error:
1420 return NULL;
1421 }
1422
1423 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type,
1424 struct bt_ctf_field_type *field_type,
1425 const char *field_name)
1426 {
1427 int ret = 0;
1428 struct bt_ctf_field_type_structure *structure;
1429
1430 if (!type || !field_type || type->frozen ||
1431 bt_ctf_validate_identifier(field_name) ||
1432 (type->declaration->id != CTF_TYPE_STRUCT)) {
1433 ret = -1;
1434 goto end;
1435 }
1436
1437 structure = container_of(type,
1438 struct bt_ctf_field_type_structure, parent);
1439 if (add_structure_field(structure->fields,
1440 structure->field_name_to_index, field_type, field_name)) {
1441 ret = -1;
1442 goto end;
1443 }
1444 end:
1445 return ret;
1446 }
1447
1448 BT_HIDDEN
1449 int bt_ctf_field_type_structure_get_field_count(
1450 struct bt_ctf_field_type *type)
1451 {
1452 int ret = 0;
1453 struct bt_ctf_field_type_structure *structure;
1454
1455 if (!type || (type->declaration->id != CTF_TYPE_STRUCT)) {
1456 ret = -1;
1457 goto end;
1458 }
1459
1460 structure = container_of(type, struct bt_ctf_field_type_structure,
1461 parent);
1462 ret = (int) structure->fields->len;
1463 end:
1464 return ret;
1465 }
1466
1467 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *type,
1468 const char **field_name, struct bt_ctf_field_type **field_type,
1469 int index)
1470 {
1471 struct bt_ctf_field_type_structure *structure;
1472 struct structure_field *field;
1473 int ret = 0;
1474
1475 if (!type || index < 0 ||
1476 (type->declaration->id != CTF_TYPE_STRUCT)) {
1477 ret = -1;
1478 goto end;
1479 }
1480
1481 structure = container_of(type, struct bt_ctf_field_type_structure,
1482 parent);
1483 if (index >= structure->fields->len) {
1484 ret = -1;
1485 goto end;
1486 }
1487
1488 field = g_ptr_array_index(structure->fields, index);
1489 if (field_type) {
1490 *field_type = field->type;
1491 bt_get(field->type);
1492 }
1493 if (field_name) {
1494 *field_name = g_quark_to_string(field->name);
1495 }
1496 end:
1497 return ret;
1498 }
1499
1500 BT_HIDDEN
1501 struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name(
1502 struct bt_ctf_field_type *type,
1503 const char *name)
1504 {
1505 size_t index;
1506 GQuark name_quark;
1507 struct structure_field *field;
1508 struct bt_ctf_field_type_structure *structure;
1509 struct bt_ctf_field_type *field_type = NULL;
1510
1511 if (!type || !name) {
1512 goto end;
1513 }
1514
1515 name_quark = g_quark_try_string(name);
1516 if (!name_quark) {
1517 goto end;
1518 }
1519
1520 structure = container_of(type, struct bt_ctf_field_type_structure,
1521 parent);
1522 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
1523 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
1524 goto end;
1525 }
1526
1527 field = structure->fields->pdata[index];
1528 field_type = field->type;
1529 bt_get(field_type);
1530 end:
1531 return field_type;
1532 }
1533
1534 struct bt_ctf_field_type *bt_ctf_field_type_variant_create(
1535 struct bt_ctf_field_type *enum_tag, const char *tag_name)
1536 {
1537 struct bt_ctf_field_type_variant *variant = NULL;
1538
1539 if (tag_name && bt_ctf_validate_identifier(tag_name)) {
1540 goto error;
1541 }
1542
1543 variant = g_new0(struct bt_ctf_field_type_variant, 1);
1544 if (!variant) {
1545 goto error;
1546 }
1547
1548 variant->parent.declaration = &variant->declaration.p;
1549 variant->parent.declaration->id = CTF_TYPE_VARIANT;
1550 variant->tag_name = g_string_new(tag_name);
1551 variant->field_name_to_index = g_hash_table_new(NULL, NULL);
1552 variant->fields = g_ptr_array_new_with_free_func(
1553 (GDestroyNotify) destroy_structure_field);
1554 if (enum_tag) {
1555 bt_get(enum_tag);
1556 variant->tag = container_of(enum_tag,
1557 struct bt_ctf_field_type_enumeration, parent);
1558 }
1559
1560 bt_ctf_field_type_init(&variant->parent, TRUE);
1561 /* A variant's alignment is undefined */
1562 variant->parent.declaration->alignment = 0;
1563 return &variant->parent;
1564 error:
1565 return NULL;
1566 }
1567
1568 BT_HIDDEN
1569 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type(
1570 struct bt_ctf_field_type *type)
1571 {
1572 struct bt_ctf_field_type_variant *variant;
1573 struct bt_ctf_field_type *tag_type = NULL;
1574
1575 if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) {
1576 goto end;
1577 }
1578
1579 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1580 if (!variant->tag) {
1581 goto end;
1582 }
1583
1584 tag_type = &variant->tag->parent;
1585 bt_get(tag_type);
1586 end:
1587 return tag_type;
1588 }
1589
1590 BT_HIDDEN
1591 const char *bt_ctf_field_type_variant_get_tag_name(
1592 struct bt_ctf_field_type *type)
1593 {
1594 struct bt_ctf_field_type_variant *variant;
1595 const char *tag_name = NULL;
1596
1597 if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) {
1598 goto end;
1599 }
1600
1601 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1602 if (variant->tag_name->len == 0) {
1603 goto end;
1604 }
1605
1606 tag_name = variant->tag_name->str;
1607 end:
1608 return tag_name;
1609 }
1610
1611 BT_HIDDEN
1612 int bt_ctf_field_type_variant_set_tag_name(
1613 struct bt_ctf_field_type *type, const char *name)
1614 {
1615 int ret = 0;
1616 struct bt_ctf_field_type_variant *variant;
1617
1618 if (!type || type->frozen ||
1619 (type->declaration->id != CTF_TYPE_VARIANT) ||
1620 bt_ctf_validate_identifier(name)) {
1621 ret = -1;
1622 goto end;
1623 }
1624
1625 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1626 g_string_assign(variant->tag_name, name);
1627 end:
1628 return ret;
1629 }
1630
1631 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type,
1632 struct bt_ctf_field_type *field_type,
1633 const char *field_name)
1634 {
1635 size_t i;
1636 int ret = 0;
1637 struct bt_ctf_field_type_variant *variant;
1638 GQuark field_name_quark = g_quark_from_string(field_name);
1639
1640 if (!type || !field_type || type->frozen ||
1641 bt_ctf_validate_identifier(field_name) ||
1642 (type->declaration->id != CTF_TYPE_VARIANT)) {
1643 ret = -1;
1644 goto end;
1645 }
1646
1647 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1648
1649 /* The user has explicitly provided a tag; validate against it. */
1650 if (variant->tag) {
1651 int name_found = 0;
1652
1653 /* Make sure this name is present in the enum tag */
1654 for (i = 0; i < variant->tag->entries->len; i++) {
1655 struct enumeration_mapping *mapping =
1656 g_ptr_array_index(variant->tag->entries, i);
1657
1658 if (mapping->string == field_name_quark) {
1659 name_found = 1;
1660 break;
1661 }
1662 }
1663
1664 if (!name_found) {
1665 /* Validation failed */
1666 ret = -1;
1667 goto end;
1668 }
1669 }
1670
1671 if (add_structure_field(variant->fields, variant->field_name_to_index,
1672 field_type, field_name)) {
1673 ret = -1;
1674 goto end;
1675 }
1676 end:
1677 return ret;
1678 }
1679
1680 BT_HIDDEN
1681 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name(
1682 struct bt_ctf_field_type *type,
1683 const char *field_name)
1684 {
1685 size_t index;
1686 GQuark name_quark;
1687 struct structure_field *field;
1688 struct bt_ctf_field_type_variant *variant;
1689 struct bt_ctf_field_type *field_type = NULL;
1690
1691 if (!type || !field_name) {
1692 goto end;
1693 }
1694
1695 name_quark = g_quark_try_string(field_name);
1696 if (!name_quark) {
1697 goto end;
1698 }
1699
1700 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1701 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
1702 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
1703 goto end;
1704 }
1705
1706 field = g_ptr_array_index(variant->fields, index);
1707 field_type = field->type;
1708 bt_get(field_type);
1709 end:
1710 return field_type;
1711 }
1712
1713 BT_HIDDEN
1714 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(
1715 struct bt_ctf_field_type *type,
1716 struct bt_ctf_field *tag)
1717 {
1718 const char *enum_value;
1719 struct bt_ctf_field_type *field_type = NULL;
1720
1721 if (!type || !tag || type->declaration->id != CTF_TYPE_VARIANT) {
1722 goto end;
1723 }
1724
1725 enum_value = bt_ctf_field_enumeration_get_mapping_name(tag);
1726 if (!enum_value) {
1727 goto end;
1728 }
1729
1730 /* Already increments field_type's reference count */
1731 field_type = bt_ctf_field_type_variant_get_field_type_by_name(
1732 type, enum_value);
1733 end:
1734 return field_type;
1735 }
1736
1737 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type)
1738 {
1739 int ret = 0;
1740 struct bt_ctf_field_type_variant *variant;
1741
1742 if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) {
1743 ret = -1;
1744 goto end;
1745 }
1746
1747 variant = container_of(type, struct bt_ctf_field_type_variant,
1748 parent);
1749 ret = (int) variant->fields->len;
1750 end:
1751 return ret;
1752
1753 }
1754
1755 BT_HIDDEN
1756 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *type,
1757 const char **field_name, struct bt_ctf_field_type **field_type,
1758 int index)
1759 {
1760 struct bt_ctf_field_type_variant *variant;
1761 struct structure_field *field;
1762 int ret = 0;
1763
1764 if (!type || index < 0 ||
1765 (type->declaration->id != CTF_TYPE_VARIANT)) {
1766 ret = -1;
1767 goto end;
1768 }
1769
1770 variant = container_of(type, struct bt_ctf_field_type_variant,
1771 parent);
1772 if (index >= variant->fields->len) {
1773 ret = -1;
1774 goto end;
1775 }
1776
1777 field = g_ptr_array_index(variant->fields, index);
1778 if (field_type) {
1779 *field_type = field->type;
1780 bt_get(field->type);
1781 }
1782 if (field_name) {
1783 *field_name = g_quark_to_string(field->name);
1784 }
1785 end:
1786 return ret;
1787 }
1788
1789 struct bt_ctf_field_type *bt_ctf_field_type_array_create(
1790 struct bt_ctf_field_type *element_type,
1791 unsigned int length)
1792 {
1793 struct bt_ctf_field_type_array *array = NULL;
1794
1795 if (!element_type || length == 0) {
1796 goto error;
1797 }
1798
1799 array = g_new0(struct bt_ctf_field_type_array, 1);
1800 if (!array) {
1801 goto error;
1802 }
1803
1804 array->parent.declaration = &array->declaration.p;
1805 array->parent.declaration->id = CTF_TYPE_ARRAY;
1806
1807 bt_get(element_type);
1808 array->element_type = element_type;
1809 array->length = length;
1810 bt_ctf_field_type_init(&array->parent, FALSE);
1811 return &array->parent;
1812 error:
1813 return NULL;
1814 }
1815
1816 BT_HIDDEN
1817 struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
1818 struct bt_ctf_field_type *type)
1819 {
1820 struct bt_ctf_field_type *ret = NULL;
1821 struct bt_ctf_field_type_array *array;
1822
1823 if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) {
1824 goto end;
1825 }
1826
1827 array = container_of(type, struct bt_ctf_field_type_array, parent);
1828 ret = array->element_type;
1829 bt_get(ret);
1830 end:
1831 return ret;
1832 }
1833
1834 BT_HIDDEN
1835 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *type,
1836 struct bt_ctf_field_type *element_type)
1837 {
1838 int ret = 0;
1839 struct bt_ctf_field_type_array *array;
1840
1841 if (!type || !element_type ||
1842 (type->declaration->id != CTF_TYPE_ARRAY)) {
1843 ret = -1;
1844 goto end;
1845 }
1846
1847 array = container_of(type, struct bt_ctf_field_type_array, parent);
1848
1849 if (array->element_type) {
1850 BT_PUT(array->element_type);
1851 }
1852
1853 array->element_type = element_type;
1854 bt_get(array->element_type);
1855
1856 end:
1857 return ret;
1858 }
1859
1860 BT_HIDDEN
1861 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type)
1862 {
1863 int64_t ret;
1864 struct bt_ctf_field_type_array *array;
1865
1866 if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) {
1867 ret = -1;
1868 goto end;
1869 }
1870
1871 array = container_of(type, struct bt_ctf_field_type_array, parent);
1872 ret = (int64_t) array->length;
1873 end:
1874 return ret;
1875 }
1876
1877 struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(
1878 struct bt_ctf_field_type *element_type,
1879 const char *length_field_name)
1880 {
1881 struct bt_ctf_field_type_sequence *sequence = NULL;
1882
1883 if (!element_type || bt_ctf_validate_identifier(length_field_name)) {
1884 goto error;
1885 }
1886
1887 sequence = g_new0(struct bt_ctf_field_type_sequence, 1);
1888 if (!sequence) {
1889 goto error;
1890 }
1891
1892 sequence->parent.declaration = &sequence->declaration.p;
1893 sequence->parent.declaration->id = CTF_TYPE_SEQUENCE;
1894 bt_get(element_type);
1895 sequence->element_type = element_type;
1896 sequence->length_field_name = g_string_new(length_field_name);
1897 bt_ctf_field_type_init(&sequence->parent, FALSE);
1898 return &sequence->parent;
1899 error:
1900 return NULL;
1901 }
1902
1903 BT_HIDDEN
1904 struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
1905 struct bt_ctf_field_type *type)
1906 {
1907 struct bt_ctf_field_type *ret = NULL;
1908 struct bt_ctf_field_type_sequence *sequence;
1909
1910 if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) {
1911 goto end;
1912 }
1913
1914 sequence = container_of(type, struct bt_ctf_field_type_sequence,
1915 parent);
1916 ret = sequence->element_type;
1917 bt_get(ret);
1918 end:
1919 return ret;
1920 }
1921
1922 BT_HIDDEN
1923 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *type,
1924 struct bt_ctf_field_type *element_type)
1925 {
1926 int ret = 0;
1927 struct bt_ctf_field_type_sequence *sequence;
1928
1929 if (!type || !element_type ||
1930 (type->declaration->id != CTF_TYPE_SEQUENCE)) {
1931 ret = -1;
1932 goto end;
1933 }
1934
1935 sequence = container_of(type, struct bt_ctf_field_type_sequence, parent);
1936
1937 if (sequence->element_type) {
1938 BT_PUT(sequence->element_type);
1939 }
1940
1941 sequence->element_type = element_type;
1942 bt_get(sequence->element_type);
1943
1944 end:
1945 return ret;
1946 }
1947
1948 BT_HIDDEN
1949 const char *bt_ctf_field_type_sequence_get_length_field_name(
1950 struct bt_ctf_field_type *type)
1951 {
1952 const char *ret = NULL;
1953 struct bt_ctf_field_type_sequence *sequence;
1954
1955 if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) {
1956 goto end;
1957 }
1958
1959 sequence = container_of(type, struct bt_ctf_field_type_sequence,
1960 parent);
1961 ret = sequence->length_field_name->str;
1962 end:
1963 return ret;
1964 }
1965
1966 struct bt_ctf_field_type *bt_ctf_field_type_string_create(void)
1967 {
1968 struct bt_ctf_field_type_string *string =
1969 g_new0(struct bt_ctf_field_type_string, 1);
1970
1971 if (!string) {
1972 return NULL;
1973 }
1974
1975 string->parent.declaration = &string->declaration.p;
1976 string->parent.declaration->id = CTF_TYPE_STRING;
1977 bt_ctf_field_type_init(&string->parent, TRUE);
1978 string->declaration.encoding =
1979 (enum ctf_string_encoding) BT_CTF_STRING_ENCODING_UTF8;
1980 string->parent.declaration->alignment = CHAR_BIT;
1981 return &string->parent;
1982 }
1983
1984 BT_HIDDEN
1985 enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding(
1986 struct bt_ctf_field_type *type)
1987 {
1988 struct bt_ctf_field_type_string *string;
1989 enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN;
1990
1991 if (!type || (type->declaration->id != CTF_TYPE_STRING)) {
1992 goto end;
1993 }
1994
1995 string = container_of(type, struct bt_ctf_field_type_string,
1996 parent);
1997 ret = (enum bt_ctf_string_encoding) string->declaration.encoding;
1998 end:
1999 return ret;
2000 }
2001
2002 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type,
2003 enum ctf_string_encoding encoding)
2004 {
2005 int ret = 0;
2006 struct bt_ctf_field_type_string *string;
2007
2008 if (!type || type->declaration->id != CTF_TYPE_STRING ||
2009 (encoding != CTF_STRING_UTF8 &&
2010 encoding != CTF_STRING_ASCII)) {
2011 ret = -1;
2012 goto end;
2013 }
2014
2015 string = container_of(type, struct bt_ctf_field_type_string, parent);
2016 string->declaration.encoding = encoding;
2017 end:
2018 return ret;
2019 }
2020
2021 BT_HIDDEN
2022 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type)
2023 {
2024 int ret;
2025 enum ctf_type_id type_id;
2026
2027 if (!type) {
2028 ret = -1;
2029 goto end;
2030 }
2031
2032 if (type->frozen) {
2033 ret = (int) type->declaration->alignment;
2034 goto end;
2035 }
2036
2037 type_id = bt_ctf_field_type_get_type_id(type);
2038 switch (type_id) {
2039 case CTF_TYPE_SEQUENCE:
2040 {
2041 struct bt_ctf_field_type *element =
2042 bt_ctf_field_type_sequence_get_element_type(type);
2043
2044 if (!element) {
2045 ret = -1;
2046 goto end;
2047 }
2048
2049 ret = bt_ctf_field_type_get_alignment(element);
2050 bt_put(element);
2051 break;
2052 }
2053 case CTF_TYPE_ARRAY:
2054 {
2055 struct bt_ctf_field_type *element =
2056 bt_ctf_field_type_array_get_element_type(type);
2057
2058 if (!element) {
2059 ret = -1;
2060 goto end;
2061 }
2062
2063 ret = bt_ctf_field_type_get_alignment(element);
2064 bt_put(element);
2065 break;
2066 }
2067 case CTF_TYPE_STRUCT:
2068 {
2069 int i, element_count;
2070
2071 element_count = bt_ctf_field_type_structure_get_field_count(
2072 type);
2073 if (element_count < 0) {
2074 ret = element_count;
2075 goto end;
2076 }
2077
2078 for (i = 0; i < element_count; i++) {
2079 struct bt_ctf_field_type *field;
2080 int field_alignment;
2081
2082 ret = bt_ctf_field_type_structure_get_field(type, NULL,
2083 &field, i);
2084 if (ret) {
2085 goto end;
2086 }
2087
2088 assert(field);
2089 field_alignment = bt_ctf_field_type_get_alignment(
2090 field);
2091 bt_put(field);
2092 if (field_alignment < 0) {
2093 ret = field_alignment;
2094 goto end;
2095 }
2096
2097 type->declaration->alignment = MAX(field_alignment,
2098 type->declaration->alignment);
2099 }
2100 ret = (int) type->declaration->alignment;
2101 break;
2102 }
2103 case CTF_TYPE_UNKNOWN:
2104 ret = -1;
2105 break;
2106 default:
2107 ret = (int) type->declaration->alignment;
2108 break;
2109 }
2110 end:
2111 return ret;
2112 }
2113
2114 static inline
2115 int is_power_of_two(unsigned int value)
2116 {
2117 return ((value & (value - 1)) == 0) && value > 0;
2118 }
2119
2120 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type,
2121 unsigned int alignment)
2122 {
2123 int ret = 0;
2124 enum ctf_type_id type_id;
2125
2126 /* Alignment must be a power of two */
2127 if (!type || type->frozen || !is_power_of_two(alignment)) {
2128 ret = -1;
2129 goto end;
2130 }
2131
2132 type_id = bt_ctf_field_type_get_type_id(type);
2133 if (type_id == CTF_TYPE_UNKNOWN) {
2134 ret = -1;
2135 goto end;
2136 }
2137
2138 if (type->declaration->id == CTF_TYPE_STRING &&
2139 alignment != CHAR_BIT) {
2140 ret = -1;
2141 goto end;
2142 }
2143
2144 if (type_id == CTF_TYPE_VARIANT ||
2145 type_id == CTF_TYPE_SEQUENCE ||
2146 type_id == CTF_TYPE_ARRAY) {
2147 /* Setting an alignment on these types makes no sense */
2148 ret = -1;
2149 goto end;
2150 }
2151
2152 type->declaration->alignment = alignment;
2153 ret = 0;
2154 end:
2155 return ret;
2156 }
2157
2158 BT_HIDDEN
2159 enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order(
2160 struct bt_ctf_field_type *type)
2161 {
2162 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
2163
2164 if (!type) {
2165 goto end;
2166 }
2167
2168 switch (type->declaration->id) {
2169 case CTF_TYPE_INTEGER:
2170 {
2171 struct bt_ctf_field_type_integer *integer = container_of(
2172 type, struct bt_ctf_field_type_integer, parent);
2173 ret = integer->user_byte_order;
2174 break;
2175 }
2176 case CTF_TYPE_FLOAT:
2177 {
2178 struct bt_ctf_field_type_floating_point *floating_point =
2179 container_of(type,
2180 struct bt_ctf_field_type_floating_point,
2181 parent);
2182 ret = floating_point->user_byte_order;
2183 break;
2184 }
2185 default:
2186 goto end;
2187 }
2188
2189 assert(ret == BT_CTF_BYTE_ORDER_NATIVE ||
2190 ret == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
2191 ret == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
2192 ret == BT_CTF_BYTE_ORDER_NETWORK);
2193
2194 end:
2195 return ret;
2196 }
2197
2198 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type,
2199 enum bt_ctf_byte_order byte_order)
2200 {
2201 int ret = 0;
2202 int internal_byte_order;
2203 enum ctf_type_id type_id;
2204
2205 if (!type || type->frozen) {
2206 ret = -1;
2207 goto end;
2208 }
2209
2210 switch (byte_order) {
2211 case BT_CTF_BYTE_ORDER_NATIVE:
2212 /* Leave unset. Will be initialized by parent. */
2213 internal_byte_order = 0;
2214 break;
2215 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
2216 internal_byte_order = LITTLE_ENDIAN;
2217 break;
2218 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
2219 case BT_CTF_BYTE_ORDER_NETWORK:
2220 internal_byte_order = BIG_ENDIAN;
2221 break;
2222 default:
2223 ret = -1;
2224 goto end;
2225 }
2226
2227 type_id = type->declaration->id;
2228 if (set_byte_order_funcs[type_id]) {
2229 set_byte_order_funcs[type_id](type, internal_byte_order, 0);
2230 }
2231 end:
2232 return ret;
2233 }
2234
2235 BT_HIDDEN
2236 enum ctf_type_id bt_ctf_field_type_get_type_id(
2237 struct bt_ctf_field_type *type)
2238 {
2239 if (!type) {
2240 return CTF_TYPE_UNKNOWN;
2241 }
2242
2243 return type->declaration->id;
2244 }
2245
2246 BT_HIDDEN
2247 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type)
2248 {
2249 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_INTEGER;
2250 }
2251
2252 BT_HIDDEN
2253 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type)
2254 {
2255 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_FLOAT;
2256 }
2257
2258 BT_HIDDEN
2259 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type)
2260 {
2261 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_ENUM;
2262 }
2263
2264 BT_HIDDEN
2265 int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type)
2266 {
2267 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_STRING;
2268 }
2269
2270 BT_HIDDEN
2271 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type)
2272 {
2273 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_STRUCT;
2274 }
2275
2276 BT_HIDDEN
2277 int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type)
2278 {
2279 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_ARRAY;
2280 }
2281
2282 BT_HIDDEN
2283 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type)
2284 {
2285 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_SEQUENCE;
2286 }
2287
2288 BT_HIDDEN
2289 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type)
2290 {
2291 return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_VARIANT;
2292 }
2293
2294 void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
2295 {
2296 bt_get(type);
2297 }
2298
2299 void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
2300 {
2301 bt_put(type);
2302 }
2303
2304 BT_HIDDEN
2305 void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type)
2306 {
2307 if (!type) {
2308 return;
2309 }
2310
2311 type->freeze(type);
2312 }
2313
2314 BT_HIDDEN
2315 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
2316 struct bt_ctf_field_type_variant *variant,
2317 int64_t tag_value)
2318 {
2319 struct bt_ctf_field_type *type = NULL;
2320 GQuark field_name_quark;
2321 gpointer index;
2322 struct structure_field *field_entry;
2323 struct range_overlap_query query = {
2324 .range_start._signed = tag_value,
2325 .range_end._signed = tag_value,
2326 .mapping_name = 0, .overlaps = 0};
2327
2328 g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap,
2329 &query);
2330 if (!query.overlaps) {
2331 goto end;
2332 }
2333
2334 field_name_quark = query.mapping_name;
2335 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
2336 GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
2337 goto end;
2338 }
2339
2340 field_entry = g_ptr_array_index(variant->fields, (size_t) index);
2341 type = field_entry->type;
2342 end:
2343 return type;
2344 }
2345
2346 BT_HIDDEN
2347 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
2348 struct bt_ctf_field_type_variant *variant,
2349 uint64_t tag_value)
2350 {
2351 struct bt_ctf_field_type *type = NULL;
2352 GQuark field_name_quark;
2353 gpointer index;
2354 struct structure_field *field_entry;
2355 struct range_overlap_query query = {
2356 .range_start._unsigned = tag_value,
2357 .range_end._unsigned = tag_value,
2358 .mapping_name = 0, .overlaps = 0};
2359
2360 g_ptr_array_foreach(variant->tag->entries,
2361 check_ranges_overlap_unsigned,
2362 &query);
2363 if (!query.overlaps) {
2364 goto end;
2365 }
2366
2367 field_name_quark = query.mapping_name;
2368 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
2369 GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
2370 goto end;
2371 }
2372
2373 field_entry = g_ptr_array_index(variant->fields, (size_t)index);
2374 type = field_entry->type;
2375 end:
2376 return type;
2377 }
2378
2379 BT_HIDDEN
2380 int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
2381 struct metadata_context *context)
2382 {
2383 int ret;
2384
2385 if (!type || !context) {
2386 ret = -1;
2387 goto end;
2388 }
2389
2390 /* Make sure field type is valid before serializing it */
2391 ret = bt_ctf_field_type_validate(type);
2392
2393 if (ret) {
2394 goto end;
2395 }
2396
2397 ret = type->serialize(type, context);
2398 end:
2399 return ret;
2400 }
2401
2402 BT_HIDDEN
2403 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type,
2404 int byte_order)
2405 {
2406 if (!type) {
2407 return;
2408 }
2409
2410 assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN);
2411 if (set_byte_order_funcs[type->declaration->id]) {
2412 set_byte_order_funcs[type->declaration->id](type,
2413 byte_order, 1);
2414 }
2415 }
2416
2417 BT_HIDDEN
2418 struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type)
2419 {
2420 struct bt_ctf_field_type *copy = NULL;
2421
2422 if (!type) {
2423 goto end;
2424 }
2425
2426 copy = type_copy_funcs[type->declaration->id](type);
2427 end:
2428 return copy;
2429 }
2430
2431 BT_HIDDEN
2432 int bt_ctf_field_type_structure_get_field_name_index(
2433 struct bt_ctf_field_type *type, const char *name)
2434 {
2435 int ret;
2436 size_t index;
2437 GQuark name_quark;
2438 struct bt_ctf_field_type_structure *structure;
2439
2440 if (!type || !name ||
2441 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) {
2442 ret = -1;
2443 goto end;
2444 }
2445
2446 name_quark = g_quark_try_string(name);
2447 if (!name_quark) {
2448 ret = -1;
2449 goto end;
2450 }
2451
2452 structure = container_of(type, struct bt_ctf_field_type_structure,
2453 parent);
2454 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
2455 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
2456 ret = -1;
2457 goto end;
2458 }
2459 ret = (int) index;
2460 end:
2461 return ret;
2462 }
2463
2464 BT_HIDDEN
2465 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type *type,
2466 struct bt_ctf_field_type *field, int index)
2467 {
2468 int ret = 0;
2469 struct bt_ctf_field_type_structure *structure;
2470
2471 if (!type || !field ||
2472 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) {
2473 ret = -1;
2474 goto end;
2475 }
2476
2477 structure = container_of(type, struct bt_ctf_field_type_structure,
2478 parent);
2479 if (index < 0 || index >= structure->fields->len) {
2480 ret = -1;
2481 goto end;
2482 }
2483
2484 bt_get(field);
2485 bt_put(((struct structure_field *)
2486 g_ptr_array_index(structure->fields, index))->type);
2487 ((struct structure_field *) structure->fields->pdata[index])->type =
2488 field;
2489 end:
2490 return ret;
2491 }
2492
2493 BT_HIDDEN
2494 int bt_ctf_field_type_variant_get_field_name_index(
2495 struct bt_ctf_field_type *type, const char *name)
2496 {
2497 int ret;
2498 size_t index;
2499 GQuark name_quark;
2500 struct bt_ctf_field_type_variant *variant;
2501
2502 if (!type || !name ||
2503 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) {
2504 ret = -1;
2505 goto end;
2506 }
2507
2508 name_quark = g_quark_try_string(name);
2509 if (!name_quark) {
2510 ret = -1;
2511 goto end;
2512 }
2513
2514 variant = container_of(type, struct bt_ctf_field_type_variant,
2515 parent);
2516 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
2517 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
2518 ret = -1;
2519 goto end;
2520 }
2521 ret = (int) index;
2522 end:
2523 return ret;
2524 }
2525
2526 BT_HIDDEN
2527 int bt_ctf_field_type_sequence_set_length_field_path(
2528 struct bt_ctf_field_type *type,
2529 struct bt_ctf_field_path *path)
2530 {
2531 int ret = 0;
2532 struct bt_ctf_field_type_sequence *sequence;
2533
2534 if (!type || bt_ctf_field_type_get_type_id(type) !=
2535 CTF_TYPE_SEQUENCE) {
2536 ret = -1;
2537 goto end;
2538 }
2539
2540 sequence = container_of(type, struct bt_ctf_field_type_sequence,
2541 parent);
2542 bt_get(path);
2543 BT_MOVE(sequence->length_field_path, path);
2544 end:
2545 return ret;
2546 }
2547
2548 BT_HIDDEN
2549 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
2550 struct bt_ctf_field_path *path)
2551 {
2552 int ret = 0;
2553 struct bt_ctf_field_type_variant *variant;
2554
2555 if (!type || bt_ctf_field_type_get_type_id(type) !=
2556 CTF_TYPE_VARIANT) {
2557 ret = -1;
2558 goto end;
2559 }
2560
2561 variant = container_of(type, struct bt_ctf_field_type_variant,
2562 parent);
2563 bt_get(path);
2564 BT_MOVE(variant->tag_field_path, path);
2565 end:
2566 return ret;
2567 }
2568
2569 BT_HIDDEN
2570 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
2571 struct bt_ctf_field_type *tag)
2572 {
2573 int ret = 0;
2574 struct bt_ctf_field_type_variant *variant;
2575
2576 if (!type || !tag ||
2577 bt_ctf_field_type_get_type_id(tag) !=
2578 CTF_TYPE_ENUM) {
2579 ret = -1;
2580 goto end;
2581 }
2582
2583 variant = container_of(type, struct bt_ctf_field_type_variant,
2584 parent);
2585 bt_get(tag);
2586 if (variant->tag) {
2587 bt_put(&variant->tag->parent);
2588 }
2589 variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration,
2590 parent);
2591 end:
2592 return ret;
2593 }
2594
2595 BT_HIDDEN
2596 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type *type,
2597 struct bt_ctf_field_type *field, int index)
2598 {
2599 int ret = 0;
2600 struct bt_ctf_field_type_variant *variant;
2601
2602 if (!type || !field ||
2603 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) {
2604 ret = -1;
2605 goto end;
2606 }
2607
2608 variant = container_of(type, struct bt_ctf_field_type_variant,
2609 parent);
2610 if (index < 0 || index >= variant->fields->len) {
2611 ret = -1;
2612 goto end;
2613 }
2614
2615 bt_get(field);
2616 bt_put(((struct structure_field *)
2617 g_ptr_array_index(variant->fields, index))->type);
2618 ((struct structure_field *) variant->fields->pdata[index])->type =
2619 field;
2620 end:
2621 return ret;
2622 }
2623
2624 static
2625 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type)
2626 {
2627 struct bt_ctf_field_type_integer *integer =
2628 (struct bt_ctf_field_type_integer *) type;
2629
2630 if (!type) {
2631 return;
2632 }
2633
2634 bt_put(integer->mapped_clock);
2635 g_free(integer);
2636 }
2637
2638 static
2639 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type)
2640 {
2641 struct bt_ctf_field_type_enumeration *enumeration =
2642 (struct bt_ctf_field_type_enumeration *) type;
2643
2644 if (!type) {
2645 return;
2646 }
2647
2648 g_ptr_array_free(enumeration->entries, TRUE);
2649 bt_put(enumeration->container);
2650 g_free(enumeration);
2651 }
2652
2653 static
2654 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type)
2655 {
2656 struct bt_ctf_field_type_floating_point *floating_point =
2657 (struct bt_ctf_field_type_floating_point *) type;
2658
2659 if (!type) {
2660 return;
2661 }
2662
2663 g_free(floating_point);
2664 }
2665
2666 static
2667 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type)
2668 {
2669 struct bt_ctf_field_type_structure *structure =
2670 (struct bt_ctf_field_type_structure *) type;
2671
2672 if (!type) {
2673 return;
2674 }
2675
2676 g_ptr_array_free(structure->fields, TRUE);
2677 g_hash_table_destroy(structure->field_name_to_index);
2678 g_free(structure);
2679 }
2680
2681 static
2682 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type)
2683 {
2684 struct bt_ctf_field_type_variant *variant =
2685 (struct bt_ctf_field_type_variant *) type;
2686
2687 if (!type) {
2688 return;
2689 }
2690
2691 g_ptr_array_free(variant->fields, TRUE);
2692 g_hash_table_destroy(variant->field_name_to_index);
2693 g_string_free(variant->tag_name, TRUE);
2694 bt_put(&variant->tag->parent);
2695 BT_PUT(variant->tag_field_path);
2696 g_free(variant);
2697 }
2698
2699 static
2700 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type)
2701 {
2702 struct bt_ctf_field_type_array *array =
2703 (struct bt_ctf_field_type_array *) type;
2704
2705 if (!type) {
2706 return;
2707 }
2708
2709 bt_put(array->element_type);
2710 g_free(array);
2711 }
2712
2713 static
2714 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type)
2715 {
2716 struct bt_ctf_field_type_sequence *sequence =
2717 (struct bt_ctf_field_type_sequence *) type;
2718
2719 if (!type) {
2720 return;
2721 }
2722
2723 bt_put(sequence->element_type);
2724 g_string_free(sequence->length_field_name, TRUE);
2725 BT_PUT(sequence->length_field_path);
2726 g_free(sequence);
2727 }
2728
2729 static
2730 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type)
2731 {
2732 struct bt_ctf_field_type_string *string =
2733 (struct bt_ctf_field_type_string *) type;
2734
2735 if (!type) {
2736 return;
2737 }
2738
2739 g_free(string);
2740 }
2741
2742 static
2743 void generic_field_type_freeze(struct bt_ctf_field_type *type)
2744 {
2745 type->frozen = 1;
2746 }
2747
2748 static
2749 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *type)
2750 {
2751 struct bt_ctf_field_type_integer *integer_type = container_of(
2752 type, struct bt_ctf_field_type_integer, parent);
2753
2754 if (integer_type->mapped_clock) {
2755 bt_ctf_clock_freeze(integer_type->mapped_clock);
2756 }
2757
2758 generic_field_type_freeze(type);
2759 }
2760
2761 static
2762 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type)
2763 {
2764 struct bt_ctf_field_type_enumeration *enumeration_type = container_of(
2765 type, struct bt_ctf_field_type_enumeration, parent);
2766
2767 generic_field_type_freeze(type);
2768 bt_ctf_field_type_freeze(enumeration_type->container);
2769 }
2770
2771 static
2772 void freeze_structure_field(struct structure_field *field)
2773 {
2774 bt_ctf_field_type_freeze(field->type);
2775 }
2776
2777 static
2778 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type)
2779 {
2780 struct bt_ctf_field_type_structure *structure_type = container_of(
2781 type, struct bt_ctf_field_type_structure, parent);
2782
2783 /* Cache the alignment */
2784 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
2785 generic_field_type_freeze(type);
2786 g_ptr_array_foreach(structure_type->fields,
2787 (GFunc) freeze_structure_field, NULL);
2788 }
2789
2790 static
2791 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type)
2792 {
2793 struct bt_ctf_field_type_variant *variant_type = container_of(
2794 type, struct bt_ctf_field_type_variant, parent);
2795
2796 generic_field_type_freeze(type);
2797 g_ptr_array_foreach(variant_type->fields,
2798 (GFunc) freeze_structure_field, NULL);
2799 }
2800
2801 static
2802 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type)
2803 {
2804 struct bt_ctf_field_type_array *array_type = container_of(
2805 type, struct bt_ctf_field_type_array, parent);
2806
2807 /* Cache the alignment */
2808 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
2809 generic_field_type_freeze(type);
2810 bt_ctf_field_type_freeze(array_type->element_type);
2811 }
2812
2813 static
2814 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type)
2815 {
2816 struct bt_ctf_field_type_sequence *sequence_type = container_of(
2817 type, struct bt_ctf_field_type_sequence, parent);
2818
2819 /* Cache the alignment */
2820 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
2821 generic_field_type_freeze(type);
2822 bt_ctf_field_type_freeze(sequence_type->element_type);
2823 }
2824
2825 static
2826 const char *get_encoding_string(enum bt_ctf_string_encoding encoding)
2827 {
2828 const char *encoding_string;
2829
2830 switch (encoding) {
2831 case BT_CTF_STRING_ENCODING_NONE:
2832 encoding_string = "none";
2833 break;
2834 case BT_CTF_STRING_ENCODING_ASCII:
2835 encoding_string = "ASCII";
2836 break;
2837 case BT_CTF_STRING_ENCODING_UTF8:
2838 encoding_string = "UTF8";
2839 break;
2840 default:
2841 encoding_string = "unknown";
2842 break;
2843 }
2844
2845 return encoding_string;
2846 }
2847
2848 static
2849 const char *get_integer_base_string(enum bt_ctf_integer_base base)
2850 {
2851 const char *base_string;
2852
2853 switch (base) {
2854 case BT_CTF_INTEGER_BASE_DECIMAL:
2855 base_string = "decimal";
2856 break;
2857 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
2858 base_string = "hexadecimal";
2859 break;
2860 case BT_CTF_INTEGER_BASE_OCTAL:
2861 base_string = "octal";
2862 break;
2863 case BT_CTF_INTEGER_BASE_BINARY:
2864 base_string = "binary";
2865 break;
2866 default:
2867 base_string = "unknown";
2868 break;
2869 }
2870
2871 return base_string;
2872 }
2873
2874 static
2875 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type,
2876 struct metadata_context *context)
2877 {
2878 struct bt_ctf_field_type_integer *integer = container_of(type,
2879 struct bt_ctf_field_type_integer, parent);
2880 int ret = 0;
2881
2882 g_string_append_printf(context->string,
2883 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2884 integer->declaration.len, type->declaration->alignment,
2885 (integer->declaration.signedness ? "true" : "false"),
2886 get_encoding_string((enum bt_ctf_string_encoding) integer->declaration.encoding),
2887 get_integer_base_string(integer->declaration.base),
2888 get_byte_order_string(integer->declaration.byte_order));
2889 if (integer->mapped_clock) {
2890 const char *clock_name = bt_ctf_clock_get_name(
2891 integer->mapped_clock);
2892
2893 if (!clock_name) {
2894 ret = -1;
2895 goto end;
2896 }
2897
2898 g_string_append_printf(context->string,
2899 "; map = clock.%s.value", clock_name);
2900 }
2901
2902 g_string_append(context->string, "; }");
2903 end:
2904 return ret;
2905 }
2906
2907 static
2908 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type,
2909 struct metadata_context *context)
2910 {
2911 size_t entry;
2912 int ret;
2913 struct bt_ctf_field_type_enumeration *enumeration = container_of(type,
2914 struct bt_ctf_field_type_enumeration, parent);
2915 struct bt_ctf_field_type *container_type;
2916 int container_signed;
2917
2918 container_type = bt_ctf_field_type_enumeration_get_container_type(type);
2919 if (!container_type) {
2920 ret = -1;
2921 goto end;
2922 }
2923
2924 container_signed = bt_ctf_field_type_integer_get_signed(container_type);
2925 if (container_signed < 0) {
2926 ret = container_signed;
2927 goto error_put_container_type;
2928 }
2929
2930 g_string_append(context->string, "enum : ");
2931 ret = bt_ctf_field_type_serialize(enumeration->container, context);
2932 if (ret) {
2933 goto error_put_container_type;
2934 }
2935
2936 g_string_append(context->string, " { ");
2937 for (entry = 0; entry < enumeration->entries->len; entry++) {
2938 struct enumeration_mapping *mapping =
2939 enumeration->entries->pdata[entry];
2940
2941 if (container_signed) {
2942 if (mapping->range_start._signed ==
2943 mapping->range_end._signed) {
2944 g_string_append_printf(context->string,
2945 "\"%s\" = %" PRId64,
2946 g_quark_to_string(mapping->string),
2947 mapping->range_start._signed);
2948 } else {
2949 g_string_append_printf(context->string,
2950 "\"%s\" = %" PRId64 " ... %" PRId64,
2951 g_quark_to_string(mapping->string),
2952 mapping->range_start._signed,
2953 mapping->range_end._signed);
2954 }
2955 } else {
2956 if (mapping->range_start._unsigned ==
2957 mapping->range_end._unsigned) {
2958 g_string_append_printf(context->string,
2959 "\"%s\" = %" PRIu64,
2960 g_quark_to_string(mapping->string),
2961 mapping->range_start._unsigned);
2962 } else {
2963 g_string_append_printf(context->string,
2964 "\"%s\" = %" PRIu64 " ... %" PRIu64,
2965 g_quark_to_string(mapping->string),
2966 mapping->range_start._unsigned,
2967 mapping->range_end._unsigned);
2968 }
2969 }
2970
2971 g_string_append(context->string,
2972 ((entry != (enumeration->entries->len - 1)) ?
2973 ", " : " }"));
2974 }
2975
2976 if (context->field_name->len) {
2977 g_string_append_printf(context->string, " %s",
2978 context->field_name->str);
2979 g_string_assign(context->field_name, "");
2980 }
2981 error_put_container_type:
2982 bt_put(container_type);
2983 end:
2984 return ret;
2985 }
2986
2987 static
2988 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type,
2989 struct metadata_context *context)
2990 {
2991 struct bt_ctf_field_type_floating_point *floating_point = container_of(
2992 type, struct bt_ctf_field_type_floating_point, parent);
2993
2994 g_string_append_printf(context->string,
2995 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2996 floating_point->declaration.exp->len,
2997 floating_point->declaration.mantissa->len + 1,
2998 get_byte_order_string(floating_point->declaration.byte_order),
2999 type->declaration->alignment);
3000 return 0;
3001 }
3002
3003 static
3004 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type,
3005 struct metadata_context *context)
3006 {
3007 size_t i;
3008 unsigned int indent;
3009 int ret = 0;
3010 struct bt_ctf_field_type_structure *structure = container_of(type,
3011 struct bt_ctf_field_type_structure, parent);
3012 GString *structure_field_name = context->field_name;
3013
3014 context->field_name = g_string_new("");
3015
3016 context->current_indentation_level++;
3017 g_string_append(context->string, "struct {\n");
3018
3019 for (i = 0; i < structure->fields->len; i++) {
3020 struct structure_field *field;
3021
3022 for (indent = 0; indent < context->current_indentation_level;
3023 indent++) {
3024 g_string_append_c(context->string, '\t');
3025 }
3026
3027 field = structure->fields->pdata[i];
3028 g_string_assign(context->field_name,
3029 g_quark_to_string(field->name));
3030 ret = bt_ctf_field_type_serialize(field->type, context);
3031 if (ret) {
3032 goto end;
3033 }
3034
3035 if (context->field_name->len) {
3036 g_string_append_printf(context->string, " %s",
3037 context->field_name->str);
3038 }
3039 g_string_append(context->string, ";\n");
3040 }
3041
3042 context->current_indentation_level--;
3043 for (indent = 0; indent < context->current_indentation_level;
3044 indent++) {
3045 g_string_append_c(context->string, '\t');
3046 }
3047
3048 g_string_append_printf(context->string, "} align(%zu)",
3049 type->declaration->alignment);
3050 end:
3051 g_string_free(context->field_name, TRUE);
3052 context->field_name = structure_field_name;
3053 return ret;
3054 }
3055
3056 static
3057 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type,
3058 struct metadata_context *context)
3059 {
3060 size_t i;
3061 unsigned int indent;
3062 int ret = 0;
3063 struct bt_ctf_field_type_variant *variant = container_of(
3064 type, struct bt_ctf_field_type_variant, parent);
3065 GString *variant_field_name = context->field_name;
3066
3067 context->field_name = g_string_new("");
3068 if (variant->tag_name->len > 0) {
3069 g_string_append_printf(context->string,
3070 "variant <%s> {\n", variant->tag_name->str);
3071 } else {
3072 g_string_append(context->string, "variant {\n");
3073 }
3074
3075 context->current_indentation_level++;
3076 for (i = 0; i < variant->fields->len; i++) {
3077 struct structure_field *field = variant->fields->pdata[i];
3078
3079 g_string_assign(context->field_name,
3080 g_quark_to_string(field->name));
3081 for (indent = 0; indent < context->current_indentation_level;
3082 indent++) {
3083 g_string_append_c(context->string, '\t');
3084 }
3085
3086 g_string_assign(context->field_name,
3087 g_quark_to_string(field->name));
3088 ret = bt_ctf_field_type_serialize(field->type, context);
3089 if (ret) {
3090 goto end;
3091 }
3092
3093 if (context->field_name->len) {
3094 g_string_append_printf(context->string, " %s;",
3095 context->field_name->str);
3096 }
3097
3098 g_string_append_c(context->string, '\n');
3099 }
3100
3101 context->current_indentation_level--;
3102 for (indent = 0; indent < context->current_indentation_level;
3103 indent++) {
3104 g_string_append_c(context->string, '\t');
3105 }
3106
3107 g_string_append(context->string, "}");
3108 end:
3109 g_string_free(context->field_name, TRUE);
3110 context->field_name = variant_field_name;
3111 return ret;
3112 }
3113
3114 static
3115 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type,
3116 struct metadata_context *context)
3117 {
3118 int ret = 0;
3119 struct bt_ctf_field_type_array *array = container_of(type,
3120 struct bt_ctf_field_type_array, parent);
3121
3122 ret = bt_ctf_field_type_serialize(array->element_type, context);
3123 if (ret) {
3124 goto end;
3125 }
3126
3127 if (context->field_name->len) {
3128 g_string_append_printf(context->string, " %s[%u]",
3129 context->field_name->str, array->length);
3130 g_string_assign(context->field_name, "");
3131 } else {
3132 g_string_append_printf(context->string, "[%u]", array->length);
3133 }
3134 end:
3135 return ret;
3136 }
3137
3138 static
3139 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type,
3140 struct metadata_context *context)
3141 {
3142 int ret = 0;
3143 struct bt_ctf_field_type_sequence *sequence = container_of(
3144 type, struct bt_ctf_field_type_sequence, parent);
3145
3146 ret = bt_ctf_field_type_serialize(sequence->element_type, context);
3147 if (ret) {
3148 goto end;
3149 }
3150
3151 if (context->field_name->len) {
3152 g_string_append_printf(context->string, " %s[%s]",
3153 context->field_name->str,
3154 sequence->length_field_name->str);
3155 g_string_assign(context->field_name, "");
3156 } else {
3157 g_string_append_printf(context->string, "[%s]",
3158 sequence->length_field_name->str);
3159 }
3160 end:
3161 return ret;
3162 }
3163
3164 static
3165 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type,
3166 struct metadata_context *context)
3167 {
3168 struct bt_ctf_field_type_string *string = container_of(
3169 type, struct bt_ctf_field_type_string, parent);
3170
3171 g_string_append_printf(context->string,
3172 "string { encoding = %s; }",
3173 get_encoding_string((enum bt_ctf_string_encoding) string->declaration.encoding));
3174 return 0;
3175 }
3176
3177 static
3178 enum bt_ctf_byte_order get_ctf_ir_byte_order(int byte_order) {
3179 enum bt_ctf_byte_order ret;
3180
3181 switch (byte_order) {
3182 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
3183 case LITTLE_ENDIAN:
3184 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
3185 break;
3186 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
3187 case BIG_ENDIAN:
3188 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
3189 break;
3190 case BT_CTF_BYTE_ORDER_NETWORK:
3191 ret = BT_CTF_BYTE_ORDER_NETWORK;
3192 break;
3193 case BT_CTF_BYTE_ORDER_NATIVE:
3194 ret = BT_CTF_BYTE_ORDER_NATIVE;
3195 break;
3196 default:
3197 ret = BT_CTF_BYTE_ORDER_UNKNOWN;
3198 break;
3199 }
3200
3201 return ret;
3202 }
3203
3204 static
3205 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type,
3206 int byte_order, int set_native)
3207 {
3208 struct bt_ctf_field_type_integer *integer_type = container_of(type,
3209 struct bt_ctf_field_type_integer, parent);
3210
3211 if (set_native) {
3212 if (integer_type->user_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
3213 /*
3214 * User byte order is native, so we can set
3215 * the real byte order.
3216 */
3217 integer_type->declaration.byte_order =
3218 byte_order;
3219 }
3220 } else {
3221 integer_type->user_byte_order =
3222 get_ctf_ir_byte_order(byte_order);
3223 integer_type->declaration.byte_order = byte_order;
3224 }
3225 }
3226
3227 static
3228 void bt_ctf_field_type_enumeration_set_byte_order(
3229 struct bt_ctf_field_type *type, int byte_order, int set_native)
3230 {
3231 struct bt_ctf_field_type_enumeration *enum_type = container_of(type,
3232 struct bt_ctf_field_type_enumeration, parent);
3233
3234 /* Safe to assume that container is an integer */
3235 bt_ctf_field_type_integer_set_byte_order(enum_type->container,
3236 byte_order, set_native);
3237 }
3238
3239 static
3240 void bt_ctf_field_type_floating_point_set_byte_order(
3241 struct bt_ctf_field_type *type, int byte_order, int set_native)
3242 {
3243 struct bt_ctf_field_type_floating_point *floating_point_type =
3244 container_of(type, struct bt_ctf_field_type_floating_point,
3245 parent);
3246
3247 if (set_native) {
3248 if (floating_point_type->user_byte_order ==
3249 BT_CTF_BYTE_ORDER_NATIVE) {
3250 /*
3251 * User byte order is native, so we can set
3252 * the real byte order.
3253 */
3254 floating_point_type->declaration.byte_order =
3255 byte_order;
3256 floating_point_type->sign.byte_order =
3257 byte_order;
3258 floating_point_type->mantissa.byte_order =
3259 byte_order;
3260 floating_point_type->exp.byte_order =
3261 byte_order;
3262 }
3263 } else {
3264 floating_point_type->user_byte_order =
3265 get_ctf_ir_byte_order(byte_order);
3266 floating_point_type->declaration.byte_order = byte_order;
3267 floating_point_type->sign.byte_order = byte_order;
3268 floating_point_type->mantissa.byte_order = byte_order;
3269 floating_point_type->exp.byte_order = byte_order;
3270 }
3271 }
3272
3273 static
3274 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type,
3275 int byte_order, int set_native)
3276 {
3277 int i;
3278 struct bt_ctf_field_type_structure *structure_type =
3279 container_of(type, struct bt_ctf_field_type_structure,
3280 parent);
3281
3282 for (i = 0; i < structure_type->fields->len; i++) {
3283 struct structure_field *field = g_ptr_array_index(
3284 structure_type->fields, i);
3285 struct bt_ctf_field_type *field_type = field->type;
3286
3287 if (set_byte_order_funcs[field_type->declaration->id]) {
3288 set_byte_order_funcs[field_type->declaration->id](
3289 field_type, byte_order, set_native);
3290 }
3291 }
3292 }
3293
3294 static
3295 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type,
3296 int byte_order, int set_native)
3297 {
3298 int i;
3299 struct bt_ctf_field_type_variant *variant_type =
3300 container_of(type, struct bt_ctf_field_type_variant,
3301 parent);
3302
3303 for (i = 0; i < variant_type->fields->len; i++) {
3304 struct structure_field *field = g_ptr_array_index(
3305 variant_type->fields, i);
3306 struct bt_ctf_field_type *field_type = field->type;
3307
3308 if (set_byte_order_funcs[field_type->declaration->id]) {
3309 set_byte_order_funcs[field_type->declaration->id](
3310 field_type, byte_order, set_native);
3311 }
3312 }
3313 }
3314
3315 static
3316 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type,
3317 int byte_order, int set_native)
3318 {
3319 struct bt_ctf_field_type_array *array_type =
3320 container_of(type, struct bt_ctf_field_type_array,
3321 parent);
3322
3323 if (set_byte_order_funcs[array_type->element_type->declaration->id]) {
3324 set_byte_order_funcs[array_type->element_type->declaration->id](
3325 array_type->element_type, byte_order, set_native);
3326 }
3327 }
3328
3329 static
3330 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type,
3331 int byte_order, int set_native)
3332 {
3333 struct bt_ctf_field_type_sequence *sequence_type =
3334 container_of(type, struct bt_ctf_field_type_sequence,
3335 parent);
3336
3337 if (set_byte_order_funcs[
3338 sequence_type->element_type->declaration->id]) {
3339 set_byte_order_funcs[
3340 sequence_type->element_type->declaration->id](
3341 sequence_type->element_type, byte_order, set_native);
3342 }
3343 }
3344
3345 static
3346 struct bt_ctf_field_type *bt_ctf_field_type_integer_copy(
3347 struct bt_ctf_field_type *type)
3348 {
3349 struct bt_ctf_field_type *copy;
3350 struct bt_ctf_field_type_integer *integer, *copy_integer;
3351
3352 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
3353 copy = bt_ctf_field_type_integer_create(integer->declaration.len);
3354 if (!copy) {
3355 goto end;
3356 }
3357
3358 copy_integer = container_of(copy, struct bt_ctf_field_type_integer,
3359 parent);
3360 copy_integer->declaration = integer->declaration;
3361 if (integer->mapped_clock) {
3362 bt_get(integer->mapped_clock);
3363 copy_integer->mapped_clock = integer->mapped_clock;
3364 }
3365
3366 copy_integer->user_byte_order = integer->user_byte_order;
3367
3368 end:
3369 return copy;
3370 }
3371
3372 static
3373 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
3374 struct bt_ctf_field_type *type)
3375 {
3376 size_t i;
3377 struct bt_ctf_field_type *copy = NULL, *copy_container;
3378 struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration;
3379
3380 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
3381 parent);
3382
3383 /* Copy the source enumeration's container */
3384 copy_container = bt_ctf_field_type_copy(enumeration->container);
3385 if (!copy_container) {
3386 goto end;
3387 }
3388
3389 copy = bt_ctf_field_type_enumeration_create(copy_container);
3390 if (!copy) {
3391 goto end;
3392 }
3393 copy_enumeration = container_of(copy,
3394 struct bt_ctf_field_type_enumeration, parent);
3395
3396 /* Copy all enumaration entries */
3397 for (i = 0; i < enumeration->entries->len; i++) {
3398 struct enumeration_mapping *mapping = g_ptr_array_index(
3399 enumeration->entries, i);
3400 struct enumeration_mapping* copy_mapping = g_new0(
3401 struct enumeration_mapping, 1);
3402
3403 if (!copy_mapping) {
3404 goto error;
3405 }
3406
3407 *copy_mapping = *mapping;
3408 g_ptr_array_add(copy_enumeration->entries, copy_mapping);
3409 }
3410
3411 copy_enumeration->declaration = enumeration->declaration;
3412 end:
3413 bt_put(copy_container);
3414 return copy;
3415 error:
3416 bt_put(copy_container);
3417 BT_PUT(copy);
3418 return copy;
3419 }
3420
3421 static
3422 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy(
3423 struct bt_ctf_field_type *type)
3424 {
3425 struct bt_ctf_field_type *copy;
3426 struct bt_ctf_field_type_floating_point *floating_point, *copy_float;
3427
3428 floating_point = container_of(type,
3429 struct bt_ctf_field_type_floating_point, parent);
3430 copy = bt_ctf_field_type_floating_point_create();
3431 if (!copy) {
3432 goto end;
3433 }
3434
3435 copy_float = container_of(copy,
3436 struct bt_ctf_field_type_floating_point, parent);
3437 copy_float->declaration = floating_point->declaration;
3438 copy_float->sign = floating_point->sign;
3439 copy_float->mantissa = floating_point->mantissa;
3440 copy_float->exp = floating_point->exp;
3441 copy_float->user_byte_order = floating_point->user_byte_order;
3442 copy_float->declaration.sign = &copy_float->sign;
3443 copy_float->declaration.mantissa = &copy_float->mantissa;
3444 copy_float->declaration.exp = &copy_float->exp;
3445 end:
3446 return copy;
3447 }
3448
3449 static
3450 struct bt_ctf_field_type *bt_ctf_field_type_structure_copy(
3451 struct bt_ctf_field_type *type)
3452 {
3453 int i;
3454 GHashTableIter iter;
3455 gpointer key, value;
3456 struct bt_ctf_field_type *copy;
3457 struct bt_ctf_field_type_structure *structure, *copy_structure;
3458
3459 structure = container_of(type, struct bt_ctf_field_type_structure,
3460 parent);
3461 copy = bt_ctf_field_type_structure_create();
3462 if (!copy) {
3463 goto end;
3464 }
3465
3466 copy_structure = container_of(copy,
3467 struct bt_ctf_field_type_structure, parent);
3468
3469 /* Copy field_name_to_index */
3470 g_hash_table_iter_init(&iter, structure->field_name_to_index);
3471 while (g_hash_table_iter_next (&iter, &key, &value)) {
3472 g_hash_table_insert(copy_structure->field_name_to_index,
3473 key, value);
3474 }
3475
3476 for (i = 0; i < structure->fields->len; i++) {
3477 struct structure_field *entry, *copy_entry;
3478 struct bt_ctf_field_type *copy_field;
3479
3480 copy_entry = g_new0(struct structure_field, 1);
3481 if (!copy_entry) {
3482 goto error;
3483 }
3484
3485 entry = g_ptr_array_index(structure->fields, i);
3486 copy_field = bt_ctf_field_type_copy(entry->type);
3487 if (!copy_field) {
3488 g_free(copy_entry);
3489 goto error;
3490 }
3491
3492 copy_entry->name = entry->name;
3493 copy_entry->type = copy_field;
3494 g_ptr_array_add(copy_structure->fields, copy_entry);
3495 }
3496
3497 copy_structure->declaration = structure->declaration;
3498 end:
3499 return copy;
3500 error:
3501 BT_PUT(copy);
3502 return copy;
3503 }
3504
3505 static
3506 struct bt_ctf_field_type *bt_ctf_field_type_variant_copy(
3507 struct bt_ctf_field_type *type)
3508 {
3509 int i;
3510 GHashTableIter iter;
3511 gpointer key, value;
3512 struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL;
3513 struct bt_ctf_field_type_variant *variant, *copy_variant;
3514
3515 variant = container_of(type, struct bt_ctf_field_type_variant,
3516 parent);
3517 if (variant->tag) {
3518 copy_tag = bt_ctf_field_type_copy(&variant->tag->parent);
3519 if (!copy_tag) {
3520 goto end;
3521 }
3522 }
3523
3524 copy = bt_ctf_field_type_variant_create(copy_tag,
3525 variant->tag_name->len ? variant->tag_name->str : NULL);
3526 if (!copy) {
3527 goto end;
3528 }
3529
3530 copy_variant = container_of(copy, struct bt_ctf_field_type_variant,
3531 parent);
3532
3533 /* Copy field_name_to_index */
3534 g_hash_table_iter_init(&iter, variant->field_name_to_index);
3535 while (g_hash_table_iter_next (&iter, &key, &value)) {
3536 g_hash_table_insert(copy_variant->field_name_to_index,
3537 key, value);
3538 }
3539
3540 for (i = 0; i < variant->fields->len; i++) {
3541 struct structure_field *entry, *copy_entry;
3542 struct bt_ctf_field_type *copy_field;
3543
3544 copy_entry = g_new0(struct structure_field, 1);
3545 if (!copy_entry) {
3546 goto error;
3547 }
3548
3549 entry = g_ptr_array_index(variant->fields, i);
3550 copy_field = bt_ctf_field_type_copy(entry->type);
3551 if (!copy_field) {
3552 g_free(copy_entry);
3553 goto error;
3554 }
3555
3556 copy_entry->name = entry->name;
3557 copy_entry->type = copy_field;
3558 g_ptr_array_add(copy_variant->fields, copy_entry);
3559 }
3560
3561 copy_variant->declaration = variant->declaration;
3562 if (variant->tag_field_path) {
3563 copy_variant->tag_field_path = bt_ctf_field_path_copy(
3564 variant->tag_field_path);
3565 if (!copy_variant->tag_field_path) {
3566 goto error;
3567 }
3568 }
3569 end:
3570 bt_put(copy_tag);
3571 return copy;
3572 error:
3573 bt_put(copy_tag);
3574 BT_PUT(copy);
3575 return copy;
3576 }
3577
3578 static
3579 struct bt_ctf_field_type *bt_ctf_field_type_array_copy(
3580 struct bt_ctf_field_type *type)
3581 {
3582 struct bt_ctf_field_type *copy = NULL, *copy_element;
3583 struct bt_ctf_field_type_array *array, *copy_array;
3584
3585 array = container_of(type, struct bt_ctf_field_type_array,
3586 parent);
3587 copy_element = bt_ctf_field_type_copy(array->element_type);
3588 if (!copy_element) {
3589 goto end;
3590 }
3591
3592 copy = bt_ctf_field_type_array_create(copy_element, array->length);
3593 if (!copy) {
3594 goto end;
3595 }
3596
3597 copy_array = container_of(copy, struct bt_ctf_field_type_array,
3598 parent);
3599 copy_array->declaration = array->declaration;
3600 end:
3601 bt_put(copy_element);
3602 return copy;
3603 }
3604
3605 static
3606 struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy(
3607 struct bt_ctf_field_type *type)
3608 {
3609 struct bt_ctf_field_type *copy = NULL, *copy_element;
3610 struct bt_ctf_field_type_sequence *sequence, *copy_sequence;
3611
3612 sequence = container_of(type, struct bt_ctf_field_type_sequence,
3613 parent);
3614 copy_element = bt_ctf_field_type_copy(sequence->element_type);
3615 if (!copy_element) {
3616 goto end;
3617 }
3618
3619 copy = bt_ctf_field_type_sequence_create(copy_element,
3620 sequence->length_field_name->len ?
3621 sequence->length_field_name->str : NULL);
3622 if (!copy) {
3623 goto end;
3624 }
3625
3626 copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence,
3627 parent);
3628 copy_sequence->declaration = sequence->declaration;
3629 if (sequence->length_field_path) {
3630 copy_sequence->length_field_path = bt_ctf_field_path_copy(
3631 sequence->length_field_path);
3632 if (!copy_sequence->length_field_path) {
3633 goto error;
3634 }
3635 }
3636 end:
3637 bt_put(copy_element);
3638 return copy;
3639 error:
3640 BT_PUT(copy);
3641 goto end;
3642 }
3643
3644 static
3645 struct bt_ctf_field_type *bt_ctf_field_type_string_copy(
3646 struct bt_ctf_field_type *type)
3647 {
3648 struct bt_ctf_field_type *copy;
3649 struct bt_ctf_field_type_string *string, *copy_string;
3650
3651 copy = bt_ctf_field_type_string_create();
3652 if (!copy) {
3653 goto end;
3654 }
3655
3656 string = container_of(type, struct bt_ctf_field_type_string,
3657 parent);
3658 copy_string = container_of(type, struct bt_ctf_field_type_string,
3659 parent);
3660 copy_string->declaration = string->declaration;
3661 end:
3662 return copy;
3663 }
3664
3665 static
3666 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *type_a,
3667 struct bt_ctf_field_type *type_b)
3668 {
3669 int ret = 1;
3670 struct bt_ctf_field_type_integer *integer_a;
3671 struct bt_ctf_field_type_integer *integer_b;
3672 struct declaration_integer *decl_a;
3673 struct declaration_integer *decl_b;
3674
3675 integer_a = container_of(type_a, struct bt_ctf_field_type_integer,
3676 parent);
3677 integer_b = container_of(type_b, struct bt_ctf_field_type_integer,
3678 parent);
3679 decl_a = &integer_a->declaration;
3680 decl_b = &integer_b->declaration;
3681
3682 /* Length */
3683 if (decl_a->len != decl_b->len) {
3684 goto end;
3685 }
3686
3687 /*
3688 * Compare user byte orders only, not the cached,
3689 * real byte orders.
3690 */
3691 if (integer_a->user_byte_order != integer_b->user_byte_order) {
3692 goto end;
3693 }
3694
3695 /* Signedness */
3696 if (decl_a->signedness != decl_b->signedness) {
3697 goto end;
3698 }
3699
3700 /* Base */
3701 if (decl_a->base != decl_b->base) {
3702 goto end;
3703 }
3704
3705 /* Encoding */
3706 if (decl_a->encoding != decl_b->encoding) {
3707 goto end;
3708 }
3709
3710 /* Mapped clock */
3711 if (integer_a->mapped_clock != integer_b->mapped_clock) {
3712 goto end;
3713 }
3714
3715 /* Equal */
3716 ret = 0;
3717
3718 end:
3719 return ret;
3720 }
3721
3722 static
3723 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *type_a,
3724 struct bt_ctf_field_type *type_b)
3725 {
3726 int ret = 1;
3727 struct bt_ctf_field_type_floating_point *float_a;
3728 struct bt_ctf_field_type_floating_point *float_b;
3729
3730 float_a = container_of(type_a,
3731 struct bt_ctf_field_type_floating_point, parent);
3732 float_b = container_of(type_b,
3733 struct bt_ctf_field_type_floating_point, parent);
3734
3735 /* Sign length */
3736 if (float_a->sign.len != float_b->sign.len) {
3737 goto end;
3738 }
3739
3740 /* Exponent length */
3741 if (float_a->exp.len != float_b->exp.len) {
3742 goto end;
3743 }
3744
3745 /* Mantissa length */
3746 if (float_a->mantissa.len != float_b->mantissa.len) {
3747 goto end;
3748 }
3749
3750 /*
3751 * Compare user byte orders only, not the cached,
3752 * real byte orders.
3753 */
3754 if (float_a->user_byte_order != float_b->user_byte_order) {
3755 goto end;
3756 }
3757
3758 /* Equal */
3759 ret = 0;
3760
3761 end:
3762 return ret;
3763 }
3764
3765 static
3766 int compare_enumeration_mappings(struct enumeration_mapping *mapping_a,
3767 struct enumeration_mapping *mapping_b)
3768 {
3769 int ret = 1;
3770
3771 /* Label */
3772 if (mapping_a->string != mapping_b->string) {
3773 goto end;
3774 }
3775
3776 /* Range start */
3777 if (mapping_a->range_start._unsigned !=
3778 mapping_b->range_start._unsigned) {
3779 goto end;
3780 }
3781
3782 /* Range end */
3783 if (mapping_a->range_end._unsigned !=
3784 mapping_b->range_end._unsigned) {
3785 goto end;
3786 }
3787
3788 /* Equal */
3789 ret = 0;
3790
3791 end:
3792 return ret;
3793 }
3794
3795 static
3796 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *type_a,
3797 struct bt_ctf_field_type *type_b)
3798 {
3799 int ret = 1;
3800 int i;
3801 struct bt_ctf_field_type_enumeration *enum_a;
3802 struct bt_ctf_field_type_enumeration *enum_b;
3803
3804 enum_a = container_of(type_a,
3805 struct bt_ctf_field_type_enumeration, parent);
3806 enum_b = container_of(type_b,
3807 struct bt_ctf_field_type_enumeration, parent);
3808
3809 /* Container field type */
3810 ret = bt_ctf_field_type_compare(enum_a->container, enum_b->container);
3811 if (ret) {
3812 goto end;
3813 }
3814
3815 ret = 1;
3816
3817 /* Entries */
3818 if (enum_a->entries->len != enum_b->entries->len) {
3819 goto end;
3820 }
3821
3822 for (i = 0; i < enum_a->entries->len; ++i) {
3823 struct enumeration_mapping *mapping_a =
3824 g_ptr_array_index(enum_a->entries, i);
3825 struct enumeration_mapping *mapping_b =
3826 g_ptr_array_index(enum_b->entries, i);
3827
3828 if (compare_enumeration_mappings(mapping_a, mapping_b)) {
3829 goto end;
3830 }
3831 }
3832
3833 /* Equal */
3834 ret = 0;
3835
3836 end:
3837 return ret;
3838 }
3839
3840 static
3841 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *type_a,
3842 struct bt_ctf_field_type *type_b)
3843 {
3844 int ret = 1;
3845 struct bt_ctf_field_type_string *string_a;
3846 struct bt_ctf_field_type_string *string_b;
3847
3848 string_a = container_of(type_a,
3849 struct bt_ctf_field_type_string, parent);
3850 string_b = container_of(type_b,
3851 struct bt_ctf_field_type_string, parent);
3852
3853 /* Encoding */
3854 if (string_a->declaration.encoding != string_b->declaration.encoding) {
3855 goto end;
3856 }
3857
3858 /* Equal */
3859 ret = 0;
3860
3861 end:
3862 return ret;
3863 }
3864
3865 static
3866 int compare_structure_fields(struct structure_field *field_a,
3867 struct structure_field *field_b)
3868 {
3869 int ret = 1;
3870
3871 /* Label */
3872 if (field_a->name != field_b->name) {
3873 goto end;
3874 }
3875
3876 /* Type */
3877 ret = bt_ctf_field_type_compare(field_a->type, field_b->type);
3878
3879 end:
3880 return ret;
3881 }
3882
3883 static
3884 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *type_a,
3885 struct bt_ctf_field_type *type_b)
3886 {
3887 int ret = 1;
3888 int i;
3889 struct bt_ctf_field_type_structure *struct_a;
3890 struct bt_ctf_field_type_structure *struct_b;
3891
3892 struct_a = container_of(type_a,
3893 struct bt_ctf_field_type_structure, parent);
3894 struct_b = container_of(type_b,
3895 struct bt_ctf_field_type_structure, parent);
3896
3897 /* Alignment */
3898 if (bt_ctf_field_type_get_alignment(type_a) !=
3899 bt_ctf_field_type_get_alignment(type_b)) {
3900 goto end;
3901 }
3902
3903 /* Fields */
3904 if (struct_a->fields->len != struct_b->fields->len) {
3905 goto end;
3906 }
3907
3908 for (i = 0; i < struct_a->fields->len; ++i) {
3909 struct structure_field *field_a =
3910 g_ptr_array_index(struct_a->fields, i);
3911 struct structure_field *field_b =
3912 g_ptr_array_index(struct_b->fields, i);
3913
3914 ret = compare_structure_fields(field_a, field_b);
3915 if (ret) {
3916 goto end;
3917 }
3918 }
3919
3920 /* Equal */
3921 ret = 0;
3922
3923 end:
3924 return ret;
3925 }
3926
3927 static
3928 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *type_a,
3929 struct bt_ctf_field_type *type_b)
3930 {
3931 int ret = 1;
3932 int i;
3933 struct bt_ctf_field_type_variant *variant_a;
3934 struct bt_ctf_field_type_variant *variant_b;
3935
3936 variant_a = container_of(type_a,
3937 struct bt_ctf_field_type_variant, parent);
3938 variant_b = container_of(type_b,
3939 struct bt_ctf_field_type_variant, parent);
3940
3941 /* Tag name */
3942 if (strcmp(variant_a->tag_name->str, variant_b->tag_name->str)) {
3943 goto end;
3944 }
3945
3946 /* Tag type */
3947 ret = bt_ctf_field_type_compare(
3948 (struct bt_ctf_field_type *) variant_a->tag,
3949 (struct bt_ctf_field_type *) variant_b->tag);
3950 if (ret) {
3951 goto end;
3952 }
3953
3954 ret = 1;
3955
3956 /* Fields */
3957 if (variant_a->fields->len != variant_b->fields->len) {
3958 goto end;
3959 }
3960
3961 for (i = 0; i < variant_a->fields->len; ++i) {
3962 struct structure_field *field_a =
3963 g_ptr_array_index(variant_a->fields, i);
3964 struct structure_field *field_b =
3965 g_ptr_array_index(variant_b->fields, i);
3966
3967 ret = compare_structure_fields(field_a, field_b);
3968 if (ret) {
3969 goto end;
3970 }
3971 }
3972
3973 /* Equal */
3974 ret = 0;
3975
3976 end:
3977 return ret;
3978 }
3979
3980 static
3981 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *type_a,
3982 struct bt_ctf_field_type *type_b)
3983 {
3984 int ret = 1;
3985 struct bt_ctf_field_type_array *array_a;
3986 struct bt_ctf_field_type_array *array_b;
3987
3988 array_a = container_of(type_a,
3989 struct bt_ctf_field_type_array, parent);
3990 array_b = container_of(type_b,
3991 struct bt_ctf_field_type_array, parent);
3992
3993 /* Length */
3994 if (array_a->length != array_b->length) {
3995 goto end;
3996 }
3997
3998 /* Element type */
3999 ret = bt_ctf_field_type_compare(array_a->element_type,
4000 array_b->element_type);
4001
4002 end:
4003 return ret;
4004 }
4005
4006 static
4007 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *type_a,
4008 struct bt_ctf_field_type *type_b)
4009 {
4010 int ret = -1;
4011 struct bt_ctf_field_type_sequence *sequence_a;
4012 struct bt_ctf_field_type_sequence *sequence_b;
4013
4014 sequence_a = container_of(type_a,
4015 struct bt_ctf_field_type_sequence, parent);
4016 sequence_b = container_of(type_b,
4017 struct bt_ctf_field_type_sequence, parent);
4018
4019 /* Length name */
4020 if (strcmp(sequence_a->length_field_name->str,
4021 sequence_b->length_field_name->str)) {
4022 goto end;
4023 }
4024
4025 /* Element type */
4026 ret = bt_ctf_field_type_compare(sequence_a->element_type,
4027 sequence_b->element_type);
4028
4029 end:
4030 return ret;
4031 }
4032
4033 BT_HIDDEN
4034 int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a,
4035 struct bt_ctf_field_type *type_b)
4036 {
4037 int ret = 1;
4038
4039 if (type_a == type_b) {
4040 /* Same reference: equal (even if both are NULL) */
4041 ret = 0;
4042 goto end;
4043 }
4044
4045 if (!type_a || !type_b) {
4046 ret = -1;
4047 goto end;
4048 }
4049
4050 if (type_a->declaration->id != type_b->declaration->id) {
4051 /* Different type IDs */
4052 goto end;
4053 }
4054
4055 if (type_a->declaration->id == CTF_TYPE_UNKNOWN) {
4056 /* Both have unknown type IDs */
4057 goto end;
4058 }
4059
4060 ret = type_compare_funcs[type_a->declaration->id](type_a, type_b);
4061
4062 end:
4063 return ret;
4064 }
4065
4066 BT_HIDDEN
4067 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *field_type)
4068 {
4069 int field_count = -1;
4070 enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type);
4071
4072 switch (type_id) {
4073 case CTF_TYPE_STRUCT:
4074 field_count =
4075 bt_ctf_field_type_structure_get_field_count(field_type);
4076 break;
4077 case CTF_TYPE_VARIANT:
4078 field_count =
4079 bt_ctf_field_type_variant_get_field_count(field_type);
4080 break;
4081 case CTF_TYPE_ARRAY:
4082 case CTF_TYPE_SEQUENCE:
4083 /*
4084 * Array and sequence types always contain a single member
4085 * (the element type).
4086 */
4087 field_count = 1;
4088 break;
4089 default:
4090 break;
4091 }
4092
4093 return field_count;
4094 }
4095
4096 BT_HIDDEN
4097 struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
4098 struct bt_ctf_field_type *field_type, int index)
4099 {
4100 struct bt_ctf_field_type *field = NULL;
4101 enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type);
4102
4103 switch (type_id) {
4104 case CTF_TYPE_STRUCT:
4105 bt_ctf_field_type_structure_get_field(field_type, NULL, &field,
4106 index);
4107 break;
4108 case CTF_TYPE_VARIANT:
4109 {
4110 int ret = bt_ctf_field_type_variant_get_field(field_type, NULL,
4111 &field, index);
4112 if (ret) {
4113 field = NULL;
4114 goto end;
4115 }
4116 break;
4117 }
4118 case CTF_TYPE_ARRAY:
4119 field = bt_ctf_field_type_array_get_element_type(field_type);
4120 break;
4121 case CTF_TYPE_SEQUENCE:
4122 field = bt_ctf_field_type_sequence_get_element_type(field_type);
4123 break;
4124 default:
4125 break;
4126 }
4127 end:
4128 return field;
4129 }
4130
4131 BT_HIDDEN
4132 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *field_type,
4133 const char *name)
4134 {
4135 int field_index = -1;
4136 enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type);
4137
4138 switch (type_id) {
4139 case CTF_TYPE_STRUCT:
4140 field_index = bt_ctf_field_type_structure_get_field_name_index(
4141 field_type, name);
4142 break;
4143 case CTF_TYPE_VARIANT:
4144 field_index = bt_ctf_field_type_variant_get_field_name_index(
4145 field_type, name);
4146 break;
4147 default:
4148 break;
4149 }
4150
4151 return field_index;
4152 }
4153
4154 BT_HIDDEN
4155 struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path(
4156 struct bt_ctf_field_type *type)
4157 {
4158 struct bt_ctf_field_path *field_path = NULL;
4159 struct bt_ctf_field_type_variant *variant;
4160
4161 if (!type || !bt_ctf_field_type_is_variant(type)) {
4162 goto end;
4163 }
4164
4165 variant = container_of(type, struct bt_ctf_field_type_variant,
4166 parent);
4167 field_path = bt_get(variant->tag_field_path);
4168 end:
4169 return field_path;
4170 }
4171
4172 BT_HIDDEN
4173 struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path(
4174 struct bt_ctf_field_type *type)
4175 {
4176 struct bt_ctf_field_path *field_path = NULL;
4177 struct bt_ctf_field_type_sequence *sequence;
4178
4179 if (!type || !bt_ctf_field_type_is_sequence(type)) {
4180 goto end;
4181 }
4182
4183 sequence = container_of(type, struct bt_ctf_field_type_sequence,
4184 parent);
4185 field_path = bt_get(sequence->length_field_path);
4186 end:
4187 return field_path;
4188 }
This page took 0.1615 seconds and 4 git commands to generate.