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