ir: add bt_ctf_field_variant_get_current_field()
[babeltrace.git] / formats / ctf / ir / event-fields.c
1 /*
2 * event-fields.c
3 *
4 * Babeltrace CTF IR - Event Fields
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-fields.h>
30 #include <babeltrace/ctf-ir/event-fields-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/compiler.h>
33
34 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
35
36 static
37 struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *);
38 static
39 struct bt_ctf_field *bt_ctf_field_enumeration_create(
40 struct bt_ctf_field_type *);
41 static
42 struct bt_ctf_field *bt_ctf_field_floating_point_create(
43 struct bt_ctf_field_type *);
44 static
45 struct bt_ctf_field *bt_ctf_field_structure_create(
46 struct bt_ctf_field_type *);
47 static
48 struct bt_ctf_field *bt_ctf_field_variant_create(
49 struct bt_ctf_field_type *);
50 static
51 struct bt_ctf_field *bt_ctf_field_array_create(
52 struct bt_ctf_field_type *);
53 static
54 struct bt_ctf_field *bt_ctf_field_sequence_create(
55 struct bt_ctf_field_type *);
56 static
57 struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *);
58
59 static
60 void bt_ctf_field_destroy(struct bt_ctf_ref *);
61 static
62 void bt_ctf_field_integer_destroy(struct bt_ctf_field *);
63 static
64 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *);
65 static
66 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *);
67 static
68 void bt_ctf_field_structure_destroy(struct bt_ctf_field *);
69 static
70 void bt_ctf_field_variant_destroy(struct bt_ctf_field *);
71 static
72 void bt_ctf_field_array_destroy(struct bt_ctf_field *);
73 static
74 void bt_ctf_field_sequence_destroy(struct bt_ctf_field *);
75 static
76 void bt_ctf_field_string_destroy(struct bt_ctf_field *);
77
78 static
79 int bt_ctf_field_generic_validate(struct bt_ctf_field *);
80 static
81 int bt_ctf_field_structure_validate(struct bt_ctf_field *);
82 static
83 int bt_ctf_field_variant_validate(struct bt_ctf_field *);
84 static
85 int bt_ctf_field_enumeration_validate(struct bt_ctf_field *);
86 static
87 int bt_ctf_field_array_validate(struct bt_ctf_field *);
88 static
89 int bt_ctf_field_sequence_validate(struct bt_ctf_field *);
90
91 static
92 int bt_ctf_field_generic_reset(struct bt_ctf_field *);
93 static
94 int bt_ctf_field_structure_reset(struct bt_ctf_field *);
95 static
96 int bt_ctf_field_variant_reset(struct bt_ctf_field *);
97 static
98 int bt_ctf_field_enumeration_reset(struct bt_ctf_field *);
99 static
100 int bt_ctf_field_array_reset(struct bt_ctf_field *);
101 static
102 int bt_ctf_field_sequence_reset(struct bt_ctf_field *);
103 static
104 int bt_ctf_field_string_reset(struct bt_ctf_field *);
105
106 static
107 int bt_ctf_field_integer_serialize(struct bt_ctf_field *,
108 struct ctf_stream_pos *);
109 static
110 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *,
111 struct ctf_stream_pos *);
112 static
113 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *,
114 struct ctf_stream_pos *);
115 static
116 int bt_ctf_field_structure_serialize(struct bt_ctf_field *,
117 struct ctf_stream_pos *);
118 static
119 int bt_ctf_field_variant_serialize(struct bt_ctf_field *,
120 struct ctf_stream_pos *);
121 static
122 int bt_ctf_field_array_serialize(struct bt_ctf_field *,
123 struct ctf_stream_pos *);
124 static
125 int bt_ctf_field_sequence_serialize(struct bt_ctf_field *,
126 struct ctf_stream_pos *);
127 static
128 int bt_ctf_field_string_serialize(struct bt_ctf_field *,
129 struct ctf_stream_pos *);
130
131 static
132 int bt_ctf_field_integer_copy(struct bt_ctf_field *, struct bt_ctf_field *);
133 static
134 int bt_ctf_field_enumeration_copy(struct bt_ctf_field *, struct bt_ctf_field *);
135 static
136 int bt_ctf_field_floating_point_copy(struct bt_ctf_field *,
137 struct bt_ctf_field *);
138 static
139 int bt_ctf_field_structure_copy(struct bt_ctf_field *, struct bt_ctf_field *);
140 static
141 int bt_ctf_field_variant_copy(struct bt_ctf_field *, struct bt_ctf_field *);
142 static
143 int bt_ctf_field_array_copy(struct bt_ctf_field *, struct bt_ctf_field *);
144 static
145 int bt_ctf_field_sequence_copy(struct bt_ctf_field *, struct bt_ctf_field *);
146 static
147 int bt_ctf_field_string_copy(struct bt_ctf_field *, struct bt_ctf_field *);
148
149 static
150 int increase_packet_size(struct ctf_stream_pos *pos);
151
152 static
153 struct bt_ctf_field *(* const field_create_funcs[])(
154 struct bt_ctf_field_type *) = {
155 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
156 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
157 [CTF_TYPE_FLOAT] =
158 bt_ctf_field_floating_point_create,
159 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create,
160 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create,
161 [CTF_TYPE_ARRAY] = bt_ctf_field_array_create,
162 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create,
163 [CTF_TYPE_STRING] = bt_ctf_field_string_create,
164 };
165
166 static
167 void (* const field_destroy_funcs[])(struct bt_ctf_field *) = {
168 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
169 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
170 [CTF_TYPE_FLOAT] =
171 bt_ctf_field_floating_point_destroy,
172 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy,
173 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy,
174 [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy,
175 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy,
176 [CTF_TYPE_STRING] = bt_ctf_field_string_destroy,
177 };
178
179 static
180 int (* const field_validate_funcs[])(struct bt_ctf_field *) = {
181 [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
182 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
183 [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
184 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate,
185 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate,
186 [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate,
187 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate,
188 [CTF_TYPE_STRING] = bt_ctf_field_generic_validate,
189 };
190
191 static
192 int (* const field_reset_funcs[])(struct bt_ctf_field *) = {
193 [CTF_TYPE_INTEGER] = bt_ctf_field_generic_reset,
194 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_reset,
195 [CTF_TYPE_FLOAT] = bt_ctf_field_generic_reset,
196 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_reset,
197 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_reset,
198 [CTF_TYPE_ARRAY] = bt_ctf_field_array_reset,
199 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_reset,
200 [CTF_TYPE_STRING] = bt_ctf_field_string_reset,
201 };
202
203 static
204 int (* const field_serialize_funcs[])(struct bt_ctf_field *,
205 struct ctf_stream_pos *) = {
206 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
207 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
208 [CTF_TYPE_FLOAT] =
209 bt_ctf_field_floating_point_serialize,
210 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize,
211 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize,
212 [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize,
213 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize,
214 [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
215 };
216
217 static
218 int (* const field_copy_funcs[])(struct bt_ctf_field *,
219 struct bt_ctf_field *) = {
220 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_copy,
221 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_copy,
222 [CTF_TYPE_FLOAT] = bt_ctf_field_floating_point_copy,
223 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_copy,
224 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_copy,
225 [CTF_TYPE_ARRAY] = bt_ctf_field_array_copy,
226 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_copy,
227 [CTF_TYPE_STRING] = bt_ctf_field_string_copy,
228 };
229
230 struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
231 {
232 struct bt_ctf_field *field = NULL;
233 enum ctf_type_id type_id;
234
235 if (!type) {
236 goto error;
237 }
238
239 type_id = bt_ctf_field_type_get_type_id(type);
240 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES ||
241 bt_ctf_field_type_validate(type)) {
242 goto error;
243 }
244
245 field = field_create_funcs[type_id](type);
246 if (!field) {
247 goto error;
248 }
249
250 /* The type's declaration can't change after this point */
251 bt_ctf_field_type_freeze(type);
252 bt_ctf_field_type_get(type);
253 bt_ctf_ref_init(&field->ref_count);
254 field->type = type;
255 error:
256 return field;
257 }
258
259 void bt_ctf_field_get(struct bt_ctf_field *field)
260 {
261 if (field) {
262 bt_ctf_ref_get(&field->ref_count);
263 }
264 }
265
266 void bt_ctf_field_put(struct bt_ctf_field *field)
267 {
268 if (field) {
269 bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
270 }
271 }
272
273 struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field)
274 {
275 struct bt_ctf_field_type *ret = NULL;
276
277 if (!field) {
278 goto end;
279 }
280
281 ret = field->type;
282 bt_ctf_field_type_get(ret);
283 end:
284 return ret;
285 }
286
287 struct bt_ctf_field *bt_ctf_field_sequence_get_length(
288 struct bt_ctf_field *field)
289 {
290 struct bt_ctf_field *ret = NULL;
291 struct bt_ctf_field_sequence *sequence;
292
293 if (!field) {
294 goto end;
295 }
296
297 if (bt_ctf_field_type_get_type_id(field->type) !=
298 CTF_TYPE_SEQUENCE) {
299 goto end;
300 }
301
302 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
303 ret = sequence->length;
304 bt_ctf_field_get(ret);
305 end:
306 return ret;
307 }
308
309 int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
310 struct bt_ctf_field *length_field)
311 {
312 int ret = 0;
313 struct bt_ctf_field_type_integer *length_type;
314 struct bt_ctf_field_integer *length;
315 struct bt_ctf_field_sequence *sequence;
316 uint64_t sequence_length;
317
318 if (!field || !length_field) {
319 ret = -1;
320 goto end;
321 }
322 if (bt_ctf_field_type_get_type_id(length_field->type) !=
323 CTF_TYPE_INTEGER) {
324 ret = -1;
325 goto end;
326 }
327
328 length_type = container_of(length_field->type,
329 struct bt_ctf_field_type_integer, parent);
330 /* The length field must be unsigned */
331 if (length_type->declaration.signedness) {
332 ret = -1;
333 goto end;
334 }
335
336 length = container_of(length_field, struct bt_ctf_field_integer,
337 parent);
338 sequence_length = length->definition.value._unsigned;
339 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
340 if (sequence->elements) {
341 g_ptr_array_free(sequence->elements, TRUE);
342 bt_ctf_field_put(sequence->length);
343 }
344
345 sequence->elements = g_ptr_array_sized_new((size_t)sequence_length);
346 if (!sequence->elements) {
347 ret = -1;
348 goto end;
349 }
350
351 g_ptr_array_set_free_func(sequence->elements,
352 (GDestroyNotify)bt_ctf_field_put);
353 g_ptr_array_set_size(sequence->elements, (size_t)sequence_length);
354 bt_ctf_field_get(length_field);
355 sequence->length = length_field;
356 end:
357 return ret;
358 }
359
360 struct bt_ctf_field *bt_ctf_field_structure_get_field(
361 struct bt_ctf_field *field, const char *name)
362 {
363 struct bt_ctf_field *new_field = NULL;
364 GQuark field_quark;
365 struct bt_ctf_field_structure *structure;
366 struct bt_ctf_field_type *field_type = NULL;
367 size_t index;
368
369 if (!field || !name ||
370 bt_ctf_field_type_get_type_id(field->type) !=
371 CTF_TYPE_STRUCT) {
372 goto error;
373 }
374
375 field_quark = g_quark_from_string(name);
376 structure = container_of(field, struct bt_ctf_field_structure, parent);
377 field_type =
378 bt_ctf_field_type_structure_get_field_type_by_name(field->type,
379 name);
380 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
381 GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) {
382 goto error;
383 }
384
385 if (structure->fields->pdata[index]) {
386 new_field = structure->fields->pdata[index];
387 goto end;
388 }
389
390 new_field = bt_ctf_field_create(field_type);
391 if (!new_field) {
392 goto error;
393 }
394
395 structure->fields->pdata[index] = new_field;
396 end:
397 bt_ctf_field_get(new_field);
398 error:
399 if (field_type) {
400 bt_ctf_field_type_put(field_type);
401 }
402 return new_field;
403 }
404
405 struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
406 struct bt_ctf_field *field, int index)
407 {
408 int ret;
409 const char *field_name;
410 struct bt_ctf_field_structure *structure;
411 struct bt_ctf_field_type *structure_type;
412 struct bt_ctf_field_type *field_type = NULL;
413 struct bt_ctf_field *ret_field = NULL;
414
415 if (!field ||
416 bt_ctf_field_type_get_type_id(field->type) != CTF_TYPE_STRUCT) {
417 goto end;
418 }
419
420 structure = container_of(field, struct bt_ctf_field_structure, parent);
421 if (index >= structure->fields->len) {
422 goto error;
423 }
424
425 ret_field = structure->fields->pdata[index];
426 if (ret_field) {
427 goto end;
428 }
429
430 /* Field has not been instanciated yet, create it */
431 structure_type = bt_ctf_field_get_type(field);
432 if (!structure_type) {
433 goto error;
434 }
435
436 ret = bt_ctf_field_type_structure_get_field(structure_type,
437 &field_name, &field_type, index);
438 bt_ctf_field_type_put(structure_type);
439 if (ret) {
440 goto error;
441 }
442
443 ret_field = bt_ctf_field_create(field_type);
444 if (!ret_field) {
445 goto error;
446 }
447
448 structure->fields->pdata[index] = ret_field;
449 end:
450 bt_ctf_field_get(ret_field);
451 error:
452 if (field_type) {
453 bt_ctf_field_type_put(field_type);
454 }
455 return ret_field;
456 }
457
458 BT_HIDDEN
459 int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
460 const char *name, struct bt_ctf_field *value)
461 {
462 int ret = 0;
463 GQuark field_quark;
464 struct bt_ctf_field_structure *structure;
465 struct bt_ctf_field_type *expected_field_type = NULL;
466 size_t index;
467
468 if (!field || !name || !value ||
469 bt_ctf_field_type_get_type_id(field->type) !=
470 CTF_TYPE_STRUCT) {
471 ret = -1;
472 goto end;
473 }
474
475 field_quark = g_quark_from_string(name);
476 structure = container_of(field, struct bt_ctf_field_structure, parent);
477 expected_field_type =
478 bt_ctf_field_type_structure_get_field_type_by_name(field->type,
479 name);
480 if (expected_field_type != value->type) {
481 ret = -1;
482 goto end;
483 }
484
485 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
486 GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) {
487 goto end;
488 }
489
490 if (structure->fields->pdata[index]) {
491 bt_ctf_field_put(structure->fields->pdata[index]);
492 }
493
494 structure->fields->pdata[index] = value;
495 bt_ctf_field_get(value);
496 end:
497 if (expected_field_type) {
498 bt_ctf_field_type_put(expected_field_type);
499 }
500 return ret;
501 }
502
503 struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
504 uint64_t index)
505 {
506 struct bt_ctf_field *new_field = NULL;
507 struct bt_ctf_field_type *field_type = NULL;
508 struct bt_ctf_field_array *array;
509
510 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
511 CTF_TYPE_ARRAY) {
512 goto end;
513 }
514
515 array = container_of(field, struct bt_ctf_field_array, parent);
516 if (index >= array->elements->len) {
517 goto end;
518 }
519
520 field_type = bt_ctf_field_type_array_get_element_type(field->type);
521 if (array->elements->pdata[(size_t)index]) {
522 new_field = array->elements->pdata[(size_t)index];
523 goto end;
524 }
525
526 new_field = bt_ctf_field_create(field_type);
527 array->elements->pdata[(size_t)index] = new_field;
528 end:
529 if (field_type) {
530 bt_ctf_field_type_put(field_type);
531 }
532 if (new_field) {
533 bt_ctf_field_get(new_field);
534 }
535 return new_field;
536 }
537
538 struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
539 uint64_t index)
540 {
541 struct bt_ctf_field *new_field = NULL;
542 struct bt_ctf_field_type *field_type = NULL;
543 struct bt_ctf_field_sequence *sequence;
544
545 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
546 CTF_TYPE_SEQUENCE) {
547 goto end;
548 }
549
550 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
551 if (!sequence->elements || sequence->elements->len <= index) {
552 goto end;
553 }
554
555 field_type = bt_ctf_field_type_sequence_get_element_type(field->type);
556 if (sequence->elements->pdata[(size_t)index]) {
557 new_field = sequence->elements->pdata[(size_t)index];
558 goto end;
559 }
560
561 new_field = bt_ctf_field_create(field_type);
562 sequence->elements->pdata[(size_t)index] = new_field;
563 end:
564 if (field_type) {
565 bt_ctf_field_type_put(field_type);
566 }
567 if (new_field) {
568 bt_ctf_field_get(new_field);
569 }
570 return new_field;
571 }
572
573 struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
574 struct bt_ctf_field *tag_field)
575 {
576 struct bt_ctf_field *new_field = NULL;
577 struct bt_ctf_field_variant *variant;
578 struct bt_ctf_field_type_variant *variant_type;
579 struct bt_ctf_field_type *field_type;
580 struct bt_ctf_field *tag_enum = NULL;
581 struct bt_ctf_field_integer *tag_enum_integer;
582 int64_t tag_enum_value;
583
584 if (!field || !tag_field ||
585 bt_ctf_field_type_get_type_id(field->type) !=
586 CTF_TYPE_VARIANT ||
587 bt_ctf_field_type_get_type_id(tag_field->type) !=
588 CTF_TYPE_ENUM) {
589 goto end;
590 }
591
592 variant = container_of(field, struct bt_ctf_field_variant, parent);
593 variant_type = container_of(field->type,
594 struct bt_ctf_field_type_variant, parent);
595 tag_enum = bt_ctf_field_enumeration_get_container(tag_field);
596 if (!tag_enum) {
597 goto end;
598 }
599
600 tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer,
601 parent);
602
603 if (bt_ctf_field_validate(tag_field) < 0) {
604 goto end;
605 }
606
607 tag_enum_value = tag_enum_integer->definition.value._signed;
608
609 /*
610 * If the variant currently has a tag and a payload, and if the
611 * requested tag value is the same as the current one, return
612 * the current payload instead of creating a fresh one.
613 */
614 if (variant->tag && variant->payload) {
615 struct bt_ctf_field *cur_tag_container = NULL;
616 struct bt_ctf_field_integer *cur_tag_enum_integer;
617 int64_t cur_tag_value;
618
619 cur_tag_container =
620 bt_ctf_field_enumeration_get_container(variant->tag);
621 cur_tag_enum_integer = container_of(cur_tag_container,
622 struct bt_ctf_field_integer, parent);
623 bt_ctf_field_put(cur_tag_container);
624 cur_tag_value = cur_tag_enum_integer->definition.value._signed;
625
626 if (cur_tag_value == tag_enum_value) {
627 new_field = variant->payload;
628 bt_ctf_field_get(new_field);
629 goto end;
630 }
631 }
632
633 field_type = bt_ctf_field_type_variant_get_field_type_signed(
634 variant_type, tag_enum_value);
635 if (!field_type) {
636 goto end;
637 }
638
639 new_field = bt_ctf_field_create(field_type);
640 if (!new_field) {
641 goto end;
642 }
643
644 bt_ctf_field_put(variant->tag);
645 bt_ctf_field_put(variant->payload);
646 bt_ctf_field_get(new_field);
647 bt_ctf_field_get(tag_field);
648 variant->tag = tag_field;
649 variant->payload = new_field;
650 end:
651 bt_ctf_field_put(tag_enum);
652 return new_field;
653 }
654
655 struct bt_ctf_field *bt_ctf_field_variant_get_current_field(
656 struct bt_ctf_field *variant_field)
657 {
658 struct bt_ctf_field *current_field = NULL;
659 struct bt_ctf_field_variant *variant;
660
661 if (!variant_field ||
662 bt_ctf_field_type_get_type_id(variant_field->type) !=
663 CTF_TYPE_VARIANT) {
664 goto end;
665 }
666
667 variant = container_of(variant_field, struct bt_ctf_field_variant,
668 parent);
669
670 if (variant->payload) {
671 current_field = variant->payload;
672 bt_ctf_field_get(current_field);
673 goto end;
674 }
675
676 end:
677 return current_field;
678 }
679
680 struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
681 struct bt_ctf_field *field)
682 {
683 struct bt_ctf_field *container = NULL;
684 struct bt_ctf_field_enumeration *enumeration;
685
686 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
687 CTF_TYPE_ENUM) {
688 goto end;
689 }
690
691 enumeration = container_of(field, struct bt_ctf_field_enumeration,
692 parent);
693 if (!enumeration->payload) {
694 struct bt_ctf_field_type_enumeration *enumeration_type =
695 container_of(field->type,
696 struct bt_ctf_field_type_enumeration, parent);
697 enumeration->payload =
698 bt_ctf_field_create(enumeration_type->container);
699 }
700
701 container = enumeration->payload;
702 bt_ctf_field_get(container);
703 end:
704 return container;
705 }
706
707 const char *bt_ctf_field_enumeration_get_mapping_name(
708 struct bt_ctf_field *field)
709 {
710 int ret;
711 const char *name = NULL;
712 struct bt_ctf_field *container = NULL;
713 struct bt_ctf_field_type *container_type = NULL;
714 struct bt_ctf_field_type_integer *integer_type = NULL;
715 struct bt_ctf_field_type_enumeration *enumeration_type = NULL;
716
717 container = bt_ctf_field_enumeration_get_container(field);
718 if (!container) {
719 goto end;
720 }
721
722 container_type = bt_ctf_field_get_type(container);
723 if (!container_type) {
724 goto error_put_container;
725 }
726
727 integer_type = container_of(container_type,
728 struct bt_ctf_field_type_integer, parent);
729 enumeration_type = container_of(field->type,
730 struct bt_ctf_field_type_enumeration, parent);
731
732 if (!integer_type->declaration.signedness) {
733 uint64_t value;
734 ret = bt_ctf_field_unsigned_integer_get_value(container,
735 &value);
736 if (ret) {
737 goto error_put_container_type;
738 }
739
740 name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
741 enumeration_type, value);
742 } else {
743 int64_t value;
744 ret = bt_ctf_field_signed_integer_get_value(container,
745 &value);
746 if (ret) {
747 goto error_put_container_type;
748 }
749
750 name = bt_ctf_field_type_enumeration_get_mapping_name_signed(
751 enumeration_type, value);
752 }
753
754 error_put_container_type:
755 bt_ctf_field_type_put(container_type);
756 error_put_container:
757 bt_ctf_field_put(container);
758 end:
759 return name;
760 }
761
762 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field,
763 int64_t *value)
764 {
765 int ret = 0;
766 struct bt_ctf_field_integer *integer;
767 struct bt_ctf_field_type_integer *integer_type;
768
769 if (!field || !value || !field->payload_set ||
770 bt_ctf_field_type_get_type_id(field->type) !=
771 CTF_TYPE_INTEGER) {
772 ret = -1;
773 goto end;
774 }
775
776 integer_type = container_of(field->type,
777 struct bt_ctf_field_type_integer, parent);
778 if (!integer_type->declaration.signedness) {
779 ret = -1;
780 goto end;
781 }
782
783 integer = container_of(field,
784 struct bt_ctf_field_integer, parent);
785 *value = integer->definition.value._signed;
786 end:
787 return ret;
788 }
789
790 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
791 int64_t value)
792 {
793 int ret = 0;
794 struct bt_ctf_field_integer *integer;
795 struct bt_ctf_field_type_integer *integer_type;
796 unsigned int size;
797 int64_t min_value, max_value;
798
799 if (!field ||
800 bt_ctf_field_type_get_type_id(field->type) !=
801 CTF_TYPE_INTEGER) {
802 ret = -1;
803 goto end;
804 }
805
806 integer = container_of(field, struct bt_ctf_field_integer, parent);
807 integer_type = container_of(field->type,
808 struct bt_ctf_field_type_integer, parent);
809 if (!integer_type->declaration.signedness) {
810 ret = -1;
811 goto end;
812 }
813
814 size = integer_type->declaration.len;
815 min_value = -((int64_t)1 << (size - 1));
816 max_value = ((int64_t)1 << (size - 1)) - 1;
817 if (value < min_value || value > max_value) {
818 ret = -1;
819 goto end;
820 }
821
822 integer->definition.value._signed = value;
823 integer->parent.payload_set = 1;
824 end:
825 return ret;
826 }
827
828 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field,
829 uint64_t *value)
830 {
831 int ret = 0;
832 struct bt_ctf_field_integer *integer;
833 struct bt_ctf_field_type_integer *integer_type;
834
835 if (!field || !value || !field->payload_set ||
836 bt_ctf_field_type_get_type_id(field->type) !=
837 CTF_TYPE_INTEGER) {
838 ret = -1;
839 goto end;
840 }
841
842 integer_type = container_of(field->type,
843 struct bt_ctf_field_type_integer, parent);
844 if (integer_type->declaration.signedness) {
845 ret = -1;
846 goto end;
847 }
848
849 integer = container_of(field,
850 struct bt_ctf_field_integer, parent);
851 *value = integer->definition.value._unsigned;
852 end:
853 return ret;
854 }
855
856 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
857 uint64_t value)
858 {
859 int ret = 0;
860 struct bt_ctf_field_integer *integer;
861 struct bt_ctf_field_type_integer *integer_type;
862 unsigned int size;
863 uint64_t max_value;
864
865 if (!field ||
866 bt_ctf_field_type_get_type_id(field->type) !=
867 CTF_TYPE_INTEGER) {
868 ret = -1;
869 goto end;
870 }
871
872 integer = container_of(field, struct bt_ctf_field_integer, parent);
873 integer_type = container_of(field->type,
874 struct bt_ctf_field_type_integer, parent);
875 if (integer_type->declaration.signedness) {
876 ret = -1;
877 goto end;
878 }
879
880 size = integer_type->declaration.len;
881 max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1;
882 if (value > max_value) {
883 ret = -1;
884 goto end;
885 }
886
887 integer->definition.value._unsigned = value;
888 integer->parent.payload_set = 1;
889 end:
890 return ret;
891 }
892
893 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field,
894 double *value)
895 {
896 int ret = 0;
897 struct bt_ctf_field_floating_point *floating_point;
898
899 if (!field || !value || !field->payload_set ||
900 bt_ctf_field_type_get_type_id(field->type) !=
901 CTF_TYPE_FLOAT) {
902 ret = -1;
903 goto end;
904 }
905
906 floating_point = container_of(field,
907 struct bt_ctf_field_floating_point, parent);
908 *value = floating_point->definition.value;
909 end:
910 return ret;
911 }
912
913 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
914 double value)
915 {
916 int ret = 0;
917 struct bt_ctf_field_floating_point *floating_point;
918
919 if (!field ||
920 bt_ctf_field_type_get_type_id(field->type) !=
921 CTF_TYPE_FLOAT) {
922 ret = -1;
923 goto end;
924 }
925 floating_point = container_of(field, struct bt_ctf_field_floating_point,
926 parent);
927 floating_point->definition.value = value;
928 floating_point->parent.payload_set = 1;
929 end:
930 return ret;
931 }
932
933 const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field)
934 {
935 const char *ret = NULL;
936 struct bt_ctf_field_string *string;
937
938 if (!field || !field->payload_set ||
939 bt_ctf_field_type_get_type_id(field->type) !=
940 CTF_TYPE_STRING) {
941 goto end;
942 }
943
944 string = container_of(field,
945 struct bt_ctf_field_string, parent);
946 ret = string->payload->str;
947 end:
948 return ret;
949 }
950
951 int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
952 const char *value)
953 {
954 int ret = 0;
955 struct bt_ctf_field_string *string;
956
957 if (!field || !value ||
958 bt_ctf_field_type_get_type_id(field->type) !=
959 CTF_TYPE_STRING) {
960 ret = -1;
961 goto end;
962 }
963
964 string = container_of(field, struct bt_ctf_field_string, parent);
965 if (string->payload) {
966 g_string_assign(string->payload, value);
967 } else {
968 string->payload = g_string_new(value);
969 }
970
971 string->parent.payload_set = 1;
972 end:
973 return ret;
974 }
975
976 int bt_ctf_field_string_append(struct bt_ctf_field *field,
977 const char *value)
978 {
979 int ret = 0;
980 struct bt_ctf_field_string *string_field;
981
982 if (!field || !value ||
983 bt_ctf_field_type_get_type_id(field->type) !=
984 CTF_TYPE_STRING) {
985 ret = -1;
986 goto end;
987 }
988
989 string_field = container_of(field, struct bt_ctf_field_string, parent);
990
991 if (string_field->payload) {
992 g_string_append(string_field->payload, value);
993 } else {
994 string_field->payload = g_string_new(value);
995 }
996
997 string_field->parent.payload_set = 1;
998
999 end:
1000 return ret;
1001 }
1002
1003 int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
1004 const char *value, unsigned int length)
1005 {
1006 int i;
1007 int ret = 0;
1008 unsigned int effective_length = length;
1009 struct bt_ctf_field_string *string_field;
1010
1011 if (!field || !value ||
1012 bt_ctf_field_type_get_type_id(field->type) !=
1013 CTF_TYPE_STRING) {
1014 ret = -1;
1015 goto end;
1016 }
1017
1018 string_field = container_of(field, struct bt_ctf_field_string, parent);
1019
1020 /* make sure no null bytes are appended */
1021 for (i = 0; i < length; ++i) {
1022 if (value[i] == '\0') {
1023 effective_length = i;
1024 break;
1025 }
1026 }
1027
1028 if (string_field->payload) {
1029 g_string_append_len(string_field->payload, value,
1030 effective_length);
1031 } else {
1032 string_field->payload = g_string_new_len(value,
1033 effective_length);
1034 }
1035
1036 string_field->parent.payload_set = 1;
1037
1038 end:
1039 return ret;
1040 }
1041
1042 BT_HIDDEN
1043 int bt_ctf_field_validate(struct bt_ctf_field *field)
1044 {
1045 int ret = 0;
1046 enum ctf_type_id type_id;
1047
1048 if (!field) {
1049 ret = -1;
1050 goto end;
1051 }
1052
1053 type_id = bt_ctf_field_type_get_type_id(field->type);
1054 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1055 ret = -1;
1056 goto end;
1057 }
1058
1059 ret = field_validate_funcs[type_id](field);
1060 end:
1061 return ret;
1062 }
1063
1064 BT_HIDDEN
1065 int bt_ctf_field_reset(struct bt_ctf_field *field)
1066 {
1067 int ret = 0;
1068 enum ctf_type_id type_id;
1069
1070 if (!field) {
1071 ret = -1;
1072 goto end;
1073 }
1074
1075 type_id = bt_ctf_field_type_get_type_id(field->type);
1076 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1077 ret = -1;
1078 goto end;
1079 }
1080
1081 ret = field_reset_funcs[type_id](field);
1082 end:
1083 return ret;
1084 }
1085
1086 BT_HIDDEN
1087 int bt_ctf_field_serialize(struct bt_ctf_field *field,
1088 struct ctf_stream_pos *pos)
1089 {
1090 int ret = 0;
1091 enum ctf_type_id type_id;
1092
1093 if (!field || !pos) {
1094 ret = -1;
1095 goto end;
1096 }
1097
1098 type_id = bt_ctf_field_type_get_type_id(field->type);
1099 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1100 ret = -1;
1101 goto end;
1102 }
1103
1104 ret = field_serialize_funcs[type_id](field, pos);
1105 end:
1106 return ret;
1107 }
1108
1109 struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
1110 {
1111 int ret;
1112 struct bt_ctf_field *copy = NULL;
1113 enum ctf_type_id type_id;
1114
1115 if (!field) {
1116 goto end;
1117 }
1118
1119 type_id = bt_ctf_field_type_get_type_id(field->type);
1120 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1121 goto end;
1122 }
1123
1124 copy = bt_ctf_field_create(field->type);
1125 if (!copy) {
1126 goto end;
1127 }
1128
1129 copy->payload_set = field->payload_set;
1130 ret = field_copy_funcs[type_id](field, copy);
1131 if (ret) {
1132 bt_ctf_field_put(copy);
1133 copy = NULL;
1134 }
1135 end:
1136 return copy;
1137 }
1138
1139 static
1140 struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
1141 {
1142 struct bt_ctf_field_type_integer *integer_type = container_of(type,
1143 struct bt_ctf_field_type_integer, parent);
1144 struct bt_ctf_field_integer *integer = g_new0(
1145 struct bt_ctf_field_integer, 1);
1146
1147 if (integer) {
1148 integer->definition.declaration = &integer_type->declaration;
1149 }
1150
1151 return integer ? &integer->parent : NULL;
1152 }
1153
1154 static
1155 struct bt_ctf_field *bt_ctf_field_enumeration_create(
1156 struct bt_ctf_field_type *type)
1157 {
1158 struct bt_ctf_field_enumeration *enumeration = g_new0(
1159 struct bt_ctf_field_enumeration, 1);
1160
1161 return enumeration ? &enumeration->parent : NULL;
1162 }
1163
1164 static
1165 struct bt_ctf_field *bt_ctf_field_floating_point_create(
1166 struct bt_ctf_field_type *type)
1167 {
1168 struct bt_ctf_field_floating_point *floating_point;
1169 struct bt_ctf_field_type_floating_point *floating_point_type;
1170
1171 floating_point = g_new0(struct bt_ctf_field_floating_point, 1);
1172 if (!floating_point) {
1173 goto end;
1174 }
1175
1176 floating_point_type = container_of(type,
1177 struct bt_ctf_field_type_floating_point, parent);
1178 floating_point->definition.declaration = container_of(
1179 type->declaration, struct declaration_float, p);
1180
1181
1182 floating_point->definition.sign = &floating_point->sign;
1183 floating_point->sign.declaration = &floating_point_type->sign;
1184 floating_point->definition.sign->p.declaration =
1185 &floating_point_type->sign.p;
1186
1187 floating_point->definition.mantissa = &floating_point->mantissa;
1188 floating_point->mantissa.declaration = &floating_point_type->mantissa;
1189 floating_point->definition.mantissa->p.declaration =
1190 &floating_point_type->mantissa.p;
1191
1192 floating_point->definition.exp = &floating_point->exp;
1193 floating_point->exp.declaration = &floating_point_type->exp;
1194 floating_point->definition.exp->p.declaration =
1195 &floating_point_type->exp.p;
1196
1197 end:
1198 return floating_point ? &floating_point->parent : NULL;
1199 }
1200
1201 static
1202 struct bt_ctf_field *bt_ctf_field_structure_create(
1203 struct bt_ctf_field_type *type)
1204 {
1205 struct bt_ctf_field_type_structure *structure_type = container_of(type,
1206 struct bt_ctf_field_type_structure, parent);
1207 struct bt_ctf_field_structure *structure = g_new0(
1208 struct bt_ctf_field_structure, 1);
1209 struct bt_ctf_field *field = NULL;
1210
1211 if (!structure || !structure_type->fields->len) {
1212 goto end;
1213 }
1214
1215 structure->field_name_to_index = structure_type->field_name_to_index;
1216 structure->fields = g_ptr_array_new_with_free_func(
1217 (GDestroyNotify)bt_ctf_field_put);
1218 g_ptr_array_set_size(structure->fields,
1219 g_hash_table_size(structure->field_name_to_index));
1220 field = &structure->parent;
1221 end:
1222 return field;
1223 }
1224
1225 static
1226 struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
1227 {
1228 struct bt_ctf_field_variant *variant = g_new0(
1229 struct bt_ctf_field_variant, 1);
1230 return variant ? &variant->parent : NULL;
1231 }
1232
1233 static
1234 struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
1235 {
1236 struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1);
1237 struct bt_ctf_field_type_array *array_type;
1238 unsigned int array_length;
1239
1240 if (!array || !type) {
1241 goto error;
1242 }
1243
1244 array_type = container_of(type, struct bt_ctf_field_type_array, parent);
1245 array_length = array_type->length;
1246 array->elements = g_ptr_array_sized_new(array_length);
1247 if (!array->elements) {
1248 goto error;
1249 }
1250
1251 g_ptr_array_set_free_func(array->elements,
1252 (GDestroyNotify)bt_ctf_field_put);
1253 g_ptr_array_set_size(array->elements, array_length);
1254 return &array->parent;
1255 error:
1256 g_free(array);
1257 return NULL;
1258 }
1259
1260 static
1261 struct bt_ctf_field *bt_ctf_field_sequence_create(
1262 struct bt_ctf_field_type *type)
1263 {
1264 struct bt_ctf_field_sequence *sequence = g_new0(
1265 struct bt_ctf_field_sequence, 1);
1266 return sequence ? &sequence->parent : NULL;
1267 }
1268
1269 static
1270 struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
1271 {
1272 struct bt_ctf_field_string *string = g_new0(
1273 struct bt_ctf_field_string, 1);
1274 return string ? &string->parent : NULL;
1275 }
1276
1277 static
1278 void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
1279 {
1280 struct bt_ctf_field *field;
1281 struct bt_ctf_field_type *type;
1282 enum ctf_type_id type_id;
1283
1284 if (!ref) {
1285 return;
1286 }
1287
1288 field = container_of(ref, struct bt_ctf_field, ref_count);
1289 type = field->type;
1290 type_id = bt_ctf_field_type_get_type_id(type);
1291 if (type_id <= CTF_TYPE_UNKNOWN ||
1292 type_id >= NR_CTF_TYPES) {
1293 return;
1294 }
1295
1296 field_destroy_funcs[type_id](field);
1297 if (type) {
1298 bt_ctf_field_type_put(type);
1299 }
1300 }
1301
1302 static
1303 void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
1304 {
1305 struct bt_ctf_field_integer *integer;
1306
1307 if (!field) {
1308 return;
1309 }
1310
1311 integer = container_of(field, struct bt_ctf_field_integer, parent);
1312 g_free(integer);
1313 }
1314
1315 static
1316 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field)
1317 {
1318 struct bt_ctf_field_enumeration *enumeration;
1319
1320 if (!field) {
1321 return;
1322 }
1323
1324 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1325 parent);
1326 bt_ctf_field_put(enumeration->payload);
1327 g_free(enumeration);
1328 }
1329
1330 static
1331 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
1332 {
1333 struct bt_ctf_field_floating_point *floating_point;
1334
1335 if (!field) {
1336 return;
1337 }
1338
1339 floating_point = container_of(field, struct bt_ctf_field_floating_point,
1340 parent);
1341 g_free(floating_point);
1342 }
1343
1344 static
1345 void bt_ctf_field_structure_destroy(struct bt_ctf_field *field)
1346 {
1347 struct bt_ctf_field_structure *structure;
1348
1349 if (!field) {
1350 return;
1351 }
1352
1353 structure = container_of(field, struct bt_ctf_field_structure, parent);
1354 g_ptr_array_free(structure->fields, TRUE);
1355 g_free(structure);
1356 }
1357
1358 static
1359 void bt_ctf_field_variant_destroy(struct bt_ctf_field *field)
1360 {
1361 struct bt_ctf_field_variant *variant;
1362
1363 if (!field) {
1364 return;
1365 }
1366
1367 variant = container_of(field, struct bt_ctf_field_variant, parent);
1368 bt_ctf_field_put(variant->tag);
1369 bt_ctf_field_put(variant->payload);
1370 g_free(variant);
1371 }
1372
1373 static
1374 void bt_ctf_field_array_destroy(struct bt_ctf_field *field)
1375 {
1376 struct bt_ctf_field_array *array;
1377
1378 if (!field) {
1379 return;
1380 }
1381
1382 array = container_of(field, struct bt_ctf_field_array, parent);
1383 g_ptr_array_free(array->elements, TRUE);
1384 g_free(array);
1385 }
1386
1387 static
1388 void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
1389 {
1390 struct bt_ctf_field_sequence *sequence;
1391
1392 if (!field) {
1393 return;
1394 }
1395
1396 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1397 g_ptr_array_free(sequence->elements, TRUE);
1398 bt_ctf_field_put(sequence->length);
1399 g_free(sequence);
1400 }
1401
1402 static
1403 void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
1404 {
1405 struct bt_ctf_field_string *string;
1406 if (!field) {
1407 return;
1408 }
1409
1410 string = container_of(field, struct bt_ctf_field_string, parent);
1411 if (string->payload) {
1412 g_string_free(string->payload, TRUE);
1413 }
1414 g_free(string);
1415 }
1416
1417 static
1418 int bt_ctf_field_generic_validate(struct bt_ctf_field *field)
1419 {
1420 return (field && field->payload_set) ? 0 : -1;
1421 }
1422
1423 static
1424 int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field)
1425 {
1426 int ret;
1427 struct bt_ctf_field_enumeration *enumeration;
1428
1429 if (!field) {
1430 ret = -1;
1431 goto end;
1432 }
1433
1434 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1435 parent);
1436 if (!enumeration->payload) {
1437 ret = -1;
1438 goto end;
1439 }
1440
1441 ret = bt_ctf_field_validate(enumeration->payload);
1442 end:
1443 return ret;
1444 }
1445
1446 static
1447 int bt_ctf_field_structure_validate(struct bt_ctf_field *field)
1448 {
1449 size_t i;
1450 int ret = 0;
1451 struct bt_ctf_field_structure *structure;
1452
1453 if (!field) {
1454 ret = -1;
1455 goto end;
1456 }
1457
1458 structure = container_of(field, struct bt_ctf_field_structure, parent);
1459 for (i = 0; i < structure->fields->len; i++) {
1460 ret = bt_ctf_field_validate(structure->fields->pdata[i]);
1461 if (ret) {
1462 goto end;
1463 }
1464 }
1465 end:
1466 return ret;
1467 }
1468
1469 static
1470 int bt_ctf_field_variant_validate(struct bt_ctf_field *field)
1471 {
1472 int ret = 0;
1473 struct bt_ctf_field_variant *variant;
1474
1475 if (!field) {
1476 ret = -1;
1477 goto end;
1478 }
1479
1480 variant = container_of(field, struct bt_ctf_field_variant, parent);
1481 ret = bt_ctf_field_validate(variant->payload);
1482 end:
1483 return ret;
1484 }
1485
1486 static
1487 int bt_ctf_field_array_validate(struct bt_ctf_field *field)
1488 {
1489 size_t i;
1490 int ret = 0;
1491 struct bt_ctf_field_array *array;
1492
1493 if (!field) {
1494 ret = -1;
1495 goto end;
1496 }
1497
1498 array = container_of(field, struct bt_ctf_field_array, parent);
1499 for (i = 0; i < array->elements->len; i++) {
1500 ret = bt_ctf_field_validate(array->elements->pdata[i]);
1501 if (ret) {
1502 goto end;
1503 }
1504 }
1505 end:
1506 return ret;
1507 }
1508
1509 static
1510 int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
1511 {
1512 size_t i;
1513 int ret = 0;
1514 struct bt_ctf_field_sequence *sequence;
1515
1516 if (!field) {
1517 ret = -1;
1518 goto end;
1519 }
1520
1521 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1522 for (i = 0; i < sequence->elements->len; i++) {
1523 ret = bt_ctf_field_validate(sequence->elements->pdata[i]);
1524 if (ret) {
1525 goto end;
1526 }
1527 }
1528 end:
1529 return ret;
1530 }
1531
1532 static
1533 int bt_ctf_field_generic_reset(struct bt_ctf_field *field)
1534 {
1535 int ret = 0;
1536
1537 if (!field) {
1538 ret = -1;
1539 goto end;
1540 }
1541
1542 field->payload_set = 0;
1543 end:
1544 return ret;
1545 }
1546
1547 static
1548 int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field)
1549 {
1550 int ret = 0;
1551 struct bt_ctf_field_enumeration *enumeration;
1552
1553 if (!field) {
1554 ret = -1;
1555 goto end;
1556 }
1557
1558 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1559 parent);
1560 if (!enumeration->payload) {
1561 goto end;
1562 }
1563
1564 ret = bt_ctf_field_reset(enumeration->payload);
1565 end:
1566 return ret;
1567 }
1568
1569 static
1570 int bt_ctf_field_structure_reset(struct bt_ctf_field *field)
1571 {
1572 size_t i;
1573 int ret = 0;
1574 struct bt_ctf_field_structure *structure;
1575
1576 if (!field) {
1577 ret = -1;
1578 goto end;
1579 }
1580
1581 structure = container_of(field, struct bt_ctf_field_structure, parent);
1582 for (i = 0; i < structure->fields->len; i++) {
1583 struct bt_ctf_field *member = structure->fields->pdata[i];
1584
1585 if (!member) {
1586 /*
1587 * Structure members are lazily initialized; skip if
1588 * this member has not been allocated yet.
1589 */
1590 continue;
1591 }
1592
1593 ret = bt_ctf_field_reset(member);
1594 if (ret) {
1595 goto end;
1596 }
1597 }
1598 end:
1599 return ret;
1600 }
1601
1602 static
1603 int bt_ctf_field_variant_reset(struct bt_ctf_field *field)
1604 {
1605 int ret = 0;
1606 struct bt_ctf_field_variant *variant;
1607
1608 if (!field) {
1609 ret = -1;
1610 goto end;
1611 }
1612
1613 variant = container_of(field, struct bt_ctf_field_variant, parent);
1614 if (variant->payload) {
1615 ret = bt_ctf_field_reset(variant->payload);
1616 }
1617 end:
1618 return ret;
1619 }
1620
1621 static
1622 int bt_ctf_field_array_reset(struct bt_ctf_field *field)
1623 {
1624 size_t i;
1625 int ret = 0;
1626 struct bt_ctf_field_array *array;
1627
1628 if (!field) {
1629 ret = -1;
1630 goto end;
1631 }
1632
1633 array = container_of(field, struct bt_ctf_field_array, parent);
1634 for (i = 0; i < array->elements->len; i++) {
1635 struct bt_ctf_field *member = array->elements->pdata[i];
1636
1637 if (!member) {
1638 /*
1639 * Array elements are lazily initialized; skip if
1640 * this member has not been allocated yet.
1641 */
1642 continue;
1643 }
1644
1645 ret = bt_ctf_field_reset(member);
1646 if (ret) {
1647 goto end;
1648 }
1649 }
1650 end:
1651 return ret;
1652 }
1653
1654 static
1655 int bt_ctf_field_sequence_reset(struct bt_ctf_field *field)
1656 {
1657 size_t i;
1658 int ret = 0;
1659 struct bt_ctf_field_sequence *sequence;
1660
1661 if (!field) {
1662 ret = -1;
1663 goto end;
1664 }
1665
1666 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1667 for (i = 0; i < sequence->elements->len; i++) {
1668 struct bt_ctf_field *member = sequence->elements->pdata[i];
1669
1670 if (!member) {
1671 /*
1672 * Sequence elements are lazily initialized; skip if
1673 * this member has not been allocated yet.
1674 */
1675 continue;
1676 }
1677
1678 ret = bt_ctf_field_reset(member);
1679 if (ret) {
1680 goto end;
1681 }
1682 }
1683 end:
1684 return ret;
1685 }
1686
1687 static
1688 int bt_ctf_field_string_reset(struct bt_ctf_field *field)
1689 {
1690 int ret = 0;
1691 struct bt_ctf_field_string *string;
1692
1693 if (!field) {
1694 ret = -1;
1695 goto end;
1696 }
1697
1698 ret = bt_ctf_field_generic_reset(field);
1699 if (ret) {
1700 goto end;
1701 }
1702
1703 string = container_of(field, struct bt_ctf_field_string, parent);
1704 if (string->payload) {
1705 g_string_truncate(string->payload, 0);
1706 }
1707 end:
1708 return ret;
1709 }
1710
1711 static
1712 int bt_ctf_field_integer_serialize(struct bt_ctf_field *field,
1713 struct ctf_stream_pos *pos)
1714 {
1715 int ret = 0;
1716 struct bt_ctf_field_integer *integer = container_of(field,
1717 struct bt_ctf_field_integer, parent);
1718
1719 retry:
1720 ret = ctf_integer_write(&pos->parent, &integer->definition.p);
1721 if (ret == -EFAULT) {
1722 /*
1723 * The field is too large to fit in the current packet's
1724 * remaining space. Bump the packet size and retry.
1725 */
1726 ret = increase_packet_size(pos);
1727 if (ret) {
1728 goto end;
1729 }
1730 goto retry;
1731 }
1732 end:
1733 return ret;
1734 }
1735
1736 static
1737 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field,
1738 struct ctf_stream_pos *pos)
1739 {
1740 struct bt_ctf_field_enumeration *enumeration = container_of(
1741 field, struct bt_ctf_field_enumeration, parent);
1742
1743 return bt_ctf_field_serialize(enumeration->payload, pos);
1744 }
1745
1746 static
1747 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field,
1748 struct ctf_stream_pos *pos)
1749 {
1750 int ret = 0;
1751 struct bt_ctf_field_floating_point *floating_point = container_of(field,
1752 struct bt_ctf_field_floating_point, parent);
1753
1754 retry:
1755 ret = ctf_float_write(&pos->parent, &floating_point->definition.p);
1756 if (ret == -EFAULT) {
1757 /*
1758 * The field is too large to fit in the current packet's
1759 * remaining space. Bump the packet size and retry.
1760 */
1761 ret = increase_packet_size(pos);
1762 if (ret) {
1763 goto end;
1764 }
1765 goto retry;
1766 }
1767 end:
1768 return ret;
1769 }
1770
1771 static
1772 int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
1773 struct ctf_stream_pos *pos)
1774 {
1775 size_t i;
1776 int ret = 0;
1777 struct bt_ctf_field_structure *structure = container_of(
1778 field, struct bt_ctf_field_structure, parent);
1779
1780 while (!ctf_pos_access_ok(pos,
1781 offset_align(pos->offset,
1782 field->type->declaration->alignment))) {
1783 ret = increase_packet_size(pos);
1784 if (ret) {
1785 goto end;
1786 }
1787 }
1788
1789 if (!ctf_align_pos(pos, field->type->declaration->alignment)) {
1790 ret = -1;
1791 goto end;
1792 }
1793
1794 for (i = 0; i < structure->fields->len; i++) {
1795 struct bt_ctf_field *field = g_ptr_array_index(
1796 structure->fields, i);
1797
1798 ret = bt_ctf_field_serialize(field, pos);
1799 if (ret) {
1800 break;
1801 }
1802 }
1803 end:
1804 return ret;
1805 }
1806
1807 static
1808 int bt_ctf_field_variant_serialize(struct bt_ctf_field *field,
1809 struct ctf_stream_pos *pos)
1810 {
1811 struct bt_ctf_field_variant *variant = container_of(
1812 field, struct bt_ctf_field_variant, parent);
1813
1814 return bt_ctf_field_serialize(variant->payload, pos);
1815 }
1816
1817 static
1818 int bt_ctf_field_array_serialize(struct bt_ctf_field *field,
1819 struct ctf_stream_pos *pos)
1820 {
1821 size_t i;
1822 int ret = 0;
1823 struct bt_ctf_field_array *array = container_of(
1824 field, struct bt_ctf_field_array, parent);
1825
1826 for (i = 0; i < array->elements->len; i++) {
1827 ret = bt_ctf_field_serialize(
1828 g_ptr_array_index(array->elements, i), pos);
1829 if (ret) {
1830 goto end;
1831 }
1832 }
1833 end:
1834 return ret;
1835 }
1836
1837 static
1838 int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field,
1839 struct ctf_stream_pos *pos)
1840 {
1841 size_t i;
1842 int ret = 0;
1843 struct bt_ctf_field_sequence *sequence = container_of(
1844 field, struct bt_ctf_field_sequence, parent);
1845
1846 for (i = 0; i < sequence->elements->len; i++) {
1847 ret = bt_ctf_field_serialize(
1848 g_ptr_array_index(sequence->elements, i), pos);
1849 if (ret) {
1850 goto end;
1851 }
1852 }
1853 end:
1854 return ret;
1855 }
1856
1857 static
1858 int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
1859 struct ctf_stream_pos *pos)
1860 {
1861 size_t i;
1862 int ret = 0;
1863 struct bt_ctf_field_string *string = container_of(field,
1864 struct bt_ctf_field_string, parent);
1865 struct bt_ctf_field_type *character_type =
1866 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1867 struct bt_ctf_field *character = bt_ctf_field_create(character_type);
1868
1869 for (i = 0; i < string->payload->len + 1; i++) {
1870 ret = bt_ctf_field_unsigned_integer_set_value(character,
1871 (uint64_t) string->payload->str[i]);
1872 if (ret) {
1873 goto end;
1874 }
1875
1876 ret = bt_ctf_field_integer_serialize(character, pos);
1877 if (ret) {
1878 goto end;
1879 }
1880 }
1881 end:
1882 bt_ctf_field_put(character);
1883 bt_ctf_field_type_put(character_type);
1884 return ret;
1885 }
1886
1887 static
1888 int bt_ctf_field_integer_copy(struct bt_ctf_field *src,
1889 struct bt_ctf_field *dst)
1890 {
1891 struct bt_ctf_field_integer *integer_src, *integer_dst;
1892
1893 integer_src = container_of(src, struct bt_ctf_field_integer, parent);
1894 integer_dst = container_of(dst, struct bt_ctf_field_integer, parent);
1895
1896 memcpy(&integer_dst->definition, &integer_src->definition,
1897 sizeof(struct definition_integer));
1898 return 0;
1899 }
1900
1901 static
1902 int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src,
1903 struct bt_ctf_field *dst)
1904 {
1905 int ret = 0;
1906 struct bt_ctf_field_enumeration *enum_src, *enum_dst;
1907
1908 enum_src = container_of(src, struct bt_ctf_field_enumeration, parent);
1909 enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent);
1910
1911 if (enum_src->payload) {
1912 enum_dst->payload = bt_ctf_field_copy(enum_src->payload);
1913 if (!enum_dst->payload) {
1914 ret = -1;
1915 goto end;
1916 }
1917 }
1918 end:
1919 return ret;
1920 }
1921
1922 static
1923 int bt_ctf_field_floating_point_copy(
1924 struct bt_ctf_field *src, struct bt_ctf_field *dst)
1925 {
1926 struct bt_ctf_field_floating_point *float_src, *float_dst;
1927
1928 float_src = container_of(src, struct bt_ctf_field_floating_point,
1929 parent);
1930 float_dst = container_of(dst, struct bt_ctf_field_floating_point,
1931 parent);
1932
1933 memcpy(&float_dst->definition, &float_src->definition,
1934 sizeof(struct definition_float));
1935 memcpy(&float_dst->sign, &float_src->sign,
1936 sizeof(struct definition_integer));
1937 memcpy(&float_dst->mantissa, &float_src->mantissa,
1938 sizeof(struct definition_integer));
1939 memcpy(&float_dst->exp, &float_src->exp,
1940 sizeof(struct definition_integer));
1941 return 0;
1942 }
1943
1944 static
1945 int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
1946 struct bt_ctf_field *dst)
1947 {
1948 int ret = 0, i;
1949 struct bt_ctf_field_structure *struct_src, *struct_dst;
1950
1951 struct_src = container_of(src, struct bt_ctf_field_structure, parent);
1952 struct_dst = container_of(dst, struct bt_ctf_field_structure, parent);
1953
1954 /* This field_name_to_index HT is owned by the structure field type */
1955 struct_dst->field_name_to_index = struct_src->field_name_to_index;
1956 g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len);
1957
1958 for (i = 0; i < struct_src->fields->len; i++) {
1959 struct bt_ctf_field *field =
1960 g_ptr_array_index(struct_src->fields, i);
1961 struct bt_ctf_field *field_copy = NULL;
1962
1963 if (field) {
1964 field_copy = bt_ctf_field_copy(field);
1965
1966 if (!field_copy) {
1967 ret = -1;
1968 goto end;
1969 }
1970 }
1971
1972 g_ptr_array_index(struct_dst->fields, i) = field_copy;
1973 }
1974 end:
1975 return ret;
1976 }
1977
1978 static
1979 int bt_ctf_field_variant_copy(struct bt_ctf_field *src,
1980 struct bt_ctf_field *dst)
1981 {
1982 int ret = 0;
1983 struct bt_ctf_field_variant *variant_src, *variant_dst;
1984
1985 variant_src = container_of(src, struct bt_ctf_field_variant, parent);
1986 variant_dst = container_of(dst, struct bt_ctf_field_variant, parent);
1987
1988 if (variant_src->tag) {
1989 variant_dst->tag = bt_ctf_field_copy(variant_src->tag);
1990 if (!variant_dst->tag) {
1991 ret = -1;
1992 goto end;
1993 }
1994 }
1995 if (variant_src->payload) {
1996 variant_dst->payload = bt_ctf_field_copy(variant_src->payload);
1997 if (!variant_dst->payload) {
1998 ret = -1;
1999 goto end;
2000 }
2001 }
2002 end:
2003 return ret;
2004 }
2005
2006 static
2007 int bt_ctf_field_array_copy(struct bt_ctf_field *src,
2008 struct bt_ctf_field *dst)
2009 {
2010 int ret = 0, i;
2011 struct bt_ctf_field_array *array_src, *array_dst;
2012
2013 array_src = container_of(src, struct bt_ctf_field_array, parent);
2014 array_dst = container_of(dst, struct bt_ctf_field_array, parent);
2015
2016 g_ptr_array_set_size(array_dst->elements, array_src->elements->len);
2017 for (i = 0; i < array_src->elements->len; i++) {
2018 struct bt_ctf_field *field =
2019 g_ptr_array_index(array_src->elements, i);
2020 struct bt_ctf_field *field_copy = NULL;
2021
2022 if (field) {
2023 field_copy = bt_ctf_field_copy(field);
2024
2025 if (!field_copy) {
2026 ret = -1;
2027 goto end;
2028 }
2029 }
2030
2031 g_ptr_array_index(array_dst->elements, i) = field_copy;
2032 }
2033 end:
2034 return ret;
2035 }
2036
2037 static
2038 int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
2039 struct bt_ctf_field *dst)
2040 {
2041 int ret = 0, i;
2042 struct bt_ctf_field_sequence *sequence_src, *sequence_dst;
2043 struct bt_ctf_field *src_length;
2044 struct bt_ctf_field *dst_length;
2045
2046 sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
2047 sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
2048
2049 src_length = bt_ctf_field_sequence_get_length(src);
2050
2051 if (!src_length) {
2052 /* no length set yet: keep destination sequence empty */
2053 goto end;
2054 }
2055
2056 /* copy source length */
2057 dst_length = bt_ctf_field_copy(src_length);
2058 bt_ctf_field_put(src_length);
2059
2060 if (!dst_length) {
2061 ret = -1;
2062 goto end;
2063 }
2064
2065 /* this will initialize the destination sequence's internal array */
2066 ret = bt_ctf_field_sequence_set_length(dst, dst_length);
2067 bt_ctf_field_put(dst_length);
2068
2069 if (ret) {
2070 goto end;
2071 }
2072
2073 assert(sequence_dst->elements->len == sequence_src->elements->len);
2074
2075 for (i = 0; i < sequence_src->elements->len; i++) {
2076 struct bt_ctf_field *field =
2077 g_ptr_array_index(sequence_src->elements, i);
2078 struct bt_ctf_field *field_copy = NULL;
2079
2080 if (field) {
2081 field_copy = bt_ctf_field_copy(field);
2082
2083 if (!field_copy) {
2084 ret = -1;
2085 goto end;
2086 }
2087 }
2088
2089 g_ptr_array_index(sequence_dst->elements, i) = field_copy;
2090 }
2091 end:
2092 return ret;
2093 }
2094
2095 static
2096 int bt_ctf_field_string_copy(struct bt_ctf_field *src,
2097 struct bt_ctf_field *dst)
2098 {
2099 int ret = 0;
2100 struct bt_ctf_field_string *string_src, *string_dst;
2101
2102 string_src = container_of(src, struct bt_ctf_field_string, parent);
2103 string_dst = container_of(dst, struct bt_ctf_field_string, parent);
2104
2105 if (string_src->payload) {
2106 string_dst->payload = g_string_new(string_src->payload->str);
2107 if (!string_dst->payload) {
2108 ret = -1;
2109 goto end;
2110 }
2111 }
2112 end:
2113 return ret;
2114 }
2115
2116 static
2117 int increase_packet_size(struct ctf_stream_pos *pos)
2118 {
2119 int ret;
2120
2121 assert(pos);
2122 ret = munmap_align(pos->base_mma);
2123 if (ret) {
2124 goto end;
2125 }
2126
2127 pos->packet_size += PACKET_LEN_INCREMENT;
2128 ret = posix_fallocate(pos->fd, pos->mmap_offset,
2129 pos->packet_size / CHAR_BIT);
2130 if (ret) {
2131 goto end;
2132 }
2133
2134 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
2135 pos->flags, pos->fd, pos->mmap_offset);
2136 if (pos->base_mma == MAP_FAILED) {
2137 ret = -1;
2138 }
2139 end:
2140 return ret;
2141 }
This page took 0.137039 seconds and 4 git commands to generate.