Fix lint warnings
[babeltrace.git] / formats / ctf / writer / event-fields.c
CommitLineData
273b65be
JG
1/*
2 * event-fields.c
3 *
4 * Babeltrace CTF Writer
5 *
6 * Copyright 2013 EfficiOS Inc.
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-writer/event-fields-internal.h>
31#include <babeltrace/ctf-writer/event-types-internal.h>
32#include <babeltrace/compiler.h>
33
34#define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
35
36static
37struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *);
38static
39struct bt_ctf_field *bt_ctf_field_enumeration_create(
40 struct bt_ctf_field_type *);
41static
42struct bt_ctf_field *bt_ctf_field_floating_point_create(
43 struct bt_ctf_field_type *);
44static
45struct bt_ctf_field *bt_ctf_field_structure_create(
46 struct bt_ctf_field_type *);
47static
48struct bt_ctf_field *bt_ctf_field_variant_create(
49 struct bt_ctf_field_type *);
50static
51struct bt_ctf_field *bt_ctf_field_array_create(
52 struct bt_ctf_field_type *);
53static
54struct bt_ctf_field *bt_ctf_field_sequence_create(
55 struct bt_ctf_field_type *);
56static
57struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *);
58
59static
60void bt_ctf_field_destroy(struct bt_ctf_ref *);
61static
62void bt_ctf_field_integer_destroy(struct bt_ctf_field *);
63static
64void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *);
65static
66void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *);
67static
68void bt_ctf_field_structure_destroy(struct bt_ctf_field *);
69static
70void bt_ctf_field_variant_destroy(struct bt_ctf_field *);
71static
72void bt_ctf_field_array_destroy(struct bt_ctf_field *);
73static
74void bt_ctf_field_sequence_destroy(struct bt_ctf_field *);
75static
76void bt_ctf_field_string_destroy(struct bt_ctf_field *);
77
78static
79int bt_ctf_field_generic_validate(struct bt_ctf_field *field);
80static
81int bt_ctf_field_structure_validate(struct bt_ctf_field *field);
82static
83int bt_ctf_field_variant_validate(struct bt_ctf_field *field);
84static
85int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field);
86static
87int bt_ctf_field_array_validate(struct bt_ctf_field *field);
88static
89int bt_ctf_field_sequence_validate(struct bt_ctf_field *field);
90
91static
92int bt_ctf_field_integer_serialize(struct bt_ctf_field *,
93 struct ctf_stream_pos *);
94static
95int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *,
96 struct ctf_stream_pos *);
97static
98int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *,
99 struct ctf_stream_pos *);
100static
101int bt_ctf_field_structure_serialize(struct bt_ctf_field *,
102 struct ctf_stream_pos *);
103static
104int bt_ctf_field_variant_serialize(struct bt_ctf_field *,
105 struct ctf_stream_pos *);
106static
107int bt_ctf_field_array_serialize(struct bt_ctf_field *,
108 struct ctf_stream_pos *);
109static
110int bt_ctf_field_sequence_serialize(struct bt_ctf_field *,
111 struct ctf_stream_pos *);
112static
113int bt_ctf_field_string_serialize(struct bt_ctf_field *,
114 struct ctf_stream_pos *);
115
116static
117int increase_packet_size(struct ctf_stream_pos *pos);
118
119static
120struct bt_ctf_field *(*field_create_funcs[])(
121 struct bt_ctf_field_type *) = {
122 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
123 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
124 [CTF_TYPE_FLOAT] =
125 bt_ctf_field_floating_point_create,
126 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create,
127 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create,
128 [CTF_TYPE_ARRAY] = bt_ctf_field_array_create,
129 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create,
130 [CTF_TYPE_STRING] = bt_ctf_field_string_create,
131};
132
133static
134void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
135 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
136 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
137 [CTF_TYPE_FLOAT] =
138 bt_ctf_field_floating_point_destroy,
139 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy,
140 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy,
141 [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy,
142 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy,
143 [CTF_TYPE_STRING] = bt_ctf_field_string_destroy,
144};
145
146static
147int (*field_validate_funcs[])(struct bt_ctf_field *) = {
148 [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
149 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
150 [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
151 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate,
152 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate,
153 [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate,
154 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate,
155 [CTF_TYPE_STRING] = bt_ctf_field_generic_validate,
156};
157
158static
159int (*field_serialize_funcs[])(struct bt_ctf_field *,
160 struct ctf_stream_pos *) = {
161 [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
162 [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
163 [CTF_TYPE_FLOAT] =
164 bt_ctf_field_floating_point_serialize,
165 [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize,
166 [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize,
167 [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize,
168 [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize,
169 [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
170};
171
172struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
173{
174 struct bt_ctf_field *field = NULL;
175 enum ctf_type_id type_id;
176
177 if (!type) {
178 goto error;
179 }
180
181 type_id = bt_ctf_field_type_get_type_id(type);
182 if (type_id <= CTF_TYPE_UNKNOWN ||
183 type_id >= NR_CTF_TYPES) {
184 goto error;
185 }
186
187 field = field_create_funcs[type_id](type);
188 if (!field) {
189 goto error;
190 }
191
192 /* The type's declaration can't change after this point */
193 bt_ctf_field_type_freeze(type);
194 bt_ctf_field_type_get(type);
195 bt_ctf_ref_init(&field->ref_count);
196 field->type = type;
197error:
198 return field;
199}
200
201void bt_ctf_field_get(struct bt_ctf_field *field)
202{
203 if (field) {
204 bt_ctf_ref_get(&field->ref_count);
205 }
206}
207
208void bt_ctf_field_put(struct bt_ctf_field *field)
209{
210 if (field) {
211 bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
212 }
213}
214
215int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
216 struct bt_ctf_field *length_field)
217{
218 int ret = 0;
219 struct bt_ctf_field_type_integer *length_type;
220 struct bt_ctf_field_integer *length;
221 struct bt_ctf_field_sequence *sequence;
222 uint64_t sequence_length;
223
224 if (!field || !length_field) {
225 ret = -1;
226 goto end;
227 }
228 if (bt_ctf_field_type_get_type_id(length_field->type) !=
229 CTF_TYPE_INTEGER) {
230 ret = -1;
231 goto end;
232 }
233
234 length_type = container_of(length_field->type,
235 struct bt_ctf_field_type_integer, parent);
236 if (length_type->declaration.signedness) {
237 ret = -1;
238 goto end;
239 }
240
241 length = container_of(length_field, struct bt_ctf_field_integer,
242 parent);
243 sequence_length = length->definition.value._unsigned;
244 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
245 if (sequence->elements) {
246 g_ptr_array_free(sequence->elements, TRUE);
247 bt_ctf_field_put(sequence->length);
248 }
249
fe0fe95c 250 sequence->elements = g_ptr_array_sized_new((size_t)sequence_length);
273b65be
JG
251 if (!sequence->elements) {
252 ret = -1;
253 goto end;
254 }
255
fe0fe95c
JG
256 g_ptr_array_set_free_func(sequence->elements,
257 (GDestroyNotify)bt_ctf_field_put);
273b65be
JG
258 g_ptr_array_set_size(sequence->elements, (size_t)sequence_length);
259 bt_ctf_field_get(length_field);
260 sequence->length = length_field;
261end:
262 return ret;
263}
264
265struct bt_ctf_field *bt_ctf_field_structure_get_field(
266 struct bt_ctf_field *field, const char *name)
267{
268 struct bt_ctf_field *new_field = NULL;
269 GQuark field_quark;
270 struct bt_ctf_field_structure *structure;
271 struct bt_ctf_field_type_structure *structure_type;
272 struct bt_ctf_field_type *field_type;
273 size_t index;
274
275 if (!field || !name ||
276 bt_ctf_field_type_get_type_id(field->type) !=
277 CTF_TYPE_STRUCT) {
278 goto error;
279 }
280
281 field_quark = g_quark_from_string(name);
282 structure = container_of(field, struct bt_ctf_field_structure, parent);
283 structure_type = container_of(field->type,
284 struct bt_ctf_field_type_structure, parent);
285 field_type = bt_ctf_field_type_structure_get_type(structure_type, name);
286 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
287 GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) {
288 goto error;
289 }
290
291 if (structure->fields->pdata[index]) {
292 new_field = structure->fields->pdata[index];
293 goto end;
294 }
295
296 new_field = bt_ctf_field_create(field_type);
297 if (!new_field) {
298 goto error;
299 }
300
301 structure->fields->pdata[index] = new_field;
302end:
303 bt_ctf_field_get(new_field);
304error:
305 return new_field;
306}
307
308BT_HIDDEN
309int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
310 const char *name, struct bt_ctf_field *value)
311{
312 int ret = 0;
313 GQuark field_quark;
314 struct bt_ctf_field_structure *structure;
315 struct bt_ctf_field_type_structure *structure_type;
316 struct bt_ctf_field_type *expected_field_type;
317 size_t index;
318
319 if (!field || !name || !value ||
320 bt_ctf_field_type_get_type_id(field->type) !=
321 CTF_TYPE_STRUCT) {
322 ret = -1;
323 goto end;
324 }
325
326 field_quark = g_quark_from_string(name);
327 structure = container_of(field, struct bt_ctf_field_structure, parent);
328 structure_type = container_of(field->type,
329 struct bt_ctf_field_type_structure, parent);
330 expected_field_type = bt_ctf_field_type_structure_get_type(
331 structure_type, name);
332 if (expected_field_type != value->type) {
333 ret = -1;
334 goto end;
335 }
336
337 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
338 GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) {
339 goto end;
340 }
341
342 if (structure->fields->pdata[index]) {
343 bt_ctf_field_put(structure->fields->pdata[index]);
344 }
345
346 structure->fields->pdata[index] = value;
347 bt_ctf_field_get(value);
348end:
349 return ret;
350}
351
352struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
353 uint64_t index)
354{
355 struct bt_ctf_field *new_field = NULL;
356 struct bt_ctf_field_array *array;
357 struct bt_ctf_field_type_array *array_type;
358 struct bt_ctf_field_type *field_type;
359
360 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
361 CTF_TYPE_ARRAY) {
362 goto end;
363 }
364
365 array = container_of(field, struct bt_ctf_field_array, parent);
366 if (index >= array->elements->len) {
367 goto end;
368 }
369
370 array_type = container_of(field->type, struct bt_ctf_field_type_array,
371 parent);
372 field_type = bt_ctf_field_type_array_get_element_type(array_type);
373 if (array->elements->pdata[(size_t)index]) {
374 new_field = array->elements->pdata[(size_t)index];
375 goto end;
376 }
377
378 new_field = bt_ctf_field_create(field_type);
379 bt_ctf_field_get(new_field);
380 array->elements->pdata[(size_t)index] = new_field;
381end:
382 return new_field;
383}
384
385struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
386 uint64_t index)
387{
388 struct bt_ctf_field *new_field = NULL;
389 struct bt_ctf_field_sequence *sequence;
390 struct bt_ctf_field_type_sequence *sequence_type;
391 struct bt_ctf_field_type *field_type;
392
393 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
394 CTF_TYPE_SEQUENCE) {
395 goto end;
396 }
397
398 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
399 if (!sequence->elements || sequence->elements->len <= index) {
400 goto end;
401 }
402
403 sequence_type = container_of(field->type,
404 struct bt_ctf_field_type_sequence, parent);
405 field_type = bt_ctf_field_type_sequence_get_element_type(sequence_type);
406 if (sequence->elements->pdata[(size_t)index]) {
407 new_field = sequence->elements->pdata[(size_t)index];
408 goto end;
409 }
410
411 new_field = bt_ctf_field_create(field_type);
412 bt_ctf_field_get(new_field);
413 sequence->elements->pdata[(size_t)index] = new_field;
414end:
415 return new_field;
416}
417
418struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
419 struct bt_ctf_field *tag_field)
420{
421 struct bt_ctf_field *new_field = NULL;
422 struct bt_ctf_field_variant *variant;
423 struct bt_ctf_field_type_variant *variant_type;
424 struct bt_ctf_field_type *field_type;
425 struct bt_ctf_field *tag_enum = NULL;
426 struct bt_ctf_field_integer *tag_enum_integer;
427 int64_t tag_enum_value;
428
429 if (!field || !tag_field ||
430 bt_ctf_field_type_get_type_id(field->type) !=
431 CTF_TYPE_VARIANT ||
432 bt_ctf_field_type_get_type_id(tag_field->type) !=
433 CTF_TYPE_ENUM) {
434 goto end;
435 }
436
437 variant = container_of(field, struct bt_ctf_field_variant, parent);
438 variant_type = container_of(field->type,
439 struct bt_ctf_field_type_variant, parent);
440 tag_enum = bt_ctf_field_enumeration_get_container(tag_field);
441 if (!tag_enum) {
442 goto end;
443 }
444
445 tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer,
446 parent);
447
448 if (!bt_ctf_field_validate(variant->tag)) {
449 goto end;
450 }
451
452 tag_enum_value = tag_enum_integer->definition.value._signed;
453 field_type = bt_ctf_field_type_variant_get_field_type(variant_type,
454 tag_enum_value);
455 if (!field_type) {
456 goto end;
457 }
458
459 new_field = bt_ctf_field_create(field_type);
460 if (!new_field) {
461 goto end;
462 }
463
464 bt_ctf_field_put(variant->tag);
465 bt_ctf_field_put(variant->payload);
466 bt_ctf_field_get(new_field);
467 bt_ctf_field_get(tag_field);
468 variant->tag = tag_field;
469 variant->payload = new_field;
470end:
471 bt_ctf_field_put(tag_enum);
472 return new_field;
473}
474
475struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
476 struct bt_ctf_field *field)
477{
478 struct bt_ctf_field *container = NULL;
479 struct bt_ctf_field_enumeration *enumeration;
480
481 if (!field) {
482 goto end;
483 }
484
485 enumeration = container_of(field, struct bt_ctf_field_enumeration,
486 parent);
487 if (!enumeration->payload) {
488 struct bt_ctf_field_type_enumeration *enumeration_type =
489 container_of(field->type,
490 struct bt_ctf_field_type_enumeration, parent);
491 enumeration->payload =
492 bt_ctf_field_create(enumeration_type->container);
493 }
494
495 container = enumeration->payload;
496 bt_ctf_field_get(container);
497end:
498 return container;
499}
500
501int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
502 int64_t value)
503{
504 int ret = 0;
505 struct bt_ctf_field_integer *integer;
506 struct bt_ctf_field_type_integer *integer_type;
507 unsigned int size;
508 int64_t min_value, max_value;
509
510 if (!field ||
511 bt_ctf_field_type_get_type_id(field->type) !=
512 CTF_TYPE_INTEGER) {
513 ret = -1;
514 goto end;
515 }
516
517 integer = container_of(field, struct bt_ctf_field_integer, parent);
518 integer_type = container_of(field->type,
519 struct bt_ctf_field_type_integer, parent);
520 if (!integer_type->declaration.signedness) {
521 ret = -1;
522 goto end;
523 }
524
525 size = integer_type->declaration.len;
526 min_value = -((int64_t)1 << (size - 1));
527 max_value = ((int64_t)1 << (size - 1)) - 1;
528 if (value < min_value || value > max_value) {
529 ret = -1;
530 goto end;
531 }
532
533 integer->definition.value._signed = value;
534 integer->parent.payload_set = 1;
535end:
536 return ret;
537}
538
539int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
540 uint64_t value)
541{
542 int ret = 0;
543 struct bt_ctf_field_integer *integer;
544 struct bt_ctf_field_type_integer *integer_type;
545 unsigned int size;
546 uint64_t max_value;
547
548 if (!field ||
549 bt_ctf_field_type_get_type_id(field->type) !=
550 CTF_TYPE_INTEGER) {
551 ret = -1;
552 goto end;
553 }
554
555 integer = container_of(field, struct bt_ctf_field_integer, parent);
556 integer_type = container_of(field->type,
557 struct bt_ctf_field_type_integer, parent);
558 if (integer_type->declaration.signedness) {
559 ret = -1;
560 goto end;
561 }
562
563 size = integer_type->declaration.len;
564 max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1;
565 if (value > max_value) {
566 ret = -1;
567 goto end;
568 }
569
570 integer->definition.value._unsigned = value;
571 integer->parent.payload_set = 1;
572end:
573 return ret;
574}
575
576int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
577 double value)
578{
579 int ret = 0;
580 struct bt_ctf_field_floating_point *floating_point;
581
582 if (!field ||
583 bt_ctf_field_type_get_type_id(field->type) !=
584 CTF_TYPE_FLOAT) {
585 ret = -1;
586 goto end;
587 }
588 floating_point = container_of(field, struct bt_ctf_field_floating_point,
589 parent);
590 floating_point->definition.value = value;
591 floating_point->parent.payload_set = 1;
592end:
593 return ret;
594}
595
596int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
597 const char *value)
598{
599 int ret = 0;
600 struct bt_ctf_field_string *string;
601
602 if (!field || !value ||
603 bt_ctf_field_type_get_type_id(field->type) !=
604 CTF_TYPE_STRING) {
605 ret = -1;
606 goto end;
607 }
608
609 string = container_of(field, struct bt_ctf_field_string, parent);
610 if (string->payload) {
611 g_string_free(string->payload, TRUE);
612 }
613
614 string->payload = g_string_new(value);
615 string->parent.payload_set = 1;
616end:
617 return ret;
618}
619
620BT_HIDDEN
621int bt_ctf_field_validate(struct bt_ctf_field *field)
622{
623 int ret = 0;
624 enum ctf_type_id type_id;
625
626 if (!field) {
627 ret = -1;
628 goto end;
629 }
630
631 type_id = bt_ctf_field_type_get_type_id(field->type);
632 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
633 ret = -1;
634 goto end;
635 }
636
637 ret = field_validate_funcs[type_id](field);
638end:
639 return ret;
640}
641
642BT_HIDDEN
643int bt_ctf_field_serialize(struct bt_ctf_field *field,
644 struct ctf_stream_pos *pos)
645{
646 int ret = 0;
647 enum ctf_type_id type_id;
648
649 if (!field || !pos) {
650 ret = -1;
651 goto end;
652 }
653
654 type_id = bt_ctf_field_type_get_type_id(field->type);
655 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
656 ret = -1;
657 goto end;
658 }
659
660 ret = field_serialize_funcs[type_id](field, pos);
661end:
662 return ret;
663}
664
665static
666struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
667{
668 struct bt_ctf_field_type_integer *integer_type = container_of(type,
669 struct bt_ctf_field_type_integer, parent);
670 struct bt_ctf_field_integer *integer = g_new0(
671 struct bt_ctf_field_integer, 1);
672
673 if (integer) {
674 integer->definition.declaration = &integer_type->declaration;
675 }
676
677 return integer ? &integer->parent : NULL;
678}
679
680static
681struct bt_ctf_field *bt_ctf_field_enumeration_create(
682 struct bt_ctf_field_type *type)
683{
684 struct bt_ctf_field_enumeration *enumeration = g_new0(
685 struct bt_ctf_field_enumeration, 1);
686
687 return enumeration ? &enumeration->parent : NULL;
688}
689
690static
691struct bt_ctf_field *bt_ctf_field_floating_point_create(
692 struct bt_ctf_field_type *type)
693{
694 struct bt_ctf_field_floating_point *floating_point;
695 struct bt_ctf_field_type_floating_point *floating_point_type;
696
697 floating_point = g_new0(struct bt_ctf_field_floating_point, 1);
698 if (!floating_point) {
699 goto end;
700 }
701
702 floating_point_type = container_of(type,
703 struct bt_ctf_field_type_floating_point, parent);
704 floating_point->definition.declaration = container_of(
705 type->declaration, struct declaration_float, p);
706
707
708 floating_point->definition.sign = &floating_point->sign;
709 floating_point->sign.declaration = &floating_point_type->sign;
710 floating_point->definition.sign->p.declaration =
711 &floating_point_type->sign.p;
712
713 floating_point->definition.mantissa = &floating_point->mantissa;
714 floating_point->mantissa.declaration = &floating_point_type->mantissa;
715 floating_point->definition.mantissa->p.declaration =
716 &floating_point_type->mantissa.p;
717
718 floating_point->definition.exp = &floating_point->exp;
719 floating_point->exp.declaration = &floating_point_type->exp;
720 floating_point->definition.exp->p.declaration =
721 &floating_point_type->exp.p;
722
723end:
724 return floating_point ? &floating_point->parent : NULL;
725}
726
727static
728struct bt_ctf_field *bt_ctf_field_structure_create(
729 struct bt_ctf_field_type *type)
730{
731 struct bt_ctf_field_type_structure *structure_type = container_of(type,
732 struct bt_ctf_field_type_structure, parent);
733 struct bt_ctf_field_structure *structure = g_new0(
734 struct bt_ctf_field_structure, 1);
735 struct bt_ctf_field *field = NULL;
736
737 if (!structure || !structure_type->fields->len) {
738 goto end;
739 }
740
741 structure->field_name_to_index = structure_type->field_name_to_index;
742 structure->fields = g_ptr_array_new_with_free_func(
743 (GDestroyNotify)bt_ctf_field_put);
744 g_ptr_array_set_size(structure->fields,
745 g_hash_table_size(structure->field_name_to_index));
746 field = &structure->parent;
747end:
748 return field;
749}
750
751static
752struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
753{
754 struct bt_ctf_field_variant *variant = g_new0(
755 struct bt_ctf_field_variant, 1);
756 return variant ? &variant->parent : NULL;
757}
758
759static
760struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
761{
762 struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1);
763 struct bt_ctf_field_type_array *array_type;
764 unsigned int array_length;
765
766 if (!array || !type) {
767 goto error;
768 }
769
770 array_type = container_of(type, struct bt_ctf_field_type_array, parent);
771 array_length = array_type->length;
fe0fe95c 772 array->elements = g_ptr_array_sized_new(array_length);
273b65be
JG
773 if (!array->elements) {
774 goto error;
775 }
776
fe0fe95c
JG
777 g_ptr_array_set_free_func(array->elements,
778 (GDestroyNotify)bt_ctf_field_put);
273b65be
JG
779 g_ptr_array_set_size(array->elements, array_length);
780 return &array->parent;
781error:
782 g_free(array);
783 return NULL;
784}
785
786static
787struct bt_ctf_field *bt_ctf_field_sequence_create(
788 struct bt_ctf_field_type *type)
789{
790 struct bt_ctf_field_sequence *sequence = g_new0(
791 struct bt_ctf_field_sequence, 1);
792 return sequence ? &sequence->parent : NULL;
793}
794
795static
796struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
797{
798 struct bt_ctf_field_string *string = g_new0(
799 struct bt_ctf_field_string, 1);
800 return string ? &string->parent : NULL;
801}
802
803static
804void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
805{
806 struct bt_ctf_field *field;
807 struct bt_ctf_field_type *type;
808 enum ctf_type_id type_id;
809
810 if (!ref) {
811 return;
812 }
813
814 field = container_of(ref, struct bt_ctf_field, ref_count);
815 type = field->type;
816 type_id = bt_ctf_field_type_get_type_id(type);
817 if (type_id <= CTF_TYPE_UNKNOWN ||
818 type_id >= NR_CTF_TYPES) {
819 return;
820 }
821
822 field_destroy_funcs[type_id](field);
823 if (type) {
824 bt_ctf_field_type_put(type);
825 }
826}
827
828static
829void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
830{
831 struct bt_ctf_field_integer *integer;
832
833 if (!field) {
834 return;
835 }
836
837 integer = container_of(field, struct bt_ctf_field_integer, parent);
838 g_free(integer);
839}
840
841static
842void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field)
843{
844 struct bt_ctf_field_enumeration *enumeration;
845
846 if (!field) {
847 return;
848 }
849
850 enumeration = container_of(field, struct bt_ctf_field_enumeration,
851 parent);
852 bt_ctf_field_put(enumeration->payload);
853 g_free(enumeration);
854}
855
856static
857void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
858{
859 struct bt_ctf_field_floating_point *floating_point;
860
861 if (!field) {
862 return;
863 }
864
865 floating_point = container_of(field, struct bt_ctf_field_floating_point,
866 parent);
867 g_free(floating_point);
868}
869
870static
871void bt_ctf_field_structure_destroy(struct bt_ctf_field *field)
872{
873 struct bt_ctf_field_structure *structure;
874
875 if (!field) {
876 return;
877 }
878
879 structure = container_of(field, struct bt_ctf_field_structure, parent);
880 g_ptr_array_free(structure->fields, TRUE);
881 g_free(structure);
882}
883
884static
885void bt_ctf_field_variant_destroy(struct bt_ctf_field *field)
886{
887 struct bt_ctf_field_variant *variant;
888
889 if (!field) {
890 return;
891 }
892
893 variant = container_of(field, struct bt_ctf_field_variant, parent);
894 bt_ctf_field_put(variant->tag);
895 bt_ctf_field_put(variant->payload);
896 g_free(variant);
897}
898
899static
900void bt_ctf_field_array_destroy(struct bt_ctf_field *field)
901{
902 struct bt_ctf_field_array *array;
903
904 if (!field) {
905 return;
906 }
907
908 array = container_of(field, struct bt_ctf_field_array, parent);
909 g_ptr_array_free(array->elements, TRUE);
910 g_free(array);
911}
912
913static
914void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
915{
916 struct bt_ctf_field_sequence *sequence;
917
918 if (!field) {
919 return;
920 }
921
922 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
923 g_ptr_array_free(sequence->elements, TRUE);
924 bt_ctf_field_put(sequence->length);
925 g_free(sequence);
926}
927
928static
929void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
930{
931 struct bt_ctf_field_string *string;
932 if (!field) {
933 return;
934 }
935
936 string = container_of(field, struct bt_ctf_field_string, parent);
937 g_string_free(string->payload, TRUE);
938 g_free(string);
939}
940
941static
942int bt_ctf_field_generic_validate(struct bt_ctf_field *field)
943{
944 return !(field && field->payload_set);
945}
946
947static
948int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field)
949{
950 int ret;
951 struct bt_ctf_field_enumeration *enumeration;
952
953 if (!field) {
954 ret = -1;
955 goto end;
956 }
957
958 enumeration = container_of(field, struct bt_ctf_field_enumeration,
959 parent);
960 if (!enumeration->payload) {
961 ret = -1;
962 goto end;
963 }
964
965 ret = bt_ctf_field_validate(enumeration->payload);
966end:
967 return ret;
968}
969
970static
971int bt_ctf_field_structure_validate(struct bt_ctf_field *field)
972{
973 size_t i;
974 int ret = 0;
975 struct bt_ctf_field_structure *structure;
976
977 if (!field) {
978 ret = -1;
979 goto end;
980 }
981
982 structure = container_of(field, struct bt_ctf_field_structure, parent);
983 for (i = 0; i < structure->fields->len; i++) {
984 ret = bt_ctf_field_validate(structure->fields->pdata[i]);
985 if (ret) {
986 goto end;
987 }
988 }
989end:
990 return ret;
991}
992
993static
994int bt_ctf_field_variant_validate(struct bt_ctf_field *field)
995{
996 int ret = 0;
997 struct bt_ctf_field_variant *variant;
998
999 if (!field) {
1000 ret = -1;
1001 goto end;
1002 }
1003
1004 variant = container_of(field, struct bt_ctf_field_variant, parent);
1005 ret = bt_ctf_field_validate(variant->payload);
1006end:
1007 return ret;
1008}
1009
1010static
1011int bt_ctf_field_array_validate(struct bt_ctf_field *field)
1012{
1013 size_t i;
1014 int ret = 0;
1015 struct bt_ctf_field_array *array;
1016
1017 if (!field) {
1018 ret = -1;
1019 goto end;
1020 }
1021
1022 array = container_of(field, struct bt_ctf_field_array, parent);
1023 for (i = 0; i < array->elements->len; i++) {
1024 ret = bt_ctf_field_validate(array->elements->pdata[i]);
1025 if (ret) {
1026 goto end;
1027 }
1028 }
1029end:
1030 return ret;
1031}
1032
1033static
1034int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
1035{
1036 size_t i;
1037 int ret = 0;
1038 struct bt_ctf_field_sequence *sequence;
1039
1040 if (!field) {
1041 ret = -1;
1042 goto end;
1043 }
1044
1045 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1046 for (i = 0; i < sequence->elements->len; i++) {
1047 ret = bt_ctf_field_validate(sequence->elements->pdata[i]);
1048 if (ret) {
1049 goto end;
1050 }
1051 }
1052end:
1053 return ret;
1054}
1055
1056static
1057int bt_ctf_field_integer_serialize(struct bt_ctf_field *field,
1058 struct ctf_stream_pos *pos)
1059{
1060 int ret = 0;
1061 struct bt_ctf_field_integer *integer = container_of(field,
1062 struct bt_ctf_field_integer, parent);
1063
1064retry:
1065 ret = ctf_integer_write(&pos->parent, &integer->definition.p);
1066 if (ret == -EFAULT) {
1067 /*
1068 * The field is too large to fit in the current packet's
1069 * remaining space. Bump the packet size and retry.
1070 */
1071 ret = increase_packet_size(pos);
1072 if (ret) {
1073 goto end;
1074 }
1075 goto retry;
1076 }
1077end:
1078 return ret;
1079}
1080
1081static
1082int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field,
1083 struct ctf_stream_pos *pos)
1084{
1085 struct bt_ctf_field_enumeration *enumeration = container_of(
1086 field, struct bt_ctf_field_enumeration, parent);
1087
1088 return bt_ctf_field_serialize(enumeration->payload, pos);
1089}
1090
1091static
1092int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field,
1093 struct ctf_stream_pos *pos)
1094{
1095 int ret = 0;
1096 struct bt_ctf_field_floating_point *floating_point = container_of(field,
1097 struct bt_ctf_field_floating_point, parent);
1098
1099retry:
1100 ret = ctf_float_write(&pos->parent, &floating_point->definition.p);
1101 if (ret == -EFAULT) {
1102 /*
1103 * The field is too large to fit in the current packet's
1104 * remaining space. Bump the packet size and retry.
1105 */
1106 ret = increase_packet_size(pos);
1107 if (ret) {
1108 goto end;
1109 }
1110 goto retry;
1111 }
1112end:
1113 return ret;
1114}
1115
1116static
1117int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
1118 struct ctf_stream_pos *pos)
1119{
1120 size_t i;
1121 int ret = 0;
1122 struct bt_ctf_field_structure *structure = container_of(
1123 field, struct bt_ctf_field_structure, parent);
1124
1125 while (!ctf_pos_access_ok(pos,
1126 offset_align(pos->offset,
1127 field->type->declaration->alignment))) {
9f56e450
JG
1128 ret = increase_packet_size(pos);
1129 if (ret) {
1130 goto end;
1131 }
273b65be
JG
1132 }
1133
1134 ctf_align_pos(pos, field->type->declaration->alignment);
1135
1136 for (i = 0; i < structure->fields->len; i++) {
1137 struct bt_ctf_field *field = g_ptr_array_index(
1138 structure->fields, i);
1139
1140 ret = bt_ctf_field_serialize(field, pos);
1141 if (ret) {
1142 break;
1143 }
1144 }
9f56e450 1145end:
273b65be
JG
1146 return ret;
1147}
1148
1149static
1150int bt_ctf_field_variant_serialize(struct bt_ctf_field *field,
1151 struct ctf_stream_pos *pos)
1152{
1153 struct bt_ctf_field_variant *variant = container_of(
1154 field, struct bt_ctf_field_variant, parent);
1155
1156 return bt_ctf_field_serialize(variant->payload, pos);
1157}
1158
1159static
1160int bt_ctf_field_array_serialize(struct bt_ctf_field *field,
1161 struct ctf_stream_pos *pos)
1162{
1163 size_t i;
1164 int ret = 0;
1165 struct bt_ctf_field_array *array = container_of(
1166 field, struct bt_ctf_field_array, parent);
1167
1168 for (i = 0; i < array->elements->len; i++) {
1169 ret = bt_ctf_field_serialize(
1170 g_ptr_array_index(array->elements, i), pos);
1171 if (ret) {
1172 goto end;
1173 }
1174 }
1175end:
1176 return ret;
1177}
1178
1179static
1180int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field,
1181 struct ctf_stream_pos *pos)
1182{
1183 size_t i;
1184 int ret = 0;
1185 struct bt_ctf_field_sequence *sequence = container_of(
1186 field, struct bt_ctf_field_sequence, parent);
1187
1188 for (i = 0; i < sequence->elements->len; i++) {
1189 ret = bt_ctf_field_serialize(
1190 g_ptr_array_index(sequence->elements, i), pos);
1191 if (ret) {
1192 goto end;
1193 }
1194 }
1195end:
1196 return ret;
1197}
1198
1199static
1200int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
1201 struct ctf_stream_pos *pos)
1202{
1203 size_t i;
1204 int ret = 0;
1205 struct bt_ctf_field_string *string = container_of(field,
1206 struct bt_ctf_field_string, parent);
1207 struct bt_ctf_field_type *character_type =
1208 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1209 struct bt_ctf_field *character = bt_ctf_field_create(character_type);
1210
1211 for (i = 0; i < string->payload->len + 1; i++) {
1212 ret = bt_ctf_field_unsigned_integer_set_value(character,
1213 (uint64_t) string->payload->str[i]);
1214 if (ret) {
1215 goto end;
1216 }
1217
1218 ret = bt_ctf_field_integer_serialize(character, pos);
1219 if (ret) {
1220 goto end;
1221 }
1222 }
1223end:
1224 bt_ctf_field_put(character);
1225 bt_ctf_field_type_put(character_type);
1226 return ret;
1227}
1228
1229static
1230int increase_packet_size(struct ctf_stream_pos *pos)
1231{
1232 int ret;
1233
1234 assert(pos);
1235 ret = munmap_align(pos->base_mma);
1236 if (ret) {
1237 goto end;
1238 }
1239
1240 pos->packet_size += PACKET_LEN_INCREMENT;
1241 ret = posix_fallocate(pos->fd, pos->mmap_offset,
1242 pos->packet_size / CHAR_BIT);
1243 if (ret) {
1244 goto end;
1245 }
1246
1247 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
1248 pos->flags, pos->fd, pos->mmap_offset);
1249 if (pos->base_mma == MAP_FAILED) {
1250 ret = -1;
1251 }
1252end:
1253 return ret;
1254}
This page took 0.067887 seconds and 4 git commands to generate.