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