ir: freeze event on bt_ctf_stream_append_event()
[babeltrace.git] / formats / ctf / ir / stream.c
1 /*
2 * stream.c
3 *
4 * Babeltrace CTF IR - Stream
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/clock.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
32 #include <babeltrace/ctf-ir/event-internal.h>
33 #include <babeltrace/ctf-ir/field-types-internal.h>
34 #include <babeltrace/ctf-ir/fields-internal.h>
35 #include <babeltrace/ctf-ir/stream.h>
36 #include <babeltrace/ctf-ir/stream-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ref.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/compiler.h>
41 #include <babeltrace/align.h>
42 #include <babeltrace/ctf/ctf-index.h>
43
44 static
45 void bt_ctf_stream_destroy(struct bt_object *obj);
46 static
47 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
48
49 static
50 int set_packet_header_magic(struct bt_ctf_stream *stream)
51 {
52 int ret = 0;
53 struct bt_ctf_field_type *magic_field_type = NULL;
54 struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field(
55 stream->packet_header, "magic");
56
57 if (!magic_field) {
58 /* No magic field found. Not an error, skip. */
59 goto end;
60 }
61
62 if (!bt_ctf_field_validate(magic_field)) {
63 /* Value already set. Not an error, skip. */
64 goto end;
65 }
66
67 magic_field_type = bt_ctf_field_get_type(magic_field);
68 assert(magic_field_type);
69
70 if (bt_ctf_field_type_get_type_id(magic_field_type) !=
71 BT_CTF_TYPE_ID_INTEGER) {
72 /* Magic field is not an integer. Not an error, skip. */
73 goto end;
74 }
75
76 if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) {
77 /*
78 * Magic field is not of the expected size.
79 * Not an error, skip.
80 */
81 goto end;
82 }
83
84 ret = bt_ctf_field_type_integer_get_signed(magic_field_type);
85 assert(ret >= 0);
86 if (ret) {
87 ret = bt_ctf_field_signed_integer_set_value(magic_field,
88 (int64_t) 0xC1FC1FC1);
89 } else {
90 ret = bt_ctf_field_unsigned_integer_set_value(magic_field,
91 (uint64_t) 0xC1FC1FC1);
92 }
93 end:
94 bt_put(magic_field);
95 bt_put(magic_field_type);
96 return ret;
97 }
98
99 static
100 int set_packet_header_uuid(struct bt_ctf_stream *stream)
101 {
102 int i, ret = 0;
103 struct bt_ctf_trace *trace = NULL;
104 struct bt_ctf_field_type *uuid_field_type = NULL;
105 struct bt_ctf_field_type *element_field_type = NULL;
106 struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field(
107 stream->packet_header, "uuid");
108
109 if (!uuid_field) {
110 /* No uuid field found. Not an error, skip. */
111 goto end;
112 }
113
114 if (!bt_ctf_field_validate(uuid_field)) {
115 /* Value already set. Not an error, skip. */
116 goto end;
117 }
118
119 uuid_field_type = bt_ctf_field_get_type(uuid_field);
120 assert(uuid_field_type);
121 if (bt_ctf_field_type_get_type_id(uuid_field_type) !=
122 BT_CTF_TYPE_ID_ARRAY) {
123 /* UUID field is not an array. Not an error, skip. */
124 goto end;
125 }
126
127 if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) {
128 /*
129 * UUID field is not of the expected size.
130 * Not an error, skip.
131 */
132 goto end;
133 }
134
135 element_field_type = bt_ctf_field_type_array_get_element_type(
136 uuid_field_type);
137 assert(element_field_type);
138 if (bt_ctf_field_type_get_type_id(element_field_type) !=
139 BT_CTF_TYPE_ID_INTEGER) {
140 /* UUID array elements are not integers. Not an error, skip */
141 goto end;
142 }
143
144 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
145 for (i = 0; i < 16; i++) {
146 struct bt_ctf_field *uuid_element =
147 bt_ctf_field_array_get_field(uuid_field, i);
148
149 ret = bt_ctf_field_type_integer_get_signed(element_field_type);
150 assert(ret >= 0);
151
152 if (ret) {
153 ret = bt_ctf_field_signed_integer_set_value(
154 uuid_element, (int64_t) trace->uuid[i]);
155 } else {
156 ret = bt_ctf_field_unsigned_integer_set_value(
157 uuid_element,
158 (uint64_t) trace->uuid[i]);
159 }
160 bt_put(uuid_element);
161 if (ret) {
162 goto end;
163 }
164 }
165
166 end:
167 bt_put(uuid_field);
168 bt_put(uuid_field_type);
169 bt_put(element_field_type);
170 BT_PUT(trace);
171 return ret;
172 }
173 static
174 int set_packet_header_stream_id(struct bt_ctf_stream *stream)
175 {
176 int ret = 0;
177 uint32_t stream_id;
178 struct bt_ctf_field_type *stream_id_field_type = NULL;
179 struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field(
180 stream->packet_header, "stream_id");
181
182 if (!stream_id_field) {
183 /* No stream_id field found. Not an error, skip. */
184 goto end;
185 }
186
187 if (!bt_ctf_field_validate(stream_id_field)) {
188 /* Value already set. Not an error, skip. */
189 goto end;
190 }
191
192 stream_id_field_type = bt_ctf_field_get_type(stream_id_field);
193 assert(stream_id_field_type);
194 if (bt_ctf_field_type_get_type_id(stream_id_field_type) !=
195 BT_CTF_TYPE_ID_INTEGER) {
196 /* stream_id field is not an integer. Not an error, skip. */
197 goto end;
198 }
199
200 stream_id = stream->stream_class->id;
201 ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type);
202 assert(ret >= 0);
203 if (ret) {
204 ret = bt_ctf_field_signed_integer_set_value(stream_id_field,
205 (int64_t) stream_id);
206 } else {
207 ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field,
208 (uint64_t) stream_id);
209 }
210 end:
211 bt_put(stream_id_field);
212 bt_put(stream_id_field_type);
213 return ret;
214 }
215
216 static
217 int set_packet_header(struct bt_ctf_stream *stream)
218 {
219 int ret;
220
221 ret = set_packet_header_magic(stream);
222 if (ret) {
223 goto end;
224 }
225
226 ret = set_packet_header_uuid(stream);
227 if (ret) {
228 goto end;
229 }
230
231 ret = set_packet_header_stream_id(stream);
232 if (ret) {
233 goto end;
234 }
235 end:
236 return ret;
237 }
238
239 static
240 void release_event(struct bt_ctf_event *event)
241 {
242 if (bt_object_get_ref_count(event)) {
243 /*
244 * The event is being orphaned, but it must guarantee the
245 * existence of its event class for the duration of its
246 * lifetime.
247 */
248 bt_get(event->event_class);
249 BT_PUT(event->base.parent);
250 } else {
251 bt_object_release(event);
252 }
253 }
254
255 BT_HIDDEN
256 struct bt_ctf_stream *bt_ctf_stream_create(
257 struct bt_ctf_stream_class *stream_class,
258 struct bt_ctf_trace *trace)
259 {
260 int ret;
261 struct bt_ctf_stream *stream = NULL;
262
263 if (!stream_class || !trace) {
264 goto end;
265 }
266
267 stream = g_new0(struct bt_ctf_stream, 1);
268 if (!stream) {
269 goto end;
270 }
271
272 bt_object_init(stream, bt_ctf_stream_destroy);
273 /*
274 * Acquire reference to parent since stream will become publicly
275 * reachable; it needs its parent to remain valid.
276 */
277 bt_object_set_parent(stream, trace);
278 stream->packet_context = bt_ctf_field_create(
279 stream_class->packet_context_type);
280 if (!stream->packet_context) {
281 goto error;
282 }
283
284 /* Initialize events_discarded */
285 ret = set_structure_field_integer(stream->packet_context,
286 "events_discarded", 0);
287 if (ret) {
288 goto error;
289 }
290
291 stream->pos.fd = -1;
292 stream->id = stream_class->next_stream_id++;
293 stream->stream_class = stream_class;
294 stream->events = g_ptr_array_new_with_free_func(
295 (GDestroyNotify) release_event);
296 if (!stream->events) {
297 goto error;
298 }
299
300 /* A trace is not allowed to have a NULL packet header */
301 assert(trace->packet_header_type);
302 stream->packet_header = bt_ctf_field_create(trace->packet_header_type);
303 if (!stream->packet_header) {
304 goto error;
305 }
306 /*
307 * Attempt to populate the default trace packet header fields
308 * (magic, uuid and stream_id). This will _not_ fail shall the
309 * fields not be found or be of an incompatible type; they will
310 * simply not be populated automatically. The user will have to
311 * make sure to set the trace packet header fields himself before
312 * flushing.
313 */
314 ret = set_packet_header(stream);
315 if (ret) {
316 goto error;
317 }
318 end:
319 return stream;
320 error:
321 BT_PUT(stream);
322 bt_put(trace);
323 return stream;
324 }
325
326 BT_HIDDEN
327 int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
328 {
329 int ret = 0;
330
331 if (stream->pos.fd != -1) {
332 ret = -1;
333 goto end;
334 }
335
336 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
337 stream->pos.fd = fd;
338 end:
339 return ret;
340 }
341
342 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
343 struct bt_ctf_stream *stream)
344 {
345 struct bt_ctf_stream_class *stream_class = NULL;
346
347 if (!stream) {
348 goto end;
349 }
350
351 stream_class = stream->stream_class;
352 bt_get(stream_class);
353 end:
354 return stream_class;
355 }
356
357 int bt_ctf_stream_get_discarded_events_count(
358 struct bt_ctf_stream *stream, uint64_t *count)
359 {
360 int64_t ret = 0;
361 int field_signed;
362 struct bt_ctf_field *events_discarded_field = NULL;
363 struct bt_ctf_field_type *events_discarded_field_type = NULL;
364
365 if (!stream || !count || !stream->packet_context) {
366 ret = -1;
367 goto end;
368 }
369
370 events_discarded_field = bt_ctf_field_structure_get_field(
371 stream->packet_context, "events_discarded");
372 if (!events_discarded_field) {
373 ret = -1;
374 goto end;
375 }
376
377 events_discarded_field_type = bt_ctf_field_get_type(
378 events_discarded_field);
379 if (!events_discarded_field_type) {
380 ret = -1;
381 goto end;
382 }
383
384 field_signed = bt_ctf_field_type_integer_get_signed(
385 events_discarded_field_type);
386 if (field_signed < 0) {
387 ret = field_signed;
388 goto end;
389 }
390
391 if (field_signed) {
392 int64_t signed_count;
393
394 ret = bt_ctf_field_signed_integer_get_value(
395 events_discarded_field, &signed_count);
396 if (ret) {
397 goto end;
398 }
399 if (signed_count < 0) {
400 /* Invalid value */
401 ret = -1;
402 goto end;
403 }
404 *count = (uint64_t) signed_count;
405 } else {
406 ret = bt_ctf_field_unsigned_integer_get_value(
407 events_discarded_field, count);
408 if (ret) {
409 goto end;
410 }
411 }
412 end:
413 bt_put(events_discarded_field);
414 bt_put(events_discarded_field_type);
415 return ret;
416 }
417
418 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
419 uint64_t event_count)
420 {
421 int ret;
422 int field_signed;
423 uint64_t previous_count;
424 uint64_t new_count;
425 struct bt_ctf_field *events_discarded_field = NULL;
426 struct bt_ctf_field_type *events_discarded_field_type = NULL;
427
428 if (!stream || !stream->packet_context) {
429 goto end;
430 }
431
432 ret = bt_ctf_stream_get_discarded_events_count(stream,
433 &previous_count);
434 if (ret) {
435 goto end;
436 }
437
438 events_discarded_field = bt_ctf_field_structure_get_field(
439 stream->packet_context, "events_discarded");
440 if (!events_discarded_field) {
441 goto end;
442 }
443
444 events_discarded_field_type = bt_ctf_field_get_type(
445 events_discarded_field);
446 if (!events_discarded_field_type) {
447 goto end;
448 }
449
450 field_signed = bt_ctf_field_type_integer_get_signed(
451 events_discarded_field_type);
452 if (field_signed < 0) {
453 goto end;
454 }
455
456 new_count = previous_count + event_count;
457 if (field_signed) {
458 ret = bt_ctf_field_signed_integer_set_value(
459 events_discarded_field, (int64_t) new_count);
460 if (ret) {
461 goto end;
462 }
463 } else {
464 ret = bt_ctf_field_unsigned_integer_set_value(
465 events_discarded_field, new_count);
466 if (ret) {
467 goto end;
468 }
469 }
470
471 end:
472 bt_put(events_discarded_field);
473 bt_put(events_discarded_field_type);
474 }
475
476 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
477 struct bt_ctf_event *event)
478 {
479 int ret = 0;
480
481 if (!stream || !event) {
482 ret = -1;
483 goto end;
484 }
485
486 bt_object_set_parent(event, stream);
487 ret = bt_ctf_event_populate_event_header(event);
488 if (ret) {
489 goto end;
490 }
491
492 /* Make sure the various scopes of the event are set */
493 ret = bt_ctf_event_validate(event);
494 if (ret) {
495 goto end;
496 }
497
498 /* Save the new event and freeze it */
499 bt_ctf_event_freeze(event);
500 g_ptr_array_add(stream->events, event);
501
502 /*
503 * Event had to hold a reference to its event class as long as it wasn't
504 * part of the same trace hierarchy. From now on, the event and its
505 * class share the same lifetime guarantees and the reference is no
506 * longer needed.
507 */
508 bt_put(event->event_class);
509 end:
510 if (ret) {
511 /*
512 * Orphan the event; we were not succesful in associating it to
513 * a stream.
514 */
515 bt_object_set_parent(event, NULL);
516 }
517 return ret;
518 }
519
520 struct bt_ctf_field *bt_ctf_stream_get_packet_context(
521 struct bt_ctf_stream *stream)
522 {
523 struct bt_ctf_field *packet_context = NULL;
524
525 if (!stream) {
526 goto end;
527 }
528
529 packet_context = stream->packet_context;
530 if (packet_context) {
531 bt_get(packet_context);
532 }
533 end:
534 return packet_context;
535 }
536
537 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
538 struct bt_ctf_field *field)
539 {
540 int ret = 0;
541 struct bt_ctf_field_type *field_type;
542
543 if (!stream || !field) {
544 ret = -1;
545 goto end;
546 }
547
548 field_type = bt_ctf_field_get_type(field);
549 if (bt_ctf_field_type_compare(field_type,
550 stream->stream_class->packet_context_type)) {
551 ret = -1;
552 goto end;
553 }
554
555 bt_put(field_type);
556 bt_get(field);
557 bt_put(stream->packet_context);
558 stream->packet_context = field;
559 end:
560 return ret;
561 }
562
563 struct bt_ctf_field *bt_ctf_stream_get_packet_header(
564 struct bt_ctf_stream *stream)
565 {
566 struct bt_ctf_field *packet_header = NULL;
567
568 if (!stream) {
569 goto end;
570 }
571
572 packet_header = stream->packet_header;
573 if (packet_header) {
574 bt_get(packet_header);
575 }
576 end:
577 return packet_header;
578 }
579
580 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
581 struct bt_ctf_field *field)
582 {
583 int ret = 0;
584 struct bt_ctf_trace *trace = NULL;
585 struct bt_ctf_field_type *field_type = NULL;
586
587 if (!stream || !field) {
588 ret = -1;
589 goto end;
590 }
591
592 trace = (struct bt_ctf_trace *) bt_object_get_parent(stream);
593 field_type = bt_ctf_field_get_type(field);
594 if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) {
595 ret = -1;
596 goto end;
597 }
598
599 bt_get(field);
600 bt_put(stream->packet_header);
601 stream->packet_header = field;
602 end:
603 BT_PUT(trace);
604 bt_put(field_type);
605 return ret;
606 }
607
608 static
609 int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp)
610 {
611 int ret = 0;
612 struct bt_ctf_field *timestamp_field = NULL;
613 struct bt_ctf_field_type *timestamp_field_type = NULL;
614
615 timestamp_field = bt_ctf_field_structure_get_field(event_header,
616 "timestamp");
617 if (!timestamp_field) {
618 ret = -1;
619 goto end;
620 }
621
622 timestamp_field_type = bt_ctf_field_get_type(timestamp_field);
623 assert(timestamp_field_type);
624 if (bt_ctf_field_type_get_type_id(timestamp_field_type) !=
625 BT_CTF_TYPE_ID_INTEGER) {
626 ret = -1;
627 goto end;
628 }
629
630 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) {
631 int64_t val;
632
633 ret = bt_ctf_field_signed_integer_get_value(timestamp_field,
634 &val);
635 if (ret) {
636 goto end;
637 }
638 *timestamp = (uint64_t) val;
639 } else {
640 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field,
641 timestamp);
642 if (ret) {
643 goto end;
644 }
645 }
646 end:
647 bt_put(timestamp_field);
648 bt_put(timestamp_field_type);
649 return ret;
650 }
651
652 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
653 {
654 int ret = 0;
655 size_t i;
656 uint64_t timestamp_begin, timestamp_end, events_discarded;
657 struct bt_ctf_field *integer = NULL;
658 struct ctf_stream_pos packet_context_pos;
659
660 if (!stream || stream->pos.fd < 0) {
661 /*
662 * Stream does not have an associated fd. It is,
663 * therefore, not a stream being used to write events.
664 */
665 ret = -1;
666 goto end;
667 }
668
669 if (!stream->events->len) {
670 goto end;
671 }
672
673 ret = bt_ctf_field_validate(stream->packet_header);
674 if (ret) {
675 goto end;
676 }
677
678 /* mmap the next packet */
679 ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
680
681 ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos);
682 if (ret) {
683 goto end;
684 }
685
686 /* Set the default context attributes if present and unset. */
687 if (!get_event_header_timestamp(
688 ((struct bt_ctf_event *) g_ptr_array_index(
689 stream->events, 0))->event_header, &timestamp_begin)) {
690 ret = set_structure_field_integer(stream->packet_context,
691 "timestamp_begin", timestamp_begin);
692 if (ret) {
693 goto end;
694 }
695 }
696
697 if (!get_event_header_timestamp(
698 ((struct bt_ctf_event *) g_ptr_array_index(
699 stream->events, stream->events->len - 1))->event_header,
700 &timestamp_end)) {
701
702 ret = set_structure_field_integer(stream->packet_context,
703 "timestamp_end", timestamp_end);
704 if (ret) {
705 goto end;
706 }
707 }
708 ret = set_structure_field_integer(stream->packet_context,
709 "content_size", UINT64_MAX);
710 if (ret) {
711 goto end;
712 }
713
714 ret = set_structure_field_integer(stream->packet_context,
715 "packet_size", UINT64_MAX);
716 if (ret) {
717 goto end;
718 }
719
720 /* Write packet context */
721 memcpy(&packet_context_pos, &stream->pos,
722 sizeof(struct ctf_stream_pos));
723 ret = bt_ctf_field_serialize(stream->packet_context,
724 &stream->pos);
725 if (ret) {
726 goto end;
727 }
728
729 ret = bt_ctf_stream_get_discarded_events_count(stream,
730 &events_discarded);
731 if (ret) {
732 goto end;
733 }
734
735 /* Unset the packet context's fields. */
736 ret = bt_ctf_field_reset(stream->packet_context);
737 if (ret) {
738 goto end;
739 }
740
741 /* Set the previous number of discarded events. */
742 ret = set_structure_field_integer(stream->packet_context,
743 "events_discarded", events_discarded);
744 if (ret) {
745 goto end;
746 }
747
748 for (i = 0; i < stream->events->len; i++) {
749 struct bt_ctf_event *event = g_ptr_array_index(
750 stream->events, i);
751
752 ret = bt_ctf_field_reset(event->event_header);
753 if (ret) {
754 goto end;
755 }
756
757 /* Write event header */
758 ret = bt_ctf_field_serialize(event->event_header,
759 &stream->pos);
760 if (ret) {
761 goto end;
762 }
763
764 /* Write stream event context */
765 if (event->stream_event_context) {
766 ret = bt_ctf_field_serialize(
767 event->stream_event_context, &stream->pos);
768 if (ret) {
769 goto end;
770 }
771 }
772
773 /* Write event content */
774 ret = bt_ctf_event_serialize(event, &stream->pos);
775 if (ret) {
776 goto end;
777 }
778 }
779
780 /*
781 * Update the packet total size and content size and overwrite the
782 * packet context.
783 * Copy base_mma as the packet may have been remapped (e.g. when a
784 * packet is resized).
785 */
786 packet_context_pos.base_mma = stream->pos.base_mma;
787 ret = set_structure_field_integer(stream->packet_context,
788 "content_size", stream->pos.offset);
789 if (ret) {
790 goto end;
791 }
792
793 ret = set_structure_field_integer(stream->packet_context,
794 "packet_size", stream->pos.packet_size);
795 if (ret) {
796 goto end;
797 }
798
799 ret = bt_ctf_field_serialize(stream->packet_context,
800 &packet_context_pos);
801 if (ret) {
802 goto end;
803 }
804
805 g_ptr_array_set_size(stream->events, 0);
806 stream->flushed_packet_count++;
807 end:
808 bt_put(integer);
809 return ret;
810 }
811
812 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
813 {
814 bt_get(stream);
815 }
816
817 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
818 {
819 bt_put(stream);
820 }
821
822 static
823 void bt_ctf_stream_destroy(struct bt_object *obj)
824 {
825 struct bt_ctf_stream *stream;
826
827 stream = container_of(obj, struct bt_ctf_stream, base);
828 ctf_fini_pos(&stream->pos);
829 if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
830 perror("close");
831 }
832
833 if (stream->events) {
834 g_ptr_array_free(stream->events, TRUE);
835 }
836 bt_put(stream->packet_header);
837 bt_put(stream->packet_context);
838 bt_put(stream->event_context);
839 g_free(stream);
840 }
841
842 static
843 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
844 uint64_t value)
845 {
846 int ret = 0;
847 struct bt_ctf_field_type *field_type = NULL;
848 struct bt_ctf_field *integer =
849 bt_ctf_field_structure_get_field(structure, name);
850
851 if (!structure || !name) {
852 ret = -1;
853 goto end;
854 }
855
856 if (!integer) {
857 /* Field not found, not an error. */
858 goto end;
859 }
860
861 /* Make sure the payload has not already been set. */
862 if (!bt_ctf_field_validate(integer)) {
863 /* Payload already set, not an error */
864 goto end;
865 }
866
867 field_type = bt_ctf_field_get_type(integer);
868 /* Something is serioulsly wrong */
869 assert(field_type);
870 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) {
871 /*
872 * The user most likely meant for us to populate this field
873 * automatically. However, we can only do this if the field
874 * is an integer. Return an error.
875 */
876 ret = -1;
877 goto end;
878 }
879
880 if (bt_ctf_field_type_integer_get_signed(field_type)) {
881 ret = bt_ctf_field_signed_integer_set_value(integer,
882 (int64_t) value);
883 } else {
884 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
885 }
886 end:
887 bt_put(integer);
888 bt_put(field_type);
889 return ret;
890 }
This page took 0.048657 seconds and 4 git commands to generate.