Fix: return event timestamp in _timestamp_at_pos
[babeltrace.git] / formats / ctf / ir / event-fields.c
CommitLineData
273b65be
JG
1/*
2 * event-fields.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Event Fields
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/ctf-writer/event-fields.h>
adc315b8
JG
30#include <babeltrace/ctf-ir/event-fields-internal.h>
31#include <babeltrace/ctf-ir/event-types-internal.h>
273b65be
JG
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
ca116246 79int bt_ctf_field_generic_validate(struct bt_ctf_field *);
273b65be 80static
ca116246 81int bt_ctf_field_structure_validate(struct bt_ctf_field *);
273b65be 82static
ca116246 83int bt_ctf_field_variant_validate(struct bt_ctf_field *);
273b65be 84static
ca116246 85int bt_ctf_field_enumeration_validate(struct bt_ctf_field *);
273b65be 86static
ca116246 87int bt_ctf_field_array_validate(struct bt_ctf_field *);
273b65be 88static
ca116246 89int bt_ctf_field_sequence_validate(struct bt_ctf_field *);
273b65be 90
12c8a1a3 91static
ca116246 92int bt_ctf_field_generic_reset(struct bt_ctf_field *);
12c8a1a3 93static
ca116246 94int bt_ctf_field_structure_reset(struct bt_ctf_field *);
12c8a1a3 95static
ca116246 96int bt_ctf_field_variant_reset(struct bt_ctf_field *);
12c8a1a3 97static
ca116246 98int bt_ctf_field_enumeration_reset(struct bt_ctf_field *);
12c8a1a3 99static
ca116246 100int bt_ctf_field_array_reset(struct bt_ctf_field *);
12c8a1a3 101static
ca116246 102int bt_ctf_field_sequence_reset(struct bt_ctf_field *);
12c8a1a3 103static
ca116246 104int bt_ctf_field_string_reset(struct bt_ctf_field *);
12c8a1a3 105
273b65be
JG
106static
107int bt_ctf_field_integer_serialize(struct bt_ctf_field *,
108 struct ctf_stream_pos *);
109static
110int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *,
111 struct ctf_stream_pos *);
112static
113int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *,
114 struct ctf_stream_pos *);
115static
116int bt_ctf_field_structure_serialize(struct bt_ctf_field *,
117 struct ctf_stream_pos *);
118static
119int bt_ctf_field_variant_serialize(struct bt_ctf_field *,
120 struct ctf_stream_pos *);
121static
122int bt_ctf_field_array_serialize(struct bt_ctf_field *,
123 struct ctf_stream_pos *);
124static
125int bt_ctf_field_sequence_serialize(struct bt_ctf_field *,
126 struct ctf_stream_pos *);
127static
128int bt_ctf_field_string_serialize(struct bt_ctf_field *,
129 struct ctf_stream_pos *);
130
87d43dc1
JG
131static
132int bt_ctf_field_integer_copy(struct bt_ctf_field *, struct bt_ctf_field *);
133static
134int bt_ctf_field_enumeration_copy(struct bt_ctf_field *, struct bt_ctf_field *);
135static
136int bt_ctf_field_floating_point_copy(struct bt_ctf_field *,
137 struct bt_ctf_field *);
138static
139int bt_ctf_field_structure_copy(struct bt_ctf_field *, struct bt_ctf_field *);
140static
141int bt_ctf_field_variant_copy(struct bt_ctf_field *, struct bt_ctf_field *);
142static
143int bt_ctf_field_array_copy(struct bt_ctf_field *, struct bt_ctf_field *);
144static
145int bt_ctf_field_sequence_copy(struct bt_ctf_field *, struct bt_ctf_field *);
146static
147int bt_ctf_field_string_copy(struct bt_ctf_field *, struct bt_ctf_field *);
148
273b65be
JG
149static
150int increase_packet_size(struct ctf_stream_pos *pos);
151
152static
ce6da4fd 153struct bt_ctf_field *(* const field_create_funcs[])(
273b65be
JG
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
166static
ce6da4fd 167void (* const field_destroy_funcs[])(struct bt_ctf_field *) = {
273b65be
JG
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
179static
ce6da4fd 180int (* const field_validate_funcs[])(struct bt_ctf_field *) = {
273b65be
JG
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
12c8a1a3 191static
ce6da4fd 192int (* const field_reset_funcs[])(struct bt_ctf_field *) = {
12c8a1a3
JG
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
273b65be 203static
ce6da4fd 204int (* const field_serialize_funcs[])(struct bt_ctf_field *,
273b65be
JG
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
87d43dc1 217static
ce6da4fd
JG
218int (* const field_copy_funcs[])(struct bt_ctf_field *,
219 struct bt_ctf_field *) = {
87d43dc1
JG
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
273b65be
JG
230struct 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);
9ce21c30
JG
240 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES ||
241 bt_ctf_field_type_validate(type)) {
273b65be
JG
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;
255error:
256 return field;
257}
258
259void bt_ctf_field_get(struct bt_ctf_field *field)
260{
261 if (field) {
262 bt_ctf_ref_get(&field->ref_count);
263 }
264}
265
266void 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
cd95e351
JG
273struct 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);
283end:
284 return ret;
285}
286
287struct 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);
305end:
306 return ret;
307}
308
273b65be
JG
309int 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);
152e7331 330 /* The length field must be unsigned */
273b65be
JG
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
fe0fe95c 345 sequence->elements = g_ptr_array_sized_new((size_t)sequence_length);
273b65be
JG
346 if (!sequence->elements) {
347 ret = -1;
348 goto end;
349 }
350
fe0fe95c
JG
351 g_ptr_array_set_free_func(sequence->elements,
352 (GDestroyNotify)bt_ctf_field_put);
273b65be
JG
353 g_ptr_array_set_size(sequence->elements, (size_t)sequence_length);
354 bt_ctf_field_get(length_field);
355 sequence->length = length_field;
356end:
357 return ret;
358}
359
360struct 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;
b92ddaaa 366 struct bt_ctf_field_type *field_type = NULL;
273b65be
JG
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);
b92ddaaa
JG
377 field_type =
378 bt_ctf_field_type_structure_get_field_type_by_name(field->type,
379 name);
273b65be
JG
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;
396end:
397 bt_ctf_field_get(new_field);
398error:
b92ddaaa
JG
399 if (field_type) {
400 bt_ctf_field_type_put(field_type);
401 }
273b65be
JG
402 return new_field;
403}
404
cd95e351 405struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
074ee56d 406 struct bt_ctf_field *field, int index)
cd95e351
JG
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;
449end:
450 bt_ctf_field_get(ret_field);
451error:
452 if (field_type) {
453 bt_ctf_field_type_put(field_type);
454 }
455 return ret_field;
456}
457
273b65be
JG
458BT_HIDDEN
459int 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;
b92ddaaa 465 struct bt_ctf_field_type *expected_field_type = NULL;
273b65be
JG
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);
b92ddaaa
JG
477 expected_field_type =
478 bt_ctf_field_type_structure_get_field_type_by_name(field->type,
479 name);
273b65be
JG
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);
496end:
b92ddaaa
JG
497 if (expected_field_type) {
498 bt_ctf_field_type_put(expected_field_type);
499 }
273b65be
JG
500 return ret;
501}
502
503struct 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;
b92ddaaa 507 struct bt_ctf_field_type *field_type = NULL;
273b65be 508 struct bt_ctf_field_array *array;
273b65be
JG
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
b92ddaaa 520 field_type = bt_ctf_field_type_array_get_element_type(field->type);
273b65be
JG
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 bt_ctf_field_get(new_field);
528 array->elements->pdata[(size_t)index] = new_field;
529end:
b92ddaaa
JG
530 if (field_type) {
531 bt_ctf_field_type_put(field_type);
532 }
273b65be
JG
533 return new_field;
534}
535
536struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
537 uint64_t index)
538{
539 struct bt_ctf_field *new_field = NULL;
b92ddaaa 540 struct bt_ctf_field_type *field_type = NULL;
273b65be 541 struct bt_ctf_field_sequence *sequence;
273b65be
JG
542
543 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
544 CTF_TYPE_SEQUENCE) {
545 goto end;
546 }
547
548 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
549 if (!sequence->elements || sequence->elements->len <= index) {
550 goto end;
551 }
552
b92ddaaa 553 field_type = bt_ctf_field_type_sequence_get_element_type(field->type);
273b65be
JG
554 if (sequence->elements->pdata[(size_t)index]) {
555 new_field = sequence->elements->pdata[(size_t)index];
556 goto end;
557 }
558
559 new_field = bt_ctf_field_create(field_type);
560 bt_ctf_field_get(new_field);
561 sequence->elements->pdata[(size_t)index] = new_field;
562end:
b92ddaaa
JG
563 if (field_type) {
564 bt_ctf_field_type_put(field_type);
565 }
273b65be
JG
566 return new_field;
567}
568
569struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
570 struct bt_ctf_field *tag_field)
571{
572 struct bt_ctf_field *new_field = NULL;
573 struct bt_ctf_field_variant *variant;
574 struct bt_ctf_field_type_variant *variant_type;
575 struct bt_ctf_field_type *field_type;
576 struct bt_ctf_field *tag_enum = NULL;
577 struct bt_ctf_field_integer *tag_enum_integer;
578 int64_t tag_enum_value;
579
580 if (!field || !tag_field ||
581 bt_ctf_field_type_get_type_id(field->type) !=
582 CTF_TYPE_VARIANT ||
583 bt_ctf_field_type_get_type_id(tag_field->type) !=
584 CTF_TYPE_ENUM) {
585 goto end;
586 }
587
588 variant = container_of(field, struct bt_ctf_field_variant, parent);
589 variant_type = container_of(field->type,
590 struct bt_ctf_field_type_variant, parent);
591 tag_enum = bt_ctf_field_enumeration_get_container(tag_field);
592 if (!tag_enum) {
593 goto end;
594 }
595
596 tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer,
597 parent);
598
2c661a4d 599 if (bt_ctf_field_validate(tag_field) < 0) {
273b65be
JG
600 goto end;
601 }
602
603 tag_enum_value = tag_enum_integer->definition.value._signed;
b92ddaaa
JG
604 field_type = bt_ctf_field_type_variant_get_field_type_signed(
605 variant_type, tag_enum_value);
273b65be
JG
606 if (!field_type) {
607 goto end;
608 }
609
610 new_field = bt_ctf_field_create(field_type);
611 if (!new_field) {
612 goto end;
613 }
614
615 bt_ctf_field_put(variant->tag);
616 bt_ctf_field_put(variant->payload);
617 bt_ctf_field_get(new_field);
618 bt_ctf_field_get(tag_field);
619 variant->tag = tag_field;
620 variant->payload = new_field;
621end:
622 bt_ctf_field_put(tag_enum);
623 return new_field;
624}
625
626struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
627 struct bt_ctf_field *field)
628{
629 struct bt_ctf_field *container = NULL;
630 struct bt_ctf_field_enumeration *enumeration;
631
b92ddaaa
JG
632 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
633 CTF_TYPE_ENUM) {
273b65be
JG
634 goto end;
635 }
636
637 enumeration = container_of(field, struct bt_ctf_field_enumeration,
638 parent);
639 if (!enumeration->payload) {
640 struct bt_ctf_field_type_enumeration *enumeration_type =
641 container_of(field->type,
642 struct bt_ctf_field_type_enumeration, parent);
643 enumeration->payload =
644 bt_ctf_field_create(enumeration_type->container);
645 }
646
647 container = enumeration->payload;
648 bt_ctf_field_get(container);
649end:
650 return container;
651}
652
cd95e351
JG
653const char *bt_ctf_field_enumeration_get_mapping_name(
654 struct bt_ctf_field *field)
655{
656 int ret;
657 const char *name = NULL;
658 struct bt_ctf_field *container = NULL;
659 struct bt_ctf_field_type *container_type = NULL;
660 struct bt_ctf_field_type_integer *integer_type = NULL;
661 struct bt_ctf_field_type_enumeration *enumeration_type = NULL;
662
663 container = bt_ctf_field_enumeration_get_container(field);
664 if (!container) {
665 goto end;
666 }
667
668 container_type = bt_ctf_field_get_type(container);
669 if (!container_type) {
670 goto error_put_container;
671 }
672
673 integer_type = container_of(container_type,
674 struct bt_ctf_field_type_integer, parent);
675 enumeration_type = container_of(field->type,
676 struct bt_ctf_field_type_enumeration, parent);
677
10817e06 678 if (!integer_type->declaration.signedness) {
cd95e351
JG
679 uint64_t value;
680 ret = bt_ctf_field_unsigned_integer_get_value(container,
681 &value);
682 if (ret) {
683 goto error_put_container_type;
684 }
685
686 name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
687 enumeration_type, value);
688 } else {
689 int64_t value;
690 ret = bt_ctf_field_signed_integer_get_value(container,
691 &value);
692 if (ret) {
693 goto error_put_container_type;
694 }
695
696 name = bt_ctf_field_type_enumeration_get_mapping_name_signed(
697 enumeration_type, value);
698 }
699
700error_put_container_type:
701 bt_ctf_field_type_put(container_type);
702error_put_container:
703 bt_ctf_field_put(container);
704end:
705 return name;
706}
707
708int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field,
709 int64_t *value)
710{
711 int ret = 0;
712 struct bt_ctf_field_integer *integer;
713 struct bt_ctf_field_type_integer *integer_type;
714
715 if (!field || !value || !field->payload_set ||
716 bt_ctf_field_type_get_type_id(field->type) !=
717 CTF_TYPE_INTEGER) {
718 ret = -1;
719 goto end;
720 }
721
722 integer_type = container_of(field->type,
723 struct bt_ctf_field_type_integer, parent);
724 if (!integer_type->declaration.signedness) {
725 ret = -1;
726 goto end;
727 }
728
729 integer = container_of(field,
730 struct bt_ctf_field_integer, parent);
731 *value = integer->definition.value._signed;
732end:
733 return ret;
734}
735
273b65be
JG
736int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
737 int64_t value)
738{
739 int ret = 0;
740 struct bt_ctf_field_integer *integer;
741 struct bt_ctf_field_type_integer *integer_type;
742 unsigned int size;
743 int64_t min_value, max_value;
744
745 if (!field ||
746 bt_ctf_field_type_get_type_id(field->type) !=
747 CTF_TYPE_INTEGER) {
748 ret = -1;
749 goto end;
750 }
751
752 integer = container_of(field, struct bt_ctf_field_integer, parent);
753 integer_type = container_of(field->type,
754 struct bt_ctf_field_type_integer, parent);
755 if (!integer_type->declaration.signedness) {
756 ret = -1;
757 goto end;
758 }
759
760 size = integer_type->declaration.len;
761 min_value = -((int64_t)1 << (size - 1));
762 max_value = ((int64_t)1 << (size - 1)) - 1;
763 if (value < min_value || value > max_value) {
764 ret = -1;
765 goto end;
766 }
767
768 integer->definition.value._signed = value;
769 integer->parent.payload_set = 1;
770end:
771 return ret;
772}
773
cd95e351
JG
774int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field,
775 uint64_t *value)
776{
777 int ret = 0;
778 struct bt_ctf_field_integer *integer;
779 struct bt_ctf_field_type_integer *integer_type;
780
781 if (!field || !value || !field->payload_set ||
782 bt_ctf_field_type_get_type_id(field->type) !=
783 CTF_TYPE_INTEGER) {
784 ret = -1;
785 goto end;
786 }
787
788 integer_type = container_of(field->type,
789 struct bt_ctf_field_type_integer, parent);
790 if (integer_type->declaration.signedness) {
791 ret = -1;
792 goto end;
793 }
794
795 integer = container_of(field,
796 struct bt_ctf_field_integer, parent);
797 *value = integer->definition.value._unsigned;
798end:
799 return ret;
800}
801
273b65be
JG
802int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
803 uint64_t value)
804{
805 int ret = 0;
806 struct bt_ctf_field_integer *integer;
807 struct bt_ctf_field_type_integer *integer_type;
808 unsigned int size;
809 uint64_t max_value;
810
811 if (!field ||
812 bt_ctf_field_type_get_type_id(field->type) !=
813 CTF_TYPE_INTEGER) {
814 ret = -1;
815 goto end;
816 }
817
818 integer = container_of(field, struct bt_ctf_field_integer, parent);
819 integer_type = container_of(field->type,
820 struct bt_ctf_field_type_integer, parent);
821 if (integer_type->declaration.signedness) {
822 ret = -1;
823 goto end;
824 }
825
826 size = integer_type->declaration.len;
827 max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1;
828 if (value > max_value) {
829 ret = -1;
830 goto end;
831 }
832
833 integer->definition.value._unsigned = value;
834 integer->parent.payload_set = 1;
835end:
836 return ret;
837}
838
cd95e351
JG
839int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field,
840 double *value)
841{
842 int ret = 0;
843 struct bt_ctf_field_floating_point *floating_point;
844
845 if (!field || !value || !field->payload_set ||
846 bt_ctf_field_type_get_type_id(field->type) !=
847 CTF_TYPE_FLOAT) {
848 ret = -1;
849 goto end;
850 }
851
852 floating_point = container_of(field,
853 struct bt_ctf_field_floating_point, parent);
854 *value = floating_point->definition.value;
855end:
856 return ret;
857}
858
273b65be
JG
859int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
860 double value)
861{
862 int ret = 0;
863 struct bt_ctf_field_floating_point *floating_point;
864
865 if (!field ||
866 bt_ctf_field_type_get_type_id(field->type) !=
867 CTF_TYPE_FLOAT) {
868 ret = -1;
869 goto end;
870 }
871 floating_point = container_of(field, struct bt_ctf_field_floating_point,
872 parent);
873 floating_point->definition.value = value;
874 floating_point->parent.payload_set = 1;
875end:
876 return ret;
877}
878
cd95e351
JG
879const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field)
880{
881 const char *ret = NULL;
882 struct bt_ctf_field_string *string;
883
884 if (!field || !field->payload_set ||
885 bt_ctf_field_type_get_type_id(field->type) !=
886 CTF_TYPE_STRING) {
887 goto end;
888 }
889
890 string = container_of(field,
891 struct bt_ctf_field_string, parent);
892 ret = string->payload->str;
893end:
894 return ret;
895}
896
273b65be
JG
897int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
898 const char *value)
899{
900 int ret = 0;
901 struct bt_ctf_field_string *string;
902
903 if (!field || !value ||
904 bt_ctf_field_type_get_type_id(field->type) !=
905 CTF_TYPE_STRING) {
906 ret = -1;
907 goto end;
908 }
909
910 string = container_of(field, struct bt_ctf_field_string, parent);
911 if (string->payload) {
97736814
JG
912 g_string_assign(string->payload, value);
913 } else {
914 string->payload = g_string_new(value);
273b65be
JG
915 }
916
273b65be
JG
917 string->parent.payload_set = 1;
918end:
919 return ret;
920}
921
922BT_HIDDEN
923int bt_ctf_field_validate(struct bt_ctf_field *field)
924{
925 int ret = 0;
926 enum ctf_type_id type_id;
927
928 if (!field) {
929 ret = -1;
930 goto end;
931 }
932
933 type_id = bt_ctf_field_type_get_type_id(field->type);
934 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
935 ret = -1;
936 goto end;
937 }
938
939 ret = field_validate_funcs[type_id](field);
940end:
941 return ret;
942}
943
12c8a1a3
JG
944BT_HIDDEN
945int bt_ctf_field_reset(struct bt_ctf_field *field)
946{
947 int ret = 0;
948 enum ctf_type_id type_id;
949
950 if (!field) {
951 ret = -1;
952 goto end;
953 }
954
955 type_id = bt_ctf_field_type_get_type_id(field->type);
956 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
957 ret = -1;
958 goto end;
959 }
960
961 ret = field_reset_funcs[type_id](field);
962end:
963 return ret;
964}
965
273b65be
JG
966BT_HIDDEN
967int bt_ctf_field_serialize(struct bt_ctf_field *field,
968 struct ctf_stream_pos *pos)
969{
970 int ret = 0;
971 enum ctf_type_id type_id;
972
973 if (!field || !pos) {
974 ret = -1;
975 goto end;
976 }
977
978 type_id = bt_ctf_field_type_get_type_id(field->type);
979 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
980 ret = -1;
981 goto end;
982 }
983
984 ret = field_serialize_funcs[type_id](field, pos);
985end:
986 return ret;
987}
988
87d43dc1
JG
989BT_HIDDEN
990struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
991{
992 int ret;
993 struct bt_ctf_field *copy = NULL;
994 enum ctf_type_id type_id;
995
996 if (!field) {
87d43dc1
JG
997 goto end;
998 }
999
1000 type_id = bt_ctf_field_type_get_type_id(field->type);
1001 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
87d43dc1
JG
1002 goto end;
1003 }
1004
1005 copy = bt_ctf_field_create(field->type);
1006 if (!copy) {
1007 goto end;
1008 }
1009
87d43dc1
JG
1010 ret = field_copy_funcs[type_id](field, copy);
1011 if (ret) {
1012 bt_ctf_field_put(copy);
1013 copy = NULL;
1014 }
1015end:
1016 return copy;
1017}
1018
273b65be
JG
1019static
1020struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
1021{
1022 struct bt_ctf_field_type_integer *integer_type = container_of(type,
1023 struct bt_ctf_field_type_integer, parent);
1024 struct bt_ctf_field_integer *integer = g_new0(
1025 struct bt_ctf_field_integer, 1);
1026
1027 if (integer) {
1028 integer->definition.declaration = &integer_type->declaration;
1029 }
1030
1031 return integer ? &integer->parent : NULL;
1032}
1033
1034static
1035struct bt_ctf_field *bt_ctf_field_enumeration_create(
1036 struct bt_ctf_field_type *type)
1037{
1038 struct bt_ctf_field_enumeration *enumeration = g_new0(
1039 struct bt_ctf_field_enumeration, 1);
1040
1041 return enumeration ? &enumeration->parent : NULL;
1042}
1043
1044static
1045struct bt_ctf_field *bt_ctf_field_floating_point_create(
1046 struct bt_ctf_field_type *type)
1047{
1048 struct bt_ctf_field_floating_point *floating_point;
1049 struct bt_ctf_field_type_floating_point *floating_point_type;
1050
1051 floating_point = g_new0(struct bt_ctf_field_floating_point, 1);
1052 if (!floating_point) {
1053 goto end;
1054 }
1055
1056 floating_point_type = container_of(type,
1057 struct bt_ctf_field_type_floating_point, parent);
1058 floating_point->definition.declaration = container_of(
1059 type->declaration, struct declaration_float, p);
1060
1061
1062 floating_point->definition.sign = &floating_point->sign;
1063 floating_point->sign.declaration = &floating_point_type->sign;
1064 floating_point->definition.sign->p.declaration =
1065 &floating_point_type->sign.p;
1066
1067 floating_point->definition.mantissa = &floating_point->mantissa;
1068 floating_point->mantissa.declaration = &floating_point_type->mantissa;
1069 floating_point->definition.mantissa->p.declaration =
1070 &floating_point_type->mantissa.p;
1071
1072 floating_point->definition.exp = &floating_point->exp;
1073 floating_point->exp.declaration = &floating_point_type->exp;
1074 floating_point->definition.exp->p.declaration =
1075 &floating_point_type->exp.p;
1076
1077end:
1078 return floating_point ? &floating_point->parent : NULL;
1079}
1080
1081static
1082struct bt_ctf_field *bt_ctf_field_structure_create(
1083 struct bt_ctf_field_type *type)
1084{
1085 struct bt_ctf_field_type_structure *structure_type = container_of(type,
1086 struct bt_ctf_field_type_structure, parent);
1087 struct bt_ctf_field_structure *structure = g_new0(
1088 struct bt_ctf_field_structure, 1);
1089 struct bt_ctf_field *field = NULL;
1090
1091 if (!structure || !structure_type->fields->len) {
1092 goto end;
1093 }
1094
1095 structure->field_name_to_index = structure_type->field_name_to_index;
1096 structure->fields = g_ptr_array_new_with_free_func(
1097 (GDestroyNotify)bt_ctf_field_put);
1098 g_ptr_array_set_size(structure->fields,
1099 g_hash_table_size(structure->field_name_to_index));
1100 field = &structure->parent;
1101end:
1102 return field;
1103}
1104
1105static
1106struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
1107{
1108 struct bt_ctf_field_variant *variant = g_new0(
1109 struct bt_ctf_field_variant, 1);
1110 return variant ? &variant->parent : NULL;
1111}
1112
1113static
1114struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
1115{
1116 struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1);
1117 struct bt_ctf_field_type_array *array_type;
1118 unsigned int array_length;
1119
1120 if (!array || !type) {
1121 goto error;
1122 }
1123
1124 array_type = container_of(type, struct bt_ctf_field_type_array, parent);
1125 array_length = array_type->length;
fe0fe95c 1126 array->elements = g_ptr_array_sized_new(array_length);
273b65be
JG
1127 if (!array->elements) {
1128 goto error;
1129 }
1130
fe0fe95c
JG
1131 g_ptr_array_set_free_func(array->elements,
1132 (GDestroyNotify)bt_ctf_field_put);
273b65be
JG
1133 g_ptr_array_set_size(array->elements, array_length);
1134 return &array->parent;
1135error:
1136 g_free(array);
1137 return NULL;
1138}
1139
1140static
1141struct bt_ctf_field *bt_ctf_field_sequence_create(
1142 struct bt_ctf_field_type *type)
1143{
1144 struct bt_ctf_field_sequence *sequence = g_new0(
1145 struct bt_ctf_field_sequence, 1);
1146 return sequence ? &sequence->parent : NULL;
1147}
1148
1149static
1150struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
1151{
1152 struct bt_ctf_field_string *string = g_new0(
1153 struct bt_ctf_field_string, 1);
1154 return string ? &string->parent : NULL;
1155}
1156
1157static
1158void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
1159{
1160 struct bt_ctf_field *field;
1161 struct bt_ctf_field_type *type;
1162 enum ctf_type_id type_id;
1163
1164 if (!ref) {
1165 return;
1166 }
1167
1168 field = container_of(ref, struct bt_ctf_field, ref_count);
1169 type = field->type;
1170 type_id = bt_ctf_field_type_get_type_id(type);
1171 if (type_id <= CTF_TYPE_UNKNOWN ||
1172 type_id >= NR_CTF_TYPES) {
1173 return;
1174 }
1175
1176 field_destroy_funcs[type_id](field);
1177 if (type) {
1178 bt_ctf_field_type_put(type);
1179 }
1180}
1181
1182static
1183void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
1184{
1185 struct bt_ctf_field_integer *integer;
1186
1187 if (!field) {
1188 return;
1189 }
1190
1191 integer = container_of(field, struct bt_ctf_field_integer, parent);
1192 g_free(integer);
1193}
1194
1195static
1196void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field)
1197{
1198 struct bt_ctf_field_enumeration *enumeration;
1199
1200 if (!field) {
1201 return;
1202 }
1203
1204 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1205 parent);
1206 bt_ctf_field_put(enumeration->payload);
1207 g_free(enumeration);
1208}
1209
1210static
1211void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
1212{
1213 struct bt_ctf_field_floating_point *floating_point;
1214
1215 if (!field) {
1216 return;
1217 }
1218
1219 floating_point = container_of(field, struct bt_ctf_field_floating_point,
1220 parent);
1221 g_free(floating_point);
1222}
1223
1224static
1225void bt_ctf_field_structure_destroy(struct bt_ctf_field *field)
1226{
1227 struct bt_ctf_field_structure *structure;
1228
1229 if (!field) {
1230 return;
1231 }
1232
1233 structure = container_of(field, struct bt_ctf_field_structure, parent);
1234 g_ptr_array_free(structure->fields, TRUE);
1235 g_free(structure);
1236}
1237
1238static
1239void bt_ctf_field_variant_destroy(struct bt_ctf_field *field)
1240{
1241 struct bt_ctf_field_variant *variant;
1242
1243 if (!field) {
1244 return;
1245 }
1246
1247 variant = container_of(field, struct bt_ctf_field_variant, parent);
1248 bt_ctf_field_put(variant->tag);
1249 bt_ctf_field_put(variant->payload);
1250 g_free(variant);
1251}
1252
1253static
1254void bt_ctf_field_array_destroy(struct bt_ctf_field *field)
1255{
1256 struct bt_ctf_field_array *array;
1257
1258 if (!field) {
1259 return;
1260 }
1261
1262 array = container_of(field, struct bt_ctf_field_array, parent);
1263 g_ptr_array_free(array->elements, TRUE);
1264 g_free(array);
1265}
1266
1267static
1268void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
1269{
1270 struct bt_ctf_field_sequence *sequence;
1271
1272 if (!field) {
1273 return;
1274 }
1275
1276 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1277 g_ptr_array_free(sequence->elements, TRUE);
1278 bt_ctf_field_put(sequence->length);
1279 g_free(sequence);
1280}
1281
1282static
1283void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
1284{
1285 struct bt_ctf_field_string *string;
1286 if (!field) {
1287 return;
1288 }
1289
1290 string = container_of(field, struct bt_ctf_field_string, parent);
9b2b7163
JG
1291 if (string->payload) {
1292 g_string_free(string->payload, TRUE);
1293 }
273b65be
JG
1294 g_free(string);
1295}
1296
1297static
1298int bt_ctf_field_generic_validate(struct bt_ctf_field *field)
1299{
da2f6971 1300 return (field && field->payload_set) ? 0 : -1;
273b65be
JG
1301}
1302
1303static
1304int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field)
1305{
1306 int ret;
1307 struct bt_ctf_field_enumeration *enumeration;
1308
1309 if (!field) {
1310 ret = -1;
1311 goto end;
1312 }
1313
1314 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1315 parent);
1316 if (!enumeration->payload) {
1317 ret = -1;
1318 goto end;
1319 }
1320
1321 ret = bt_ctf_field_validate(enumeration->payload);
1322end:
1323 return ret;
1324}
1325
1326static
1327int bt_ctf_field_structure_validate(struct bt_ctf_field *field)
1328{
1329 size_t i;
1330 int ret = 0;
1331 struct bt_ctf_field_structure *structure;
1332
1333 if (!field) {
1334 ret = -1;
1335 goto end;
1336 }
1337
1338 structure = container_of(field, struct bt_ctf_field_structure, parent);
1339 for (i = 0; i < structure->fields->len; i++) {
1340 ret = bt_ctf_field_validate(structure->fields->pdata[i]);
1341 if (ret) {
1342 goto end;
1343 }
1344 }
1345end:
1346 return ret;
1347}
1348
1349static
1350int bt_ctf_field_variant_validate(struct bt_ctf_field *field)
1351{
1352 int ret = 0;
1353 struct bt_ctf_field_variant *variant;
1354
1355 if (!field) {
1356 ret = -1;
1357 goto end;
1358 }
1359
1360 variant = container_of(field, struct bt_ctf_field_variant, parent);
1361 ret = bt_ctf_field_validate(variant->payload);
1362end:
1363 return ret;
1364}
1365
1366static
1367int bt_ctf_field_array_validate(struct bt_ctf_field *field)
1368{
1369 size_t i;
1370 int ret = 0;
1371 struct bt_ctf_field_array *array;
1372
1373 if (!field) {
1374 ret = -1;
1375 goto end;
1376 }
1377
1378 array = container_of(field, struct bt_ctf_field_array, parent);
1379 for (i = 0; i < array->elements->len; i++) {
1380 ret = bt_ctf_field_validate(array->elements->pdata[i]);
1381 if (ret) {
1382 goto end;
1383 }
1384 }
1385end:
1386 return ret;
1387}
1388
1389static
1390int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
1391{
1392 size_t i;
1393 int ret = 0;
1394 struct bt_ctf_field_sequence *sequence;
1395
1396 if (!field) {
1397 ret = -1;
1398 goto end;
1399 }
1400
1401 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1402 for (i = 0; i < sequence->elements->len; i++) {
1403 ret = bt_ctf_field_validate(sequence->elements->pdata[i]);
1404 if (ret) {
1405 goto end;
1406 }
1407 }
1408end:
1409 return ret;
1410}
1411
12c8a1a3
JG
1412static
1413int bt_ctf_field_generic_reset(struct bt_ctf_field *field)
1414{
1415 int ret = 0;
1416
1417 if (!field) {
1418 ret = -1;
1419 goto end;
1420 }
1421
1422 field->payload_set = 0;
1423end:
1424 return ret;
1425}
1426
1427static
1428int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field)
1429{
1430 int ret = 0;
1431 struct bt_ctf_field_enumeration *enumeration;
1432
1433 if (!field) {
1434 ret = -1;
1435 goto end;
1436 }
1437
1438 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1439 parent);
1440 if (!enumeration->payload) {
1441 goto end;
1442 }
1443
1444 ret = bt_ctf_field_reset(enumeration->payload);
1445end:
1446 return ret;
1447}
1448
1449static
1450int bt_ctf_field_structure_reset(struct bt_ctf_field *field)
1451{
1452 size_t i;
1453 int ret = 0;
1454 struct bt_ctf_field_structure *structure;
1455
1456 if (!field) {
1457 ret = -1;
1458 goto end;
1459 }
1460
1461 structure = container_of(field, struct bt_ctf_field_structure, parent);
1462 for (i = 0; i < structure->fields->len; i++) {
1463 struct bt_ctf_field *member = structure->fields->pdata[i];
1464
1465 if (!member) {
1466 /*
1467 * Structure members are lazily initialized; skip if
1468 * this member has not been allocated yet.
1469 */
1470 continue;
1471 }
1472
1473 ret = bt_ctf_field_reset(member);
1474 if (ret) {
1475 goto end;
1476 }
1477 }
1478end:
1479 return ret;
1480}
1481
1482static
1483int bt_ctf_field_variant_reset(struct bt_ctf_field *field)
1484{
1485 int ret = 0;
1486 struct bt_ctf_field_variant *variant;
1487
1488 if (!field) {
1489 ret = -1;
1490 goto end;
1491 }
1492
1493 variant = container_of(field, struct bt_ctf_field_variant, parent);
1494 if (variant->payload) {
1495 ret = bt_ctf_field_reset(variant->payload);
1496 }
1497end:
1498 return ret;
1499}
1500
1501static
1502int bt_ctf_field_array_reset(struct bt_ctf_field *field)
1503{
1504 size_t i;
1505 int ret = 0;
1506 struct bt_ctf_field_array *array;
1507
1508 if (!field) {
1509 ret = -1;
1510 goto end;
1511 }
1512
1513 array = container_of(field, struct bt_ctf_field_array, parent);
1514 for (i = 0; i < array->elements->len; i++) {
1515 struct bt_ctf_field *member = array->elements->pdata[i];
1516
1517 if (!member) {
1518 /*
1519 * Array elements are lazily initialized; skip if
1520 * this member has not been allocated yet.
1521 */
1522 continue;
1523 }
1524
1525 ret = bt_ctf_field_reset(member);
1526 if (ret) {
1527 goto end;
1528 }
1529 }
1530end:
1531 return ret;
1532}
1533
1534static
1535int bt_ctf_field_sequence_reset(struct bt_ctf_field *field)
1536{
1537 size_t i;
1538 int ret = 0;
1539 struct bt_ctf_field_sequence *sequence;
1540
1541 if (!field) {
1542 ret = -1;
1543 goto end;
1544 }
1545
1546 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1547 for (i = 0; i < sequence->elements->len; i++) {
1548 struct bt_ctf_field *member = sequence->elements->pdata[i];
1549
1550 if (!member) {
1551 /*
1552 * Sequence elements are lazily initialized; skip if
1553 * this member has not been allocated yet.
1554 */
1555 continue;
1556 }
1557
1558 ret = bt_ctf_field_reset(member);
1559 if (ret) {
1560 goto end;
1561 }
1562 }
1563end:
1564 return ret;
1565}
1566
1567static
1568int bt_ctf_field_string_reset(struct bt_ctf_field *field)
1569{
1570 int ret = 0;
1571 struct bt_ctf_field_string *string;
1572
1573 if (!field) {
1574 ret = -1;
1575 goto end;
1576 }
1577
1578 ret = bt_ctf_field_generic_reset(field);
1579 if (ret) {
1580 goto end;
1581 }
1582
1583 string = container_of(field, struct bt_ctf_field_string, parent);
1584 if (string->payload) {
1585 g_string_truncate(string->payload, 0);
1586 }
1587end:
1588 return ret;
1589}
1590
273b65be
JG
1591static
1592int bt_ctf_field_integer_serialize(struct bt_ctf_field *field,
1593 struct ctf_stream_pos *pos)
1594{
1595 int ret = 0;
1596 struct bt_ctf_field_integer *integer = container_of(field,
1597 struct bt_ctf_field_integer, parent);
1598
1599retry:
1600 ret = ctf_integer_write(&pos->parent, &integer->definition.p);
1601 if (ret == -EFAULT) {
1602 /*
1603 * The field is too large to fit in the current packet's
1604 * remaining space. Bump the packet size and retry.
1605 */
1606 ret = increase_packet_size(pos);
1607 if (ret) {
1608 goto end;
1609 }
1610 goto retry;
1611 }
1612end:
1613 return ret;
1614}
1615
1616static
1617int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field,
1618 struct ctf_stream_pos *pos)
1619{
1620 struct bt_ctf_field_enumeration *enumeration = container_of(
1621 field, struct bt_ctf_field_enumeration, parent);
1622
1623 return bt_ctf_field_serialize(enumeration->payload, pos);
1624}
1625
1626static
1627int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field,
1628 struct ctf_stream_pos *pos)
1629{
1630 int ret = 0;
1631 struct bt_ctf_field_floating_point *floating_point = container_of(field,
1632 struct bt_ctf_field_floating_point, parent);
1633
1634retry:
1635 ret = ctf_float_write(&pos->parent, &floating_point->definition.p);
1636 if (ret == -EFAULT) {
1637 /*
1638 * The field is too large to fit in the current packet's
1639 * remaining space. Bump the packet size and retry.
1640 */
1641 ret = increase_packet_size(pos);
1642 if (ret) {
1643 goto end;
1644 }
1645 goto retry;
1646 }
1647end:
1648 return ret;
1649}
1650
1651static
1652int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
1653 struct ctf_stream_pos *pos)
1654{
1655 size_t i;
1656 int ret = 0;
1657 struct bt_ctf_field_structure *structure = container_of(
1658 field, struct bt_ctf_field_structure, parent);
1659
1660 while (!ctf_pos_access_ok(pos,
1661 offset_align(pos->offset,
1662 field->type->declaration->alignment))) {
9f56e450
JG
1663 ret = increase_packet_size(pos);
1664 if (ret) {
1665 goto end;
1666 }
273b65be
JG
1667 }
1668
70fd5a51
MD
1669 if (!ctf_align_pos(pos, field->type->declaration->alignment)) {
1670 ret = -1;
1671 goto end;
1672 }
273b65be
JG
1673
1674 for (i = 0; i < structure->fields->len; i++) {
1675 struct bt_ctf_field *field = g_ptr_array_index(
1676 structure->fields, i);
1677
1678 ret = bt_ctf_field_serialize(field, pos);
1679 if (ret) {
1680 break;
1681 }
1682 }
9f56e450 1683end:
273b65be
JG
1684 return ret;
1685}
1686
1687static
1688int bt_ctf_field_variant_serialize(struct bt_ctf_field *field,
1689 struct ctf_stream_pos *pos)
1690{
1691 struct bt_ctf_field_variant *variant = container_of(
1692 field, struct bt_ctf_field_variant, parent);
1693
1694 return bt_ctf_field_serialize(variant->payload, pos);
1695}
1696
1697static
1698int bt_ctf_field_array_serialize(struct bt_ctf_field *field,
1699 struct ctf_stream_pos *pos)
1700{
1701 size_t i;
1702 int ret = 0;
1703 struct bt_ctf_field_array *array = container_of(
1704 field, struct bt_ctf_field_array, parent);
1705
1706 for (i = 0; i < array->elements->len; i++) {
1707 ret = bt_ctf_field_serialize(
1708 g_ptr_array_index(array->elements, i), pos);
1709 if (ret) {
1710 goto end;
1711 }
1712 }
1713end:
1714 return ret;
1715}
1716
1717static
1718int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field,
1719 struct ctf_stream_pos *pos)
1720{
1721 size_t i;
1722 int ret = 0;
1723 struct bt_ctf_field_sequence *sequence = container_of(
1724 field, struct bt_ctf_field_sequence, parent);
1725
1726 for (i = 0; i < sequence->elements->len; i++) {
1727 ret = bt_ctf_field_serialize(
1728 g_ptr_array_index(sequence->elements, i), pos);
1729 if (ret) {
1730 goto end;
1731 }
1732 }
1733end:
1734 return ret;
1735}
1736
1737static
1738int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
1739 struct ctf_stream_pos *pos)
1740{
1741 size_t i;
1742 int ret = 0;
1743 struct bt_ctf_field_string *string = container_of(field,
1744 struct bt_ctf_field_string, parent);
1745 struct bt_ctf_field_type *character_type =
1746 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1747 struct bt_ctf_field *character = bt_ctf_field_create(character_type);
1748
1749 for (i = 0; i < string->payload->len + 1; i++) {
1750 ret = bt_ctf_field_unsigned_integer_set_value(character,
1751 (uint64_t) string->payload->str[i]);
1752 if (ret) {
1753 goto end;
1754 }
1755
1756 ret = bt_ctf_field_integer_serialize(character, pos);
1757 if (ret) {
1758 goto end;
1759 }
1760 }
1761end:
1762 bt_ctf_field_put(character);
1763 bt_ctf_field_type_put(character_type);
1764 return ret;
1765}
1766
87d43dc1
JG
1767static
1768int bt_ctf_field_integer_copy(struct bt_ctf_field *src,
1769 struct bt_ctf_field *dst)
1770{
1771 struct bt_ctf_field_integer *integer_src, *integer_dst;
1772
1773 integer_src = container_of(src, struct bt_ctf_field_integer, parent);
8bfa3f9c 1774 integer_dst = container_of(dst, struct bt_ctf_field_integer, parent);
87d43dc1
JG
1775
1776 memcpy(&integer_dst->definition, &integer_src->definition,
1777 sizeof(struct definition_integer));
1778 return 0;
1779}
1780
1781static
1782int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src,
1783 struct bt_ctf_field *dst)
1784{
1785 int ret = 0;
1786 struct bt_ctf_field_enumeration *enum_src, *enum_dst;
1787
1788 enum_src = container_of(src, struct bt_ctf_field_enumeration, parent);
1789 enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent);
1790
1791 if (enum_src->payload) {
1792 enum_dst->payload = bt_ctf_field_copy(enum_src->payload);
1793 if (!enum_dst->payload) {
1794 ret = -1;
1795 goto end;
1796 }
1797 }
1798end:
1799 return ret;
1800}
1801
1802static
1803int bt_ctf_field_floating_point_copy(
1804 struct bt_ctf_field *src, struct bt_ctf_field *dst)
1805{
1806 struct bt_ctf_field_floating_point *float_src, *float_dst;
1807
1808 float_src = container_of(src, struct bt_ctf_field_floating_point,
1809 parent);
1810 float_dst = container_of(dst, struct bt_ctf_field_floating_point,
1811 parent);
1812
1813 memcpy(&float_dst->definition, &float_src->definition,
1814 sizeof(struct definition_float));
1815 memcpy(&float_dst->sign, &float_src->sign,
1816 sizeof(struct definition_integer));
1817 memcpy(&float_dst->mantissa, &float_src->mantissa,
1818 sizeof(struct definition_integer));
1819 memcpy(&float_dst->exp, &float_src->exp,
1820 sizeof(struct definition_integer));
1821 return 0;
1822}
1823
1824static
1825int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
1826 struct bt_ctf_field *dst)
1827{
8bfa3f9c 1828 int ret = 0, i;
87d43dc1
JG
1829 struct bt_ctf_field_structure *struct_src, *struct_dst;
1830
1831 struct_src = container_of(src, struct bt_ctf_field_structure, parent);
1832 struct_dst = container_of(dst, struct bt_ctf_field_structure, parent);
1833
8bfa3f9c 1834 /* This field_name_to_index HT is owned by the structure field type */
87d43dc1 1835 struct_dst->field_name_to_index = struct_src->field_name_to_index;
8bfa3f9c 1836 g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len);
87d43dc1
JG
1837
1838 for (i = 0; i < struct_src->fields->len; i++) {
1839 struct bt_ctf_field *field_copy = bt_ctf_field_copy(
1840 g_ptr_array_index(struct_src->fields, i));
1841
1842 if (!field_copy) {
1843 ret = -1;
1844 goto end;
1845 }
8bfa3f9c 1846 g_ptr_array_index(struct_dst->fields, i) = field_copy;
87d43dc1
JG
1847 }
1848end:
1849 return ret;
1850}
1851
1852static
1853int bt_ctf_field_variant_copy(struct bt_ctf_field *src,
1854 struct bt_ctf_field *dst)
1855{
1856 int ret = 0;
1857 struct bt_ctf_field_variant *variant_src, *variant_dst;
1858
1859 variant_src = container_of(src, struct bt_ctf_field_variant, parent);
1860 variant_dst = container_of(dst, struct bt_ctf_field_variant, parent);
1861
1862 if (variant_src->tag) {
1863 variant_dst->tag = bt_ctf_field_copy(variant_src->tag);
1864 if (!variant_dst->tag) {
1865 ret = -1;
1866 goto end;
1867 }
1868 }
1869 if (variant_src->payload) {
1870 variant_dst->payload = bt_ctf_field_copy(variant_src->payload);
1871 if (!variant_dst->payload) {
1872 ret = -1;
1873 goto end;
1874 }
1875 }
1876end:
1877 return ret;
1878}
1879
1880static
1881int bt_ctf_field_array_copy(struct bt_ctf_field *src,
1882 struct bt_ctf_field *dst)
1883{
1884 int ret = 0, i;
1885 struct bt_ctf_field_array *array_src, *array_dst;
1886
1887 array_src = container_of(src, struct bt_ctf_field_array, parent);
1888 array_dst = container_of(dst, struct bt_ctf_field_array, parent);
1889
8bfa3f9c 1890 g_ptr_array_set_size(array_dst->elements, array_src->elements->len);
87d43dc1
JG
1891 for (i = 0; i < array_src->elements->len; i++) {
1892 struct bt_ctf_field *field_copy = bt_ctf_field_copy(
1893 g_ptr_array_index(array_src->elements, i));
1894
1895 if (!field_copy) {
1896 ret = -1;
1897 goto end;
1898 }
8bfa3f9c 1899 g_ptr_array_index(array_dst->elements, i) = field_copy;
87d43dc1
JG
1900 }
1901end:
1902 return ret;
1903}
1904
1905static
1906int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
1907 struct bt_ctf_field *dst)
1908{
1909 int ret = 0, i;
1910 struct bt_ctf_field_sequence *sequence_src, *sequence_dst;
1911
1912 sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
1913 sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
1914
8bfa3f9c 1915 g_ptr_array_set_size(sequence_dst->elements,
87d43dc1
JG
1916 sequence_src->elements->len);
1917 for (i = 0; i < sequence_src->elements->len; i++) {
1918 struct bt_ctf_field *field_copy = bt_ctf_field_copy(
1919 g_ptr_array_index(sequence_src->elements, i));
1920
1921 if (!field_copy) {
1922 ret = -1;
1923 goto end;
1924 }
8bfa3f9c 1925 g_ptr_array_index(sequence_dst->elements, i) = field_copy;
87d43dc1
JG
1926 }
1927end:
1928 return ret;
1929}
1930
1931static
1932int bt_ctf_field_string_copy(struct bt_ctf_field *src,
1933 struct bt_ctf_field *dst)
1934{
1935 int ret = 0;
1936 struct bt_ctf_field_string *string_src, *string_dst;
1937
1938 string_src = container_of(src, struct bt_ctf_field_string, parent);
1939 string_dst = container_of(dst, struct bt_ctf_field_string, parent);
1940
1941 if (string_src->payload) {
1942 string_dst->payload = g_string_new(string_src->payload->str);
1943 if (!string_dst->payload) {
1944 ret = -1;
1945 goto end;
1946 }
1947 }
1948end:
1949 return ret;
1950}
1951
273b65be
JG
1952static
1953int increase_packet_size(struct ctf_stream_pos *pos)
1954{
1955 int ret;
1956
1957 assert(pos);
1958 ret = munmap_align(pos->base_mma);
1959 if (ret) {
1960 goto end;
1961 }
1962
1963 pos->packet_size += PACKET_LEN_INCREMENT;
1964 ret = posix_fallocate(pos->fd, pos->mmap_offset,
1965 pos->packet_size / CHAR_BIT);
1966 if (ret) {
1967 goto end;
1968 }
1969
1970 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
1971 pos->flags, pos->fd, pos->mmap_offset);
1972 if (pos->base_mma == MAP_FAILED) {
1973 ret = -1;
1974 }
1975end:
1976 return ret;
1977}
This page took 0.105935 seconds and 4 git commands to generate.