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