Fix: ir: return current variant's field if possible
[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;
2829190c
PP
604
605 /*
606 * If the variant currently has a tag and a payload, and if the
607 * requested tag value is the same as the current one, return
608 * the current payload instead of creating a fresh one.
609 */
610 if (variant->tag && variant->payload) {
611 struct bt_ctf_field *cur_tag_container = NULL;
612 struct bt_ctf_field_integer *cur_tag_enum_integer;
613 int64_t cur_tag_value;
614
615 cur_tag_container =
616 bt_ctf_field_enumeration_get_container(variant->tag);
617 cur_tag_enum_integer = container_of(cur_tag_container,
618 struct bt_ctf_field_integer, parent);
619 bt_ctf_field_put(cur_tag_container);
620 cur_tag_value = cur_tag_enum_integer->definition.value._signed;
621
622 if (cur_tag_value == tag_enum_value) {
623 new_field = variant->payload;
624 bt_ctf_field_get(new_field);
625 goto end;
626 }
627 }
628
b92ddaaa
JG
629 field_type = bt_ctf_field_type_variant_get_field_type_signed(
630 variant_type, tag_enum_value);
273b65be
JG
631 if (!field_type) {
632 goto end;
633 }
634
635 new_field = bt_ctf_field_create(field_type);
636 if (!new_field) {
637 goto end;
638 }
639
640 bt_ctf_field_put(variant->tag);
641 bt_ctf_field_put(variant->payload);
642 bt_ctf_field_get(new_field);
643 bt_ctf_field_get(tag_field);
644 variant->tag = tag_field;
645 variant->payload = new_field;
646end:
647 bt_ctf_field_put(tag_enum);
648 return new_field;
649}
650
651struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
652 struct bt_ctf_field *field)
653{
654 struct bt_ctf_field *container = NULL;
655 struct bt_ctf_field_enumeration *enumeration;
656
b92ddaaa
JG
657 if (!field || bt_ctf_field_type_get_type_id(field->type) !=
658 CTF_TYPE_ENUM) {
273b65be
JG
659 goto end;
660 }
661
662 enumeration = container_of(field, struct bt_ctf_field_enumeration,
663 parent);
664 if (!enumeration->payload) {
665 struct bt_ctf_field_type_enumeration *enumeration_type =
666 container_of(field->type,
667 struct bt_ctf_field_type_enumeration, parent);
668 enumeration->payload =
669 bt_ctf_field_create(enumeration_type->container);
670 }
671
672 container = enumeration->payload;
673 bt_ctf_field_get(container);
674end:
675 return container;
676}
677
cd95e351
JG
678const char *bt_ctf_field_enumeration_get_mapping_name(
679 struct bt_ctf_field *field)
680{
681 int ret;
682 const char *name = NULL;
683 struct bt_ctf_field *container = NULL;
684 struct bt_ctf_field_type *container_type = NULL;
685 struct bt_ctf_field_type_integer *integer_type = NULL;
686 struct bt_ctf_field_type_enumeration *enumeration_type = NULL;
687
688 container = bt_ctf_field_enumeration_get_container(field);
689 if (!container) {
690 goto end;
691 }
692
693 container_type = bt_ctf_field_get_type(container);
694 if (!container_type) {
695 goto error_put_container;
696 }
697
698 integer_type = container_of(container_type,
699 struct bt_ctf_field_type_integer, parent);
700 enumeration_type = container_of(field->type,
701 struct bt_ctf_field_type_enumeration, parent);
702
10817e06 703 if (!integer_type->declaration.signedness) {
cd95e351
JG
704 uint64_t value;
705 ret = bt_ctf_field_unsigned_integer_get_value(container,
706 &value);
707 if (ret) {
708 goto error_put_container_type;
709 }
710
711 name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
712 enumeration_type, value);
713 } else {
714 int64_t value;
715 ret = bt_ctf_field_signed_integer_get_value(container,
716 &value);
717 if (ret) {
718 goto error_put_container_type;
719 }
720
721 name = bt_ctf_field_type_enumeration_get_mapping_name_signed(
722 enumeration_type, value);
723 }
724
725error_put_container_type:
726 bt_ctf_field_type_put(container_type);
727error_put_container:
728 bt_ctf_field_put(container);
729end:
730 return name;
731}
732
733int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field,
734 int64_t *value)
735{
736 int ret = 0;
737 struct bt_ctf_field_integer *integer;
738 struct bt_ctf_field_type_integer *integer_type;
739
740 if (!field || !value || !field->payload_set ||
741 bt_ctf_field_type_get_type_id(field->type) !=
742 CTF_TYPE_INTEGER) {
743 ret = -1;
744 goto end;
745 }
746
747 integer_type = container_of(field->type,
748 struct bt_ctf_field_type_integer, parent);
749 if (!integer_type->declaration.signedness) {
750 ret = -1;
751 goto end;
752 }
753
754 integer = container_of(field,
755 struct bt_ctf_field_integer, parent);
756 *value = integer->definition.value._signed;
757end:
758 return ret;
759}
760
273b65be
JG
761int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
762 int64_t value)
763{
764 int ret = 0;
765 struct bt_ctf_field_integer *integer;
766 struct bt_ctf_field_type_integer *integer_type;
767 unsigned int size;
768 int64_t min_value, max_value;
769
770 if (!field ||
771 bt_ctf_field_type_get_type_id(field->type) !=
772 CTF_TYPE_INTEGER) {
773 ret = -1;
774 goto end;
775 }
776
777 integer = container_of(field, struct bt_ctf_field_integer, parent);
778 integer_type = container_of(field->type,
779 struct bt_ctf_field_type_integer, parent);
780 if (!integer_type->declaration.signedness) {
781 ret = -1;
782 goto end;
783 }
784
785 size = integer_type->declaration.len;
786 min_value = -((int64_t)1 << (size - 1));
787 max_value = ((int64_t)1 << (size - 1)) - 1;
788 if (value < min_value || value > max_value) {
789 ret = -1;
790 goto end;
791 }
792
793 integer->definition.value._signed = value;
794 integer->parent.payload_set = 1;
795end:
796 return ret;
797}
798
cd95e351
JG
799int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field,
800 uint64_t *value)
801{
802 int ret = 0;
803 struct bt_ctf_field_integer *integer;
804 struct bt_ctf_field_type_integer *integer_type;
805
806 if (!field || !value || !field->payload_set ||
807 bt_ctf_field_type_get_type_id(field->type) !=
808 CTF_TYPE_INTEGER) {
809 ret = -1;
810 goto end;
811 }
812
813 integer_type = container_of(field->type,
814 struct bt_ctf_field_type_integer, parent);
815 if (integer_type->declaration.signedness) {
816 ret = -1;
817 goto end;
818 }
819
820 integer = container_of(field,
821 struct bt_ctf_field_integer, parent);
822 *value = integer->definition.value._unsigned;
823end:
824 return ret;
825}
826
273b65be
JG
827int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
828 uint64_t value)
829{
830 int ret = 0;
831 struct bt_ctf_field_integer *integer;
832 struct bt_ctf_field_type_integer *integer_type;
833 unsigned int size;
834 uint64_t max_value;
835
836 if (!field ||
837 bt_ctf_field_type_get_type_id(field->type) !=
838 CTF_TYPE_INTEGER) {
839 ret = -1;
840 goto end;
841 }
842
843 integer = container_of(field, struct bt_ctf_field_integer, parent);
844 integer_type = container_of(field->type,
845 struct bt_ctf_field_type_integer, parent);
846 if (integer_type->declaration.signedness) {
847 ret = -1;
848 goto end;
849 }
850
851 size = integer_type->declaration.len;
852 max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1;
853 if (value > max_value) {
854 ret = -1;
855 goto end;
856 }
857
858 integer->definition.value._unsigned = value;
859 integer->parent.payload_set = 1;
860end:
861 return ret;
862}
863
cd95e351
JG
864int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field,
865 double *value)
866{
867 int ret = 0;
868 struct bt_ctf_field_floating_point *floating_point;
869
870 if (!field || !value || !field->payload_set ||
871 bt_ctf_field_type_get_type_id(field->type) !=
872 CTF_TYPE_FLOAT) {
873 ret = -1;
874 goto end;
875 }
876
877 floating_point = container_of(field,
878 struct bt_ctf_field_floating_point, parent);
879 *value = floating_point->definition.value;
880end:
881 return ret;
882}
883
273b65be
JG
884int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
885 double value)
886{
887 int ret = 0;
888 struct bt_ctf_field_floating_point *floating_point;
889
890 if (!field ||
891 bt_ctf_field_type_get_type_id(field->type) !=
892 CTF_TYPE_FLOAT) {
893 ret = -1;
894 goto end;
895 }
896 floating_point = container_of(field, struct bt_ctf_field_floating_point,
897 parent);
898 floating_point->definition.value = value;
899 floating_point->parent.payload_set = 1;
900end:
901 return ret;
902}
903
cd95e351
JG
904const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field)
905{
906 const char *ret = NULL;
907 struct bt_ctf_field_string *string;
908
909 if (!field || !field->payload_set ||
910 bt_ctf_field_type_get_type_id(field->type) !=
911 CTF_TYPE_STRING) {
912 goto end;
913 }
914
915 string = container_of(field,
916 struct bt_ctf_field_string, parent);
917 ret = string->payload->str;
918end:
919 return ret;
920}
921
273b65be
JG
922int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
923 const char *value)
924{
925 int ret = 0;
926 struct bt_ctf_field_string *string;
927
928 if (!field || !value ||
929 bt_ctf_field_type_get_type_id(field->type) !=
930 CTF_TYPE_STRING) {
931 ret = -1;
932 goto end;
933 }
934
935 string = container_of(field, struct bt_ctf_field_string, parent);
936 if (string->payload) {
97736814
JG
937 g_string_assign(string->payload, value);
938 } else {
939 string->payload = g_string_new(value);
273b65be
JG
940 }
941
273b65be
JG
942 string->parent.payload_set = 1;
943end:
944 return ret;
945}
946
c6f9c5a3
PP
947int bt_ctf_field_string_append(struct bt_ctf_field *field,
948 const char *value)
949{
950 int ret = 0;
951 struct bt_ctf_field_string *string_field;
952
953 if (!field || !value ||
954 bt_ctf_field_type_get_type_id(field->type) !=
955 CTF_TYPE_STRING) {
956 ret = -1;
957 goto end;
958 }
959
960 string_field = container_of(field, struct bt_ctf_field_string, parent);
961
962 if (string_field->payload) {
963 g_string_append(string_field->payload, value);
964 } else {
965 string_field->payload = g_string_new(value);
966 }
967
968 string_field->parent.payload_set = 1;
969
970end:
971 return ret;
972}
973
f98c6554
PP
974int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
975 const char *value, unsigned int length)
976{
977 int i;
978 int ret = 0;
979 unsigned int effective_length = length;
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 /* make sure no null bytes are appended */
992 for (i = 0; i < length; ++i) {
993 if (value[i] == '\0') {
994 effective_length = i;
995 break;
996 }
997 }
998
999 if (string_field->payload) {
ce6d5230 1000 g_string_append_len(string_field->payload, value,
f98c6554
PP
1001 effective_length);
1002 } else {
1003 string_field->payload = g_string_new_len(value,
1004 effective_length);
1005 }
1006
1007 string_field->parent.payload_set = 1;
1008
1009end:
1010 return ret;
1011}
1012
273b65be
JG
1013BT_HIDDEN
1014int bt_ctf_field_validate(struct bt_ctf_field *field)
1015{
1016 int ret = 0;
1017 enum ctf_type_id type_id;
1018
1019 if (!field) {
1020 ret = -1;
1021 goto end;
1022 }
1023
1024 type_id = bt_ctf_field_type_get_type_id(field->type);
1025 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1026 ret = -1;
1027 goto end;
1028 }
1029
1030 ret = field_validate_funcs[type_id](field);
1031end:
1032 return ret;
1033}
1034
12c8a1a3
JG
1035BT_HIDDEN
1036int bt_ctf_field_reset(struct bt_ctf_field *field)
1037{
1038 int ret = 0;
1039 enum ctf_type_id type_id;
1040
1041 if (!field) {
1042 ret = -1;
1043 goto end;
1044 }
1045
1046 type_id = bt_ctf_field_type_get_type_id(field->type);
1047 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1048 ret = -1;
1049 goto end;
1050 }
1051
1052 ret = field_reset_funcs[type_id](field);
1053end:
1054 return ret;
1055}
1056
273b65be
JG
1057BT_HIDDEN
1058int bt_ctf_field_serialize(struct bt_ctf_field *field,
1059 struct ctf_stream_pos *pos)
1060{
1061 int ret = 0;
1062 enum ctf_type_id type_id;
1063
1064 if (!field || !pos) {
1065 ret = -1;
1066 goto end;
1067 }
1068
1069 type_id = bt_ctf_field_type_get_type_id(field->type);
1070 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
1071 ret = -1;
1072 goto end;
1073 }
1074
1075 ret = field_serialize_funcs[type_id](field, pos);
1076end:
1077 return ret;
1078}
1079
87d43dc1
JG
1080BT_HIDDEN
1081struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
1082{
1083 int ret;
1084 struct bt_ctf_field *copy = NULL;
1085 enum ctf_type_id type_id;
1086
1087 if (!field) {
87d43dc1
JG
1088 goto end;
1089 }
1090
1091 type_id = bt_ctf_field_type_get_type_id(field->type);
1092 if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
87d43dc1
JG
1093 goto end;
1094 }
1095
1096 copy = bt_ctf_field_create(field->type);
1097 if (!copy) {
1098 goto end;
1099 }
1100
70996764 1101 copy->payload_set = field->payload_set;
87d43dc1
JG
1102 ret = field_copy_funcs[type_id](field, copy);
1103 if (ret) {
1104 bt_ctf_field_put(copy);
1105 copy = NULL;
1106 }
1107end:
1108 return copy;
1109}
1110
273b65be
JG
1111static
1112struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
1113{
1114 struct bt_ctf_field_type_integer *integer_type = container_of(type,
1115 struct bt_ctf_field_type_integer, parent);
1116 struct bt_ctf_field_integer *integer = g_new0(
1117 struct bt_ctf_field_integer, 1);
1118
1119 if (integer) {
1120 integer->definition.declaration = &integer_type->declaration;
1121 }
1122
1123 return integer ? &integer->parent : NULL;
1124}
1125
1126static
1127struct bt_ctf_field *bt_ctf_field_enumeration_create(
1128 struct bt_ctf_field_type *type)
1129{
1130 struct bt_ctf_field_enumeration *enumeration = g_new0(
1131 struct bt_ctf_field_enumeration, 1);
1132
1133 return enumeration ? &enumeration->parent : NULL;
1134}
1135
1136static
1137struct bt_ctf_field *bt_ctf_field_floating_point_create(
1138 struct bt_ctf_field_type *type)
1139{
1140 struct bt_ctf_field_floating_point *floating_point;
1141 struct bt_ctf_field_type_floating_point *floating_point_type;
1142
1143 floating_point = g_new0(struct bt_ctf_field_floating_point, 1);
1144 if (!floating_point) {
1145 goto end;
1146 }
1147
1148 floating_point_type = container_of(type,
1149 struct bt_ctf_field_type_floating_point, parent);
1150 floating_point->definition.declaration = container_of(
1151 type->declaration, struct declaration_float, p);
1152
1153
1154 floating_point->definition.sign = &floating_point->sign;
1155 floating_point->sign.declaration = &floating_point_type->sign;
1156 floating_point->definition.sign->p.declaration =
1157 &floating_point_type->sign.p;
1158
1159 floating_point->definition.mantissa = &floating_point->mantissa;
1160 floating_point->mantissa.declaration = &floating_point_type->mantissa;
1161 floating_point->definition.mantissa->p.declaration =
1162 &floating_point_type->mantissa.p;
1163
1164 floating_point->definition.exp = &floating_point->exp;
1165 floating_point->exp.declaration = &floating_point_type->exp;
1166 floating_point->definition.exp->p.declaration =
1167 &floating_point_type->exp.p;
1168
1169end:
1170 return floating_point ? &floating_point->parent : NULL;
1171}
1172
1173static
1174struct bt_ctf_field *bt_ctf_field_structure_create(
1175 struct bt_ctf_field_type *type)
1176{
1177 struct bt_ctf_field_type_structure *structure_type = container_of(type,
1178 struct bt_ctf_field_type_structure, parent);
1179 struct bt_ctf_field_structure *structure = g_new0(
1180 struct bt_ctf_field_structure, 1);
1181 struct bt_ctf_field *field = NULL;
1182
1183 if (!structure || !structure_type->fields->len) {
1184 goto end;
1185 }
1186
1187 structure->field_name_to_index = structure_type->field_name_to_index;
1188 structure->fields = g_ptr_array_new_with_free_func(
1189 (GDestroyNotify)bt_ctf_field_put);
1190 g_ptr_array_set_size(structure->fields,
1191 g_hash_table_size(structure->field_name_to_index));
1192 field = &structure->parent;
1193end:
1194 return field;
1195}
1196
1197static
1198struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
1199{
1200 struct bt_ctf_field_variant *variant = g_new0(
1201 struct bt_ctf_field_variant, 1);
1202 return variant ? &variant->parent : NULL;
1203}
1204
1205static
1206struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
1207{
1208 struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1);
1209 struct bt_ctf_field_type_array *array_type;
1210 unsigned int array_length;
1211
1212 if (!array || !type) {
1213 goto error;
1214 }
1215
1216 array_type = container_of(type, struct bt_ctf_field_type_array, parent);
1217 array_length = array_type->length;
fe0fe95c 1218 array->elements = g_ptr_array_sized_new(array_length);
273b65be
JG
1219 if (!array->elements) {
1220 goto error;
1221 }
1222
fe0fe95c
JG
1223 g_ptr_array_set_free_func(array->elements,
1224 (GDestroyNotify)bt_ctf_field_put);
273b65be
JG
1225 g_ptr_array_set_size(array->elements, array_length);
1226 return &array->parent;
1227error:
1228 g_free(array);
1229 return NULL;
1230}
1231
1232static
1233struct bt_ctf_field *bt_ctf_field_sequence_create(
1234 struct bt_ctf_field_type *type)
1235{
1236 struct bt_ctf_field_sequence *sequence = g_new0(
1237 struct bt_ctf_field_sequence, 1);
1238 return sequence ? &sequence->parent : NULL;
1239}
1240
1241static
1242struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
1243{
1244 struct bt_ctf_field_string *string = g_new0(
1245 struct bt_ctf_field_string, 1);
1246 return string ? &string->parent : NULL;
1247}
1248
1249static
1250void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
1251{
1252 struct bt_ctf_field *field;
1253 struct bt_ctf_field_type *type;
1254 enum ctf_type_id type_id;
1255
1256 if (!ref) {
1257 return;
1258 }
1259
1260 field = container_of(ref, struct bt_ctf_field, ref_count);
1261 type = field->type;
1262 type_id = bt_ctf_field_type_get_type_id(type);
1263 if (type_id <= CTF_TYPE_UNKNOWN ||
1264 type_id >= NR_CTF_TYPES) {
1265 return;
1266 }
1267
1268 field_destroy_funcs[type_id](field);
1269 if (type) {
1270 bt_ctf_field_type_put(type);
1271 }
1272}
1273
1274static
1275void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
1276{
1277 struct bt_ctf_field_integer *integer;
1278
1279 if (!field) {
1280 return;
1281 }
1282
1283 integer = container_of(field, struct bt_ctf_field_integer, parent);
1284 g_free(integer);
1285}
1286
1287static
1288void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field)
1289{
1290 struct bt_ctf_field_enumeration *enumeration;
1291
1292 if (!field) {
1293 return;
1294 }
1295
1296 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1297 parent);
1298 bt_ctf_field_put(enumeration->payload);
1299 g_free(enumeration);
1300}
1301
1302static
1303void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
1304{
1305 struct bt_ctf_field_floating_point *floating_point;
1306
1307 if (!field) {
1308 return;
1309 }
1310
1311 floating_point = container_of(field, struct bt_ctf_field_floating_point,
1312 parent);
1313 g_free(floating_point);
1314}
1315
1316static
1317void bt_ctf_field_structure_destroy(struct bt_ctf_field *field)
1318{
1319 struct bt_ctf_field_structure *structure;
1320
1321 if (!field) {
1322 return;
1323 }
1324
1325 structure = container_of(field, struct bt_ctf_field_structure, parent);
1326 g_ptr_array_free(structure->fields, TRUE);
1327 g_free(structure);
1328}
1329
1330static
1331void bt_ctf_field_variant_destroy(struct bt_ctf_field *field)
1332{
1333 struct bt_ctf_field_variant *variant;
1334
1335 if (!field) {
1336 return;
1337 }
1338
1339 variant = container_of(field, struct bt_ctf_field_variant, parent);
1340 bt_ctf_field_put(variant->tag);
1341 bt_ctf_field_put(variant->payload);
1342 g_free(variant);
1343}
1344
1345static
1346void bt_ctf_field_array_destroy(struct bt_ctf_field *field)
1347{
1348 struct bt_ctf_field_array *array;
1349
1350 if (!field) {
1351 return;
1352 }
1353
1354 array = container_of(field, struct bt_ctf_field_array, parent);
1355 g_ptr_array_free(array->elements, TRUE);
1356 g_free(array);
1357}
1358
1359static
1360void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
1361{
1362 struct bt_ctf_field_sequence *sequence;
1363
1364 if (!field) {
1365 return;
1366 }
1367
1368 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1369 g_ptr_array_free(sequence->elements, TRUE);
1370 bt_ctf_field_put(sequence->length);
1371 g_free(sequence);
1372}
1373
1374static
1375void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
1376{
1377 struct bt_ctf_field_string *string;
1378 if (!field) {
1379 return;
1380 }
1381
1382 string = container_of(field, struct bt_ctf_field_string, parent);
9b2b7163
JG
1383 if (string->payload) {
1384 g_string_free(string->payload, TRUE);
1385 }
273b65be
JG
1386 g_free(string);
1387}
1388
1389static
1390int bt_ctf_field_generic_validate(struct bt_ctf_field *field)
1391{
da2f6971 1392 return (field && field->payload_set) ? 0 : -1;
273b65be
JG
1393}
1394
1395static
1396int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field)
1397{
1398 int ret;
1399 struct bt_ctf_field_enumeration *enumeration;
1400
1401 if (!field) {
1402 ret = -1;
1403 goto end;
1404 }
1405
1406 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1407 parent);
1408 if (!enumeration->payload) {
1409 ret = -1;
1410 goto end;
1411 }
1412
1413 ret = bt_ctf_field_validate(enumeration->payload);
1414end:
1415 return ret;
1416}
1417
1418static
1419int bt_ctf_field_structure_validate(struct bt_ctf_field *field)
1420{
1421 size_t i;
1422 int ret = 0;
1423 struct bt_ctf_field_structure *structure;
1424
1425 if (!field) {
1426 ret = -1;
1427 goto end;
1428 }
1429
1430 structure = container_of(field, struct bt_ctf_field_structure, parent);
1431 for (i = 0; i < structure->fields->len; i++) {
1432 ret = bt_ctf_field_validate(structure->fields->pdata[i]);
1433 if (ret) {
1434 goto end;
1435 }
1436 }
1437end:
1438 return ret;
1439}
1440
1441static
1442int bt_ctf_field_variant_validate(struct bt_ctf_field *field)
1443{
1444 int ret = 0;
1445 struct bt_ctf_field_variant *variant;
1446
1447 if (!field) {
1448 ret = -1;
1449 goto end;
1450 }
1451
1452 variant = container_of(field, struct bt_ctf_field_variant, parent);
1453 ret = bt_ctf_field_validate(variant->payload);
1454end:
1455 return ret;
1456}
1457
1458static
1459int bt_ctf_field_array_validate(struct bt_ctf_field *field)
1460{
1461 size_t i;
1462 int ret = 0;
1463 struct bt_ctf_field_array *array;
1464
1465 if (!field) {
1466 ret = -1;
1467 goto end;
1468 }
1469
1470 array = container_of(field, struct bt_ctf_field_array, parent);
1471 for (i = 0; i < array->elements->len; i++) {
1472 ret = bt_ctf_field_validate(array->elements->pdata[i]);
1473 if (ret) {
1474 goto end;
1475 }
1476 }
1477end:
1478 return ret;
1479}
1480
1481static
1482int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
1483{
1484 size_t i;
1485 int ret = 0;
1486 struct bt_ctf_field_sequence *sequence;
1487
1488 if (!field) {
1489 ret = -1;
1490 goto end;
1491 }
1492
1493 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1494 for (i = 0; i < sequence->elements->len; i++) {
1495 ret = bt_ctf_field_validate(sequence->elements->pdata[i]);
1496 if (ret) {
1497 goto end;
1498 }
1499 }
1500end:
1501 return ret;
1502}
1503
12c8a1a3
JG
1504static
1505int bt_ctf_field_generic_reset(struct bt_ctf_field *field)
1506{
1507 int ret = 0;
1508
1509 if (!field) {
1510 ret = -1;
1511 goto end;
1512 }
1513
1514 field->payload_set = 0;
1515end:
1516 return ret;
1517}
1518
1519static
1520int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field)
1521{
1522 int ret = 0;
1523 struct bt_ctf_field_enumeration *enumeration;
1524
1525 if (!field) {
1526 ret = -1;
1527 goto end;
1528 }
1529
1530 enumeration = container_of(field, struct bt_ctf_field_enumeration,
1531 parent);
1532 if (!enumeration->payload) {
1533 goto end;
1534 }
1535
1536 ret = bt_ctf_field_reset(enumeration->payload);
1537end:
1538 return ret;
1539}
1540
1541static
1542int bt_ctf_field_structure_reset(struct bt_ctf_field *field)
1543{
1544 size_t i;
1545 int ret = 0;
1546 struct bt_ctf_field_structure *structure;
1547
1548 if (!field) {
1549 ret = -1;
1550 goto end;
1551 }
1552
1553 structure = container_of(field, struct bt_ctf_field_structure, parent);
1554 for (i = 0; i < structure->fields->len; i++) {
1555 struct bt_ctf_field *member = structure->fields->pdata[i];
1556
1557 if (!member) {
1558 /*
1559 * Structure members are lazily initialized; skip if
1560 * this member has not been allocated yet.
1561 */
1562 continue;
1563 }
1564
1565 ret = bt_ctf_field_reset(member);
1566 if (ret) {
1567 goto end;
1568 }
1569 }
1570end:
1571 return ret;
1572}
1573
1574static
1575int bt_ctf_field_variant_reset(struct bt_ctf_field *field)
1576{
1577 int ret = 0;
1578 struct bt_ctf_field_variant *variant;
1579
1580 if (!field) {
1581 ret = -1;
1582 goto end;
1583 }
1584
1585 variant = container_of(field, struct bt_ctf_field_variant, parent);
1586 if (variant->payload) {
1587 ret = bt_ctf_field_reset(variant->payload);
1588 }
1589end:
1590 return ret;
1591}
1592
1593static
1594int bt_ctf_field_array_reset(struct bt_ctf_field *field)
1595{
1596 size_t i;
1597 int ret = 0;
1598 struct bt_ctf_field_array *array;
1599
1600 if (!field) {
1601 ret = -1;
1602 goto end;
1603 }
1604
1605 array = container_of(field, struct bt_ctf_field_array, parent);
1606 for (i = 0; i < array->elements->len; i++) {
1607 struct bt_ctf_field *member = array->elements->pdata[i];
1608
1609 if (!member) {
1610 /*
1611 * Array elements are lazily initialized; skip if
1612 * this member has not been allocated yet.
1613 */
1614 continue;
1615 }
1616
1617 ret = bt_ctf_field_reset(member);
1618 if (ret) {
1619 goto end;
1620 }
1621 }
1622end:
1623 return ret;
1624}
1625
1626static
1627int bt_ctf_field_sequence_reset(struct bt_ctf_field *field)
1628{
1629 size_t i;
1630 int ret = 0;
1631 struct bt_ctf_field_sequence *sequence;
1632
1633 if (!field) {
1634 ret = -1;
1635 goto end;
1636 }
1637
1638 sequence = container_of(field, struct bt_ctf_field_sequence, parent);
1639 for (i = 0; i < sequence->elements->len; i++) {
1640 struct bt_ctf_field *member = sequence->elements->pdata[i];
1641
1642 if (!member) {
1643 /*
1644 * Sequence elements are lazily initialized; skip if
1645 * this member has not been allocated yet.
1646 */
1647 continue;
1648 }
1649
1650 ret = bt_ctf_field_reset(member);
1651 if (ret) {
1652 goto end;
1653 }
1654 }
1655end:
1656 return ret;
1657}
1658
1659static
1660int bt_ctf_field_string_reset(struct bt_ctf_field *field)
1661{
1662 int ret = 0;
1663 struct bt_ctf_field_string *string;
1664
1665 if (!field) {
1666 ret = -1;
1667 goto end;
1668 }
1669
1670 ret = bt_ctf_field_generic_reset(field);
1671 if (ret) {
1672 goto end;
1673 }
1674
1675 string = container_of(field, struct bt_ctf_field_string, parent);
1676 if (string->payload) {
1677 g_string_truncate(string->payload, 0);
1678 }
1679end:
1680 return ret;
1681}
1682
273b65be
JG
1683static
1684int bt_ctf_field_integer_serialize(struct bt_ctf_field *field,
1685 struct ctf_stream_pos *pos)
1686{
1687 int ret = 0;
1688 struct bt_ctf_field_integer *integer = container_of(field,
1689 struct bt_ctf_field_integer, parent);
1690
1691retry:
1692 ret = ctf_integer_write(&pos->parent, &integer->definition.p);
1693 if (ret == -EFAULT) {
1694 /*
1695 * The field is too large to fit in the current packet's
1696 * remaining space. Bump the packet size and retry.
1697 */
1698 ret = increase_packet_size(pos);
1699 if (ret) {
1700 goto end;
1701 }
1702 goto retry;
1703 }
1704end:
1705 return ret;
1706}
1707
1708static
1709int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field,
1710 struct ctf_stream_pos *pos)
1711{
1712 struct bt_ctf_field_enumeration *enumeration = container_of(
1713 field, struct bt_ctf_field_enumeration, parent);
1714
1715 return bt_ctf_field_serialize(enumeration->payload, pos);
1716}
1717
1718static
1719int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field,
1720 struct ctf_stream_pos *pos)
1721{
1722 int ret = 0;
1723 struct bt_ctf_field_floating_point *floating_point = container_of(field,
1724 struct bt_ctf_field_floating_point, parent);
1725
1726retry:
1727 ret = ctf_float_write(&pos->parent, &floating_point->definition.p);
1728 if (ret == -EFAULT) {
1729 /*
1730 * The field is too large to fit in the current packet's
1731 * remaining space. Bump the packet size and retry.
1732 */
1733 ret = increase_packet_size(pos);
1734 if (ret) {
1735 goto end;
1736 }
1737 goto retry;
1738 }
1739end:
1740 return ret;
1741}
1742
1743static
1744int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
1745 struct ctf_stream_pos *pos)
1746{
1747 size_t i;
1748 int ret = 0;
1749 struct bt_ctf_field_structure *structure = container_of(
1750 field, struct bt_ctf_field_structure, parent);
1751
1752 while (!ctf_pos_access_ok(pos,
1753 offset_align(pos->offset,
1754 field->type->declaration->alignment))) {
9f56e450
JG
1755 ret = increase_packet_size(pos);
1756 if (ret) {
1757 goto end;
1758 }
273b65be
JG
1759 }
1760
70fd5a51
MD
1761 if (!ctf_align_pos(pos, field->type->declaration->alignment)) {
1762 ret = -1;
1763 goto end;
1764 }
273b65be
JG
1765
1766 for (i = 0; i < structure->fields->len; i++) {
1767 struct bt_ctf_field *field = g_ptr_array_index(
1768 structure->fields, i);
1769
1770 ret = bt_ctf_field_serialize(field, pos);
1771 if (ret) {
1772 break;
1773 }
1774 }
9f56e450 1775end:
273b65be
JG
1776 return ret;
1777}
1778
1779static
1780int bt_ctf_field_variant_serialize(struct bt_ctf_field *field,
1781 struct ctf_stream_pos *pos)
1782{
1783 struct bt_ctf_field_variant *variant = container_of(
1784 field, struct bt_ctf_field_variant, parent);
1785
1786 return bt_ctf_field_serialize(variant->payload, pos);
1787}
1788
1789static
1790int bt_ctf_field_array_serialize(struct bt_ctf_field *field,
1791 struct ctf_stream_pos *pos)
1792{
1793 size_t i;
1794 int ret = 0;
1795 struct bt_ctf_field_array *array = container_of(
1796 field, struct bt_ctf_field_array, parent);
1797
1798 for (i = 0; i < array->elements->len; i++) {
1799 ret = bt_ctf_field_serialize(
1800 g_ptr_array_index(array->elements, i), pos);
1801 if (ret) {
1802 goto end;
1803 }
1804 }
1805end:
1806 return ret;
1807}
1808
1809static
1810int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field,
1811 struct ctf_stream_pos *pos)
1812{
1813 size_t i;
1814 int ret = 0;
1815 struct bt_ctf_field_sequence *sequence = container_of(
1816 field, struct bt_ctf_field_sequence, parent);
1817
1818 for (i = 0; i < sequence->elements->len; i++) {
1819 ret = bt_ctf_field_serialize(
1820 g_ptr_array_index(sequence->elements, i), pos);
1821 if (ret) {
1822 goto end;
1823 }
1824 }
1825end:
1826 return ret;
1827}
1828
1829static
1830int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
1831 struct ctf_stream_pos *pos)
1832{
1833 size_t i;
1834 int ret = 0;
1835 struct bt_ctf_field_string *string = container_of(field,
1836 struct bt_ctf_field_string, parent);
1837 struct bt_ctf_field_type *character_type =
1838 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1839 struct bt_ctf_field *character = bt_ctf_field_create(character_type);
1840
1841 for (i = 0; i < string->payload->len + 1; i++) {
1842 ret = bt_ctf_field_unsigned_integer_set_value(character,
1843 (uint64_t) string->payload->str[i]);
1844 if (ret) {
1845 goto end;
1846 }
1847
1848 ret = bt_ctf_field_integer_serialize(character, pos);
1849 if (ret) {
1850 goto end;
1851 }
1852 }
1853end:
1854 bt_ctf_field_put(character);
1855 bt_ctf_field_type_put(character_type);
1856 return ret;
1857}
1858
87d43dc1
JG
1859static
1860int bt_ctf_field_integer_copy(struct bt_ctf_field *src,
1861 struct bt_ctf_field *dst)
1862{
1863 struct bt_ctf_field_integer *integer_src, *integer_dst;
1864
1865 integer_src = container_of(src, struct bt_ctf_field_integer, parent);
8bfa3f9c 1866 integer_dst = container_of(dst, struct bt_ctf_field_integer, parent);
87d43dc1
JG
1867
1868 memcpy(&integer_dst->definition, &integer_src->definition,
1869 sizeof(struct definition_integer));
1870 return 0;
1871}
1872
1873static
1874int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src,
1875 struct bt_ctf_field *dst)
1876{
1877 int ret = 0;
1878 struct bt_ctf_field_enumeration *enum_src, *enum_dst;
1879
1880 enum_src = container_of(src, struct bt_ctf_field_enumeration, parent);
1881 enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent);
1882
1883 if (enum_src->payload) {
1884 enum_dst->payload = bt_ctf_field_copy(enum_src->payload);
1885 if (!enum_dst->payload) {
1886 ret = -1;
1887 goto end;
1888 }
1889 }
1890end:
1891 return ret;
1892}
1893
1894static
1895int bt_ctf_field_floating_point_copy(
1896 struct bt_ctf_field *src, struct bt_ctf_field *dst)
1897{
1898 struct bt_ctf_field_floating_point *float_src, *float_dst;
1899
1900 float_src = container_of(src, struct bt_ctf_field_floating_point,
1901 parent);
1902 float_dst = container_of(dst, struct bt_ctf_field_floating_point,
1903 parent);
1904
1905 memcpy(&float_dst->definition, &float_src->definition,
1906 sizeof(struct definition_float));
1907 memcpy(&float_dst->sign, &float_src->sign,
1908 sizeof(struct definition_integer));
1909 memcpy(&float_dst->mantissa, &float_src->mantissa,
1910 sizeof(struct definition_integer));
1911 memcpy(&float_dst->exp, &float_src->exp,
1912 sizeof(struct definition_integer));
1913 return 0;
1914}
1915
1916static
1917int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
1918 struct bt_ctf_field *dst)
1919{
8bfa3f9c 1920 int ret = 0, i;
87d43dc1
JG
1921 struct bt_ctf_field_structure *struct_src, *struct_dst;
1922
1923 struct_src = container_of(src, struct bt_ctf_field_structure, parent);
1924 struct_dst = container_of(dst, struct bt_ctf_field_structure, parent);
1925
8bfa3f9c 1926 /* This field_name_to_index HT is owned by the structure field type */
87d43dc1 1927 struct_dst->field_name_to_index = struct_src->field_name_to_index;
8bfa3f9c 1928 g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len);
87d43dc1
JG
1929
1930 for (i = 0; i < struct_src->fields->len; i++) {
1931 struct bt_ctf_field *field_copy = bt_ctf_field_copy(
1932 g_ptr_array_index(struct_src->fields, i));
1933
1934 if (!field_copy) {
1935 ret = -1;
1936 goto end;
1937 }
8bfa3f9c 1938 g_ptr_array_index(struct_dst->fields, i) = field_copy;
87d43dc1
JG
1939 }
1940end:
1941 return ret;
1942}
1943
1944static
1945int bt_ctf_field_variant_copy(struct bt_ctf_field *src,
1946 struct bt_ctf_field *dst)
1947{
1948 int ret = 0;
1949 struct bt_ctf_field_variant *variant_src, *variant_dst;
1950
1951 variant_src = container_of(src, struct bt_ctf_field_variant, parent);
1952 variant_dst = container_of(dst, struct bt_ctf_field_variant, parent);
1953
1954 if (variant_src->tag) {
1955 variant_dst->tag = bt_ctf_field_copy(variant_src->tag);
1956 if (!variant_dst->tag) {
1957 ret = -1;
1958 goto end;
1959 }
1960 }
1961 if (variant_src->payload) {
1962 variant_dst->payload = bt_ctf_field_copy(variant_src->payload);
1963 if (!variant_dst->payload) {
1964 ret = -1;
1965 goto end;
1966 }
1967 }
1968end:
1969 return ret;
1970}
1971
1972static
1973int bt_ctf_field_array_copy(struct bt_ctf_field *src,
1974 struct bt_ctf_field *dst)
1975{
1976 int ret = 0, i;
1977 struct bt_ctf_field_array *array_src, *array_dst;
1978
1979 array_src = container_of(src, struct bt_ctf_field_array, parent);
1980 array_dst = container_of(dst, struct bt_ctf_field_array, parent);
1981
8bfa3f9c 1982 g_ptr_array_set_size(array_dst->elements, array_src->elements->len);
87d43dc1
JG
1983 for (i = 0; i < array_src->elements->len; i++) {
1984 struct bt_ctf_field *field_copy = bt_ctf_field_copy(
1985 g_ptr_array_index(array_src->elements, i));
1986
1987 if (!field_copy) {
1988 ret = -1;
1989 goto end;
1990 }
8bfa3f9c 1991 g_ptr_array_index(array_dst->elements, i) = field_copy;
87d43dc1
JG
1992 }
1993end:
1994 return ret;
1995}
1996
1997static
1998int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
1999 struct bt_ctf_field *dst)
2000{
2001 int ret = 0, i;
2002 struct bt_ctf_field_sequence *sequence_src, *sequence_dst;
59ab494d
PP
2003 struct bt_ctf_field *src_length;
2004 struct bt_ctf_field *dst_length;
87d43dc1
JG
2005
2006 sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
2007 sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
2008
59ab494d
PP
2009 src_length = bt_ctf_field_sequence_get_length(src);
2010
2011 if (!src_length) {
2012 /* no length set yet: keep destination sequence empty */
2013 goto end;
2014 }
2015
2016 /* copy source length */
2017 dst_length = bt_ctf_field_copy(src_length);
2018 bt_ctf_field_put(src_length);
2019
2020 if (!dst_length) {
2021 ret = -1;
2022 goto end;
2023 }
2024
2025 /* this will initialize the destination sequence's internal array */
2026 ret = bt_ctf_field_sequence_set_length(dst, dst_length);
2027 bt_ctf_field_put(dst_length);
2028
2029 if (ret) {
2030 goto end;
2031 }
2032
2033 assert(sequence_dst->elements->len == sequence_src->elements->len);
2034
87d43dc1
JG
2035 for (i = 0; i < sequence_src->elements->len; i++) {
2036 struct bt_ctf_field *field_copy = bt_ctf_field_copy(
2037 g_ptr_array_index(sequence_src->elements, i));
2038
2039 if (!field_copy) {
2040 ret = -1;
2041 goto end;
2042 }
59ab494d 2043
8bfa3f9c 2044 g_ptr_array_index(sequence_dst->elements, i) = field_copy;
87d43dc1
JG
2045 }
2046end:
2047 return ret;
2048}
2049
2050static
2051int bt_ctf_field_string_copy(struct bt_ctf_field *src,
2052 struct bt_ctf_field *dst)
2053{
2054 int ret = 0;
2055 struct bt_ctf_field_string *string_src, *string_dst;
2056
2057 string_src = container_of(src, struct bt_ctf_field_string, parent);
2058 string_dst = container_of(dst, struct bt_ctf_field_string, parent);
2059
2060 if (string_src->payload) {
2061 string_dst->payload = g_string_new(string_src->payload->str);
2062 if (!string_dst->payload) {
2063 ret = -1;
2064 goto end;
2065 }
2066 }
2067end:
2068 return ret;
2069}
2070
273b65be
JG
2071static
2072int increase_packet_size(struct ctf_stream_pos *pos)
2073{
2074 int ret;
2075
2076 assert(pos);
2077 ret = munmap_align(pos->base_mma);
2078 if (ret) {
2079 goto end;
2080 }
2081
2082 pos->packet_size += PACKET_LEN_INCREMENT;
2083 ret = posix_fallocate(pos->fd, pos->mmap_offset,
2084 pos->packet_size / CHAR_BIT);
2085 if (ret) {
2086 goto end;
2087 }
2088
2089 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
2090 pos->flags, pos->fd, pos->mmap_offset);
2091 if (pos->base_mma == MAP_FAILED) {
2092 ret = -1;
2093 }
2094end:
2095 return ret;
2096}
This page took 0.108303 seconds and 4 git commands to generate.