Unify reference counting using a common bt_object base
[babeltrace.git] / formats / ctf / ir / stream-class.c
CommitLineData
11b0cdc8 1/*
3f043b05 2 * stream-class.c
11b0cdc8 3 *
d2dc44b6 4 * Babeltrace CTF IR - Stream Class
11b0cdc8 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
11b0cdc8
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/ctf-writer/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/event-types-internal.h>
34#include <babeltrace/ctf-ir/event-fields-internal.h>
35#include <babeltrace/ctf-writer/stream.h>
36#include <babeltrace/ctf-ir/stream-class-internal.h>
26079216 37#include <babeltrace/ctf-ir/visitor-internal.h>
11b0cdc8 38#include <babeltrace/ctf-writer/functor-internal.h>
654c1444 39#include <babeltrace/ctf-ir/utils.h>
83509119 40#include <babeltrace/ref.h>
11b0cdc8
JG
41#include <babeltrace/compiler.h>
42#include <babeltrace/align.h>
43
44static
83509119 45void bt_ctf_stream_class_destroy(struct bt_object *obj);
11b0cdc8 46static
662e778c 47int init_event_header(struct bt_ctf_stream_class *stream_class);
11b0cdc8 48static
662e778c 49int init_packet_context(struct bt_ctf_stream_class *stream_class);
11b0cdc8
JG
50
51struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
52{
12c8a1a3 53 int ret;
11b0cdc8
JG
54 struct bt_ctf_stream_class *stream_class = NULL;
55
3ea33115 56 if (name && bt_ctf_validate_identifier(name)) {
11b0cdc8
JG
57 goto error;
58 }
59
60 stream_class = g_new0(struct bt_ctf_stream_class, 1);
61 if (!stream_class) {
62 goto error;
63 }
64
65 stream_class->name = g_string_new(name);
66 stream_class->event_classes = g_ptr_array_new_with_free_func(
83509119 67 (GDestroyNotify) bt_put);
11b0cdc8 68 if (!stream_class->event_classes) {
83509119 69 goto error;
11b0cdc8
JG
70 }
71
662e778c
JG
72 ret = init_event_header(stream_class);
73 if (ret) {
83509119 74 goto error;
662e778c
JG
75 }
76
77 ret = init_packet_context(stream_class);
12c8a1a3 78 if (ret) {
83509119 79 goto error;
12c8a1a3
JG
80 }
81
83509119 82 bt_object_init(stream_class, bt_ctf_stream_class_destroy);
11b0cdc8
JG
83 return stream_class;
84
11b0cdc8 85error:
83509119 86 BT_PUT(stream_class);
11b0cdc8
JG
87 return stream_class;
88}
89
142c5610
JG
90struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
91 struct bt_ctf_stream_class *stream_class)
92{
93 struct bt_ctf_trace *trace = NULL;
94
95 if (!stream_class) {
96 goto end;
97 }
98
99 trace = stream_class->trace;
100 if (trace) {
83509119 101 bt_get(trace);
142c5610
JG
102 }
103end:
104 return trace;
105}
106
69dc4535
JG
107const char *bt_ctf_stream_class_get_name(
108 struct bt_ctf_stream_class *stream_class)
109{
110 const char *name = NULL;
111
112 if (!stream_class) {
113 goto end;
114 }
115
116 name = stream_class->name->str;
117end:
118 return name;
119}
120
3ea33115
JG
121int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
122 const char *name)
123{
124 int ret = 0;
125
126 if (!stream_class || stream_class->frozen) {
127 ret = -1;
128 goto end;
129 }
130
131 g_string_assign(stream_class->name, name);
132end:
133 return ret;
134}
135
2f100782
JG
136struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
137 struct bt_ctf_stream_class *stream_class)
138{
139 struct bt_ctf_clock *clock = NULL;
140
141 if (!stream_class || !stream_class->clock) {
142 goto end;
143 }
144
145 clock = stream_class->clock;
83509119 146 bt_get(clock);
2f100782
JG
147end:
148 return clock;
149}
150
11b0cdc8
JG
151int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
152 struct bt_ctf_clock *clock)
153{
154 int ret = 0;
eee752e5 155 struct bt_ctf_field_type *timestamp_field = NULL;
11b0cdc8
JG
156
157 if (!stream_class || !clock || stream_class->frozen) {
158 ret = -1;
159 goto end;
160 }
161
eee752e5
JG
162 /*
163 * Look for a "timestamp" field in the stream class' event header type
164 * and map the stream's clock to that field if no current mapping is
165 * currently set.
166 */
167 timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name(
168 stream_class->event_header_type, "timestamp");
169 if (timestamp_field) {
170 struct bt_ctf_clock *mapped_clock;
171
172 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
173 timestamp_field);
174 if (mapped_clock) {
83509119 175 bt_put(mapped_clock);
eee752e5
JG
176 goto end;
177 }
178
179 ret = bt_ctf_field_type_integer_set_mapped_clock(
180 timestamp_field, clock);
181 if (ret) {
182 goto end;
183 }
184 }
185
11b0cdc8 186 if (stream_class->clock) {
83509119 187 bt_put(stream_class->clock);
11b0cdc8
JG
188 }
189
190 stream_class->clock = clock;
83509119 191 bt_get(clock);
11b0cdc8 192end:
eee752e5 193 if (timestamp_field) {
83509119 194 bt_put(timestamp_field);
eee752e5 195 }
11b0cdc8
JG
196 return ret;
197}
198
2f100782
JG
199int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
200{
201 int64_t ret;
202
203 if (!stream_class || !stream_class->id_set) {
204 ret = -1;
205 goto end;
206 }
207
208 ret = (int64_t) stream_class->id;
209end:
210 return ret;
211}
212
5ca83563
JG
213BT_HIDDEN
214int _bt_ctf_stream_class_set_id(
215 struct bt_ctf_stream_class *stream_class, uint32_t id)
216{
217 stream_class->id = id;
218 stream_class->id_set = 1;
219 return 0;
220}
221
29664b2a
PP
222struct event_class_set_stream_id_data {
223 uint32_t stream_id;
224 int ret;
225};
226
227static
228void event_class_set_stream_id(gpointer event_class, gpointer data)
229{
230 struct event_class_set_stream_id_data *typed_data = data;
231
232 typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
233 typed_data->stream_id);
234}
235
236BT_HIDDEN
237int bt_ctf_stream_class_set_id_no_check(
238 struct bt_ctf_stream_class *stream_class, uint32_t id)
2f100782
JG
239{
240 int ret = 0;
29664b2a
PP
241 struct event_class_set_stream_id_data data =
242 { .stream_id = id, .ret = 0 };
2f100782 243
29664b2a
PP
244 /*
245 * Make sure all event classes have their "stream_id" attribute
246 * set to this value.
247 */
248 g_ptr_array_foreach(stream_class->event_classes,
249 event_class_set_stream_id, &data);
250 ret = data.ret;
251 if (ret) {
2f100782
JG
252 goto end;
253 }
254
5ca83563
JG
255 ret = _bt_ctf_stream_class_set_id(stream_class, id);
256 if (ret) {
257 goto end;
258 }
2f100782
JG
259end:
260 return ret;
261}
262
29664b2a
PP
263int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
264 uint32_t id)
265{
266 int ret = 0;
267
268 if (!stream_class || stream_class->frozen) {
269 ret = -1;
270 goto end;
271 }
272
273 ret = bt_ctf_stream_class_set_id_no_check(stream_class, id);
274end:
275 return ret;
276}
277
0d23acbe
PP
278static
279void event_class_exists(gpointer element, gpointer query)
280{
281 struct bt_ctf_event_class *event_class_a = element;
282 struct search_query *search_query = query;
283 struct bt_ctf_event_class *event_class_b = search_query->value;
284 int64_t id_a, id_b;
285
286 if (search_query->value == element) {
287 search_query->found = 1;
288 goto end;
289 }
290
291 /*
292 * Two event classes cannot share the same name in a given
293 * stream class.
294 */
295 if (!strcmp(bt_ctf_event_class_get_name(event_class_a),
296 bt_ctf_event_class_get_name(event_class_b))) {
297 search_query->found = 1;
298 goto end;
299 }
300
301 /*
302 * Two event classes cannot share the same ID in a given
303 * stream class.
304 */
305 id_a = bt_ctf_event_class_get_id(event_class_a);
306 id_b = bt_ctf_event_class_get_id(event_class_b);
307
308 if (id_a < 0 || id_b < 0) {
309 /* at least one ID is not set: will be automatically set later */
310 goto end;
311 }
312
313 if (id_a == id_b) {
314 search_query->found = 1;
315 goto end;
316 }
317
318end:
319 return;
320}
321
11b0cdc8
JG
322int bt_ctf_stream_class_add_event_class(
323 struct bt_ctf_stream_class *stream_class,
324 struct bt_ctf_event_class *event_class)
325{
326 int ret = 0;
2f100782 327 int64_t event_id;
11b0cdc8
JG
328
329 if (!stream_class || !event_class) {
330 ret = -1;
331 goto end;
332 }
333
334 /* Check for duplicate event classes */
335 struct search_query query = { .value = event_class, .found = 0 };
0d23acbe
PP
336 g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
337 &query);
11b0cdc8
JG
338 if (query.found) {
339 ret = -1;
340 goto end;
341 }
342
26079216
JG
343 /*
344 * Resolve the event's sequence length and variant tags if the
345 * stream is already associated with a trace. Otherwise, this
346 * validation will be performed once the stream is registered
347 * to a trace.
348 */
349 if (stream_class->trace) {
350 ret = bt_ctf_event_class_resolve_types(event_class,
351 stream_class->trace, stream_class);
352 if (ret) {
353 goto end;
354 }
355 }
356
2f100782
JG
357 /* Only set an event id if none was explicitly set before */
358 event_id = bt_ctf_event_class_get_id(event_class);
359 if (event_id < 0) {
360 if (bt_ctf_event_class_set_id(event_class,
361 stream_class->next_event_id++)) {
362 ret = -1;
363 goto end;
364 }
365 }
366
367 ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
368 if (ret) {
11b0cdc8
JG
369 goto end;
370 }
371
29664b2a
PP
372 ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
373 if (ret) {
374 goto end;
375 }
376
83509119 377 bt_get(event_class);
11b0cdc8 378 g_ptr_array_add(stream_class->event_classes, event_class);
58203827 379 bt_ctf_event_class_freeze(event_class);
5ca83563
JG
380
381 if (stream_class->byte_order) {
382 /*
383 * Only set native byte order if it has been initialized
384 * when the stream class was added to a trace.
385 *
386 * If not set here, this will be set when the stream
387 * classe will be added to a trace.
388 */
389 bt_ctf_event_class_set_native_byte_order(event_class,
390 stream_class->byte_order);
391 }
11b0cdc8
JG
392end:
393 return ret;
394}
395
074ee56d 396int bt_ctf_stream_class_get_event_class_count(
69dc4535
JG
397 struct bt_ctf_stream_class *stream_class)
398{
074ee56d 399 int ret;
69dc4535
JG
400
401 if (!stream_class) {
402 ret = -1;
403 goto end;
404 }
405
074ee56d 406 ret = (int) stream_class->event_classes->len;
69dc4535
JG
407end:
408 return ret;
409}
410
411struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
074ee56d 412 struct bt_ctf_stream_class *stream_class, int index)
69dc4535
JG
413{
414 struct bt_ctf_event_class *event_class = NULL;
415
074ee56d
JG
416 if (!stream_class || index < 0 ||
417 index >= stream_class->event_classes->len) {
69dc4535
JG
418 goto end;
419 }
420
421 event_class = g_ptr_array_index(stream_class->event_classes, index);
83509119 422 bt_get(event_class);
69dc4535
JG
423end:
424 return event_class;
425}
426
427struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
428 struct bt_ctf_stream_class *stream_class, const char *name)
429{
430 size_t i;
69dc4535
JG
431 struct bt_ctf_event_class *event_class = NULL;
432
433 if (!stream_class || !name) {
434 goto end;
435 }
436
69dc4535 437 for (i = 0; i < stream_class->event_classes->len; i++) {
b8248cc0 438 struct bt_ctf_event_class *cur_event_class =
69dc4535 439 g_ptr_array_index(stream_class->event_classes, i);
b8248cc0
PP
440 const char *cur_event_class_name =
441 bt_ctf_event_class_get_name(cur_event_class);
69dc4535 442
b8248cc0
PP
443 if (!strcmp(name, cur_event_class_name)) {
444 event_class = cur_event_class;
83509119 445 bt_get(event_class);
69dc4535
JG
446 goto end;
447 }
448 }
449end:
450 return event_class;
451}
452
0863f950
PP
453struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
454 struct bt_ctf_stream_class *stream_class, uint32_t id)
455{
456 size_t i;
457 struct bt_ctf_event_class *event_class = NULL;
458
459 if (!stream_class) {
460 goto end;
461 }
462
463 for (i = 0; i < stream_class->event_classes->len; i++) {
464 struct bt_ctf_event_class *current_event_class =
465 g_ptr_array_index(stream_class->event_classes, i);
466
467 if (bt_ctf_event_class_get_id(current_event_class) == id) {
468 event_class = current_event_class;
83509119 469 bt_get(event_class);
0863f950
PP
470 goto end;
471 }
472 }
473end:
474 return event_class;
475}
476
12c8a1a3
JG
477struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
478 struct bt_ctf_stream_class *stream_class)
479{
480 struct bt_ctf_field_type *ret = NULL;
481
482 if (!stream_class) {
483 goto end;
484 }
485
486 assert(stream_class->packet_context_type);
83509119 487 bt_get(stream_class->packet_context_type);
12c8a1a3
JG
488 ret = stream_class->packet_context_type;
489end:
490 return ret;
491}
492
493int bt_ctf_stream_class_set_packet_context_type(
494 struct bt_ctf_stream_class *stream_class,
495 struct bt_ctf_field_type *packet_context_type)
496{
497 int ret = 0;
498
0a00bfa1 499 if (!stream_class || !packet_context_type || stream_class->frozen) {
12c8a1a3
JG
500 ret = -1;
501 goto end;
502 }
503
504 assert(stream_class->packet_context_type);
662e778c
JG
505 if (stream_class->packet_context_type == packet_context_type) {
506 goto end;
507 }
b34f4d90 508 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
12c8a1a3
JG
509 CTF_TYPE_STRUCT) {
510 /* A packet context must be a structure */
511 ret = -1;
512 goto end;
513 }
514
83509119
JG
515 bt_put(stream_class->packet_context_type);
516 bt_get(packet_context_type);
12c8a1a3
JG
517 stream_class->packet_context_type = packet_context_type;
518end:
519 return ret;
520}
521
662e778c
JG
522struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
523 struct bt_ctf_stream_class *stream_class)
524{
525 struct bt_ctf_field_type *ret = NULL;
526
527 if (!stream_class || !stream_class->event_header_type) {
528 goto end;
529 }
530
531 assert(stream_class->event_header_type);
83509119 532 bt_get(stream_class->event_header_type);
662e778c
JG
533 ret = stream_class->event_header_type;
534end:
535 return ret;
536}
537
538int bt_ctf_stream_class_set_event_header_type(
539 struct bt_ctf_stream_class *stream_class,
540 struct bt_ctf_field_type *event_header_type)
541{
542 int ret = 0;
543
544 if (!stream_class || !event_header_type || stream_class->frozen) {
545 ret = -1;
546 goto end;
547 }
548
549 assert(stream_class->event_header_type);
550 if (stream_class->event_header_type == event_header_type) {
551 goto end;
552 }
553 if (bt_ctf_field_type_get_type_id(event_header_type) !=
554 CTF_TYPE_STRUCT) {
555 /* An event header must be a structure */
556 ret = -1;
557 goto end;
558 }
559
83509119
JG
560 bt_put(stream_class->event_header_type);
561 bt_get(event_header_type);
662e778c
JG
562 stream_class->event_header_type = event_header_type;
563end:
564 return ret;
565}
566
af181248
JG
567struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
568 struct bt_ctf_stream_class *stream_class)
569{
570 struct bt_ctf_field_type *ret = NULL;
571
572 if (!stream_class || !stream_class->event_context_type) {
573 goto end;
574 }
575
576 assert(stream_class->event_context_type);
83509119 577 bt_get(stream_class->event_context_type);
af181248
JG
578 ret = stream_class->event_context_type;
579end:
580 return ret;
581}
582
583int bt_ctf_stream_class_set_event_context_type(
584 struct bt_ctf_stream_class *stream_class,
585 struct bt_ctf_field_type *event_context_type)
586{
587 int ret = 0;
588
589 if (!stream_class || !event_context_type || stream_class->frozen) {
590 ret = -1;
591 goto end;
592 }
593
594 if (bt_ctf_field_type_get_type_id(event_context_type) !=
595 CTF_TYPE_STRUCT) {
596 /* A packet context must be a structure */
597 ret = -1;
598 goto end;
599 }
600
83509119
JG
601 bt_put(stream_class->event_context_type);
602 bt_get(event_context_type);
af181248
JG
603 stream_class->event_context_type = event_context_type;
604end:
605 return ret;
606}
607
11b0cdc8
JG
608void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
609{
83509119 610 bt_get(stream_class);
11b0cdc8
JG
611}
612
613void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
614{
83509119 615 bt_put(stream_class);
11b0cdc8
JG
616}
617
618BT_HIDDEN
619void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
620{
621 if (!stream_class) {
622 return;
623 }
624
625 stream_class->frozen = 1;
662e778c 626 bt_ctf_field_type_freeze(stream_class->event_header_type);
12c8a1a3 627 bt_ctf_field_type_freeze(stream_class->packet_context_type);
af181248 628 bt_ctf_field_type_freeze(stream_class->event_context_type);
11b0cdc8 629 bt_ctf_clock_freeze(stream_class->clock);
11b0cdc8
JG
630}
631
11b0cdc8
JG
632BT_HIDDEN
633int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
634 enum bt_ctf_byte_order byte_order)
635{
5ca83563 636 int i, ret = 0;
c35a1669 637 int internal_byte_order;
11b0cdc8 638
c35a1669
JG
639 /* Note that "NATIVE" means the trace's endianness, not the host's. */
640 if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
5ca83563 641 byte_order > BT_CTF_BYTE_ORDER_NETWORK) {
c35a1669
JG
642 ret = -1;
643 goto end;
644 }
645
646 switch (byte_order) {
647 case BT_CTF_BYTE_ORDER_NETWORK:
648 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
649 internal_byte_order = BIG_ENDIAN;
650 break;
651 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
652 internal_byte_order = LITTLE_ENDIAN;
653 break;
654 default:
655 ret = -1;
11b0cdc8
JG
656 goto end;
657 }
c35a1669
JG
658
659 stream_class->byte_order = internal_byte_order;
5ca83563
JG
660
661 /* Set native byte order to little or big endian */
662 bt_ctf_field_type_set_native_byte_order(
663 stream_class->event_header_type, stream_class->byte_order);
664 bt_ctf_field_type_set_native_byte_order(
665 stream_class->packet_context_type, stream_class->byte_order);
666 bt_ctf_field_type_set_native_byte_order(
667 stream_class->event_context_type, stream_class->byte_order);
668
669 /* Set all events' native byte order */
670 for (i = 0; i < stream_class->event_classes->len; i++) {
671 bt_ctf_event_class_set_native_byte_order(
672 g_ptr_array_index(stream_class->event_classes, i),
673 stream_class->byte_order);
674 bt_ctf_event_class_freeze(
675 g_ptr_array_index(stream_class->event_classes, i));
676 }
11b0cdc8
JG
677end:
678 return ret;
679}
680
681BT_HIDDEN
682int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
683 struct metadata_context *context)
684{
2f100782 685 int64_t ret = 0;
11b0cdc8
JG
686 size_t i;
687
688 g_string_assign(context->field_name, "");
689 context->current_indentation_level = 1;
690 if (!stream_class->id_set) {
691 ret = -1;
692 goto end;
693 }
694
695 g_string_append_printf(context->string,
696 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
697 stream_class->id);
698 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
699 context);
700 if (ret) {
701 goto end;
702 }
703
704 g_string_append(context->string, ";\n\n\tpacket.context := ");
705 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
706 context);
707 if (ret) {
708 goto end;
709 }
710
711 if (stream_class->event_context_type) {
712 g_string_append(context->string, ";\n\n\tevent.context := ");
713 ret = bt_ctf_field_type_serialize(
714 stream_class->event_context_type, context);
715 if (ret) {
716 goto end;
717 }
718 }
719
720 g_string_append(context->string, ";\n};\n\n");
11b0cdc8
JG
721 for (i = 0; i < stream_class->event_classes->len; i++) {
722 struct bt_ctf_event_class *event_class =
723 stream_class->event_classes->pdata[i];
724
11b0cdc8
JG
725 ret = bt_ctf_event_class_serialize(event_class, context);
726 if (ret) {
727 goto end;
728 }
729 }
730end:
731 context->current_indentation_level = 0;
732 return ret;
733}
734
d3814b54
JG
735BT_HIDDEN
736int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class,
737 struct bt_ctf_trace *trace)
738{
739 int ret = 0;
740
741 if (!stream_class) {
742 ret = -1;
743 goto end;
744 }
745
746 if (stream_class->trace && trace) {
747 /* Already attached to a trace */
748 ret = -1;
749 goto end;
750 }
751
752 stream_class->trace = trace;
753end:
754 return ret;
755}
756
11b0cdc8 757static
83509119 758void bt_ctf_stream_class_destroy(struct bt_object *obj)
11b0cdc8
JG
759{
760 struct bt_ctf_stream_class *stream_class;
761
83509119
JG
762 stream_class = container_of(obj, struct bt_ctf_stream_class, base);
763 bt_put(stream_class->clock);
11b0cdc8
JG
764
765 if (stream_class->event_classes) {
2f100782
JG
766 size_t i;
767
768 /* Unregister this stream class from the event classes */
769 for (i = 0; i < stream_class->event_classes->len; i++) {
770 struct bt_ctf_event_class *event_class =
771 g_ptr_array_index(stream_class->event_classes,
772 i);
773
774 bt_ctf_event_class_set_stream_class(event_class, NULL);
775 }
776
11b0cdc8
JG
777 g_ptr_array_free(stream_class->event_classes, TRUE);
778 }
779
780 if (stream_class->name) {
781 g_string_free(stream_class->name, TRUE);
782 }
783
83509119
JG
784 bt_put(stream_class->event_header_type);
785 bt_put(stream_class->packet_context_type);
786 bt_put(stream_class->event_context_type);
11b0cdc8
JG
787 g_free(stream_class);
788}
789
790static
662e778c 791int init_event_header(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
792{
793 int ret = 0;
794 struct bt_ctf_field_type *event_header_type =
795 bt_ctf_field_type_structure_create();
796 struct bt_ctf_field_type *_uint32_t =
797 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
798 struct bt_ctf_field_type *_uint64_t =
799 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
800
801 if (!event_header_type) {
802 ret = -1;
803 goto end;
804 }
805
11b0cdc8
JG
806 ret = bt_ctf_field_type_structure_add_field(event_header_type,
807 _uint32_t, "id");
808 if (ret) {
809 goto end;
810 }
811
812 ret = bt_ctf_field_type_structure_add_field(event_header_type,
813 _uint64_t, "timestamp");
814 if (ret) {
815 goto end;
816 }
817
662e778c 818 if (stream_class->event_header_type) {
83509119 819 bt_put(stream_class->event_header_type);
de876b7f 820 }
662e778c 821 stream_class->event_header_type = event_header_type;
11b0cdc8
JG
822end:
823 if (ret) {
83509119 824 bt_put(event_header_type);
11b0cdc8
JG
825 }
826
83509119
JG
827 bt_put(_uint32_t);
828 bt_put(_uint64_t);
11b0cdc8
JG
829 return ret;
830}
831
832static
662e778c 833int init_packet_context(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
834{
835 int ret = 0;
836 struct bt_ctf_field_type *packet_context_type =
837 bt_ctf_field_type_structure_create();
838 struct bt_ctf_field_type *_uint64_t =
839 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
840
841 if (!packet_context_type) {
842 ret = -1;
843 goto end;
844 }
845
846 /*
847 * We create a stream packet context as proposed in the CTF
848 * specification.
849 */
11b0cdc8
JG
850 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
851 _uint64_t, "timestamp_begin");
852 if (ret) {
853 goto end;
854 }
855
856 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
857 _uint64_t, "timestamp_end");
858 if (ret) {
859 goto end;
860 }
861
862 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
863 _uint64_t, "content_size");
864 if (ret) {
865 goto end;
866 }
867
868 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
869 _uint64_t, "packet_size");
870 if (ret) {
871 goto end;
872 }
873
874 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
875 _uint64_t, "events_discarded");
876 if (ret) {
877 goto end;
878 }
879
83509119 880 bt_put(stream_class->packet_context_type);
11b0cdc8 881 stream_class->packet_context_type = packet_context_type;
11b0cdc8
JG
882end:
883 if (ret) {
83509119 884 bt_put(packet_context_type);
11b0cdc8
JG
885 goto end;
886 }
887
83509119 888 bt_put(_uint64_t);
11b0cdc8
JG
889 return ret;
890}
This page took 0.063438 seconds and 4 git commands to generate.