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