lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / trace-ir / stream-class.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/STREAM-CLASS"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/trace-const.h>
29 #include "compat/compiler.h"
30 #include "common/align.h"
31 #include "compat/endian.h"
32 #include "common/assert.h"
33 #include "lib/property.h"
34 #include <inttypes.h>
35 #include <stdint.h>
36 #include <stdbool.h>
37
38 #include "clock-class.h"
39 #include "event-class.h"
40 #include "field-class.h"
41 #include "field.h"
42 #include "field-wrapper.h"
43 #include "resolve-field-path.h"
44 #include "stream-class.h"
45 #include "trace.h"
46 #include "utils.h"
47 #include "lib/func-status.h"
48
49 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
50 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
51
52 static
53 void destroy_stream_class(struct bt_object *obj)
54 {
55 struct bt_stream_class *stream_class = (void *) obj;
56
57 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class);
58 BT_LOGD_STR("Putting default clock class.");
59 BT_OBJECT_PUT_REF_AND_RESET(stream_class->default_clock_class);
60
61 if (stream_class->event_classes) {
62 BT_LOGD_STR("Destroying event classes.");
63 g_ptr_array_free(stream_class->event_classes, TRUE);
64 stream_class->event_classes = NULL;
65 }
66
67 if (stream_class->name.str) {
68 g_string_free(stream_class->name.str, TRUE);
69 stream_class->name.str = NULL;
70 stream_class->name.value = NULL;
71 }
72
73 BT_LOGD_STR("Putting packet context field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(stream_class->packet_context_fc);
75 BT_LOGD_STR("Putting event common context field class.");
76 BT_OBJECT_PUT_REF_AND_RESET(stream_class->event_common_context_fc);
77 bt_object_pool_finalize(&stream_class->packet_context_field_pool);
78 g_free(stream_class);
79 }
80
81 static
82 void free_field_wrapper(struct bt_field_wrapper *field_wrapper,
83 struct bt_stream_class *stream_class)
84 {
85 bt_field_wrapper_destroy((void *) field_wrapper);
86 }
87
88 BT_ASSERT_PRE_FUNC
89 static
90 bool stream_class_id_is_unique(const struct bt_trace_class *tc, uint64_t id)
91 {
92 uint64_t i;
93 bool is_unique = true;
94
95 for (i = 0; i < tc->stream_classes->len; i++) {
96 const struct bt_stream_class *sc =
97 tc->stream_classes->pdata[i];
98
99 if (sc->id == id) {
100 is_unique = false;
101 goto end;
102 }
103 }
104
105 end:
106 return is_unique;
107 }
108
109 static
110 struct bt_stream_class *create_stream_class_with_id(
111 struct bt_trace_class *tc, uint64_t id)
112 {
113 struct bt_stream_class *stream_class = NULL;
114 int ret;
115
116 BT_ASSERT(tc);
117 BT_ASSERT_PRE(stream_class_id_is_unique(tc, id),
118 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64, tc, id);
119 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64,
120 tc, id);
121 stream_class = g_new0(struct bt_stream_class, 1);
122 if (!stream_class) {
123 BT_LOGE_STR("Failed to allocate one stream class.");
124 goto error;
125 }
126
127 bt_object_init_shared_with_parent(&stream_class->base,
128 destroy_stream_class);
129
130 stream_class->name.str = g_string_new(NULL);
131 if (!stream_class->name.str) {
132 BT_LOGE_STR("Failed to allocate a GString.");
133 ret = -1;
134 goto end;
135 }
136
137 stream_class->id = id;
138 stream_class->assigns_automatic_event_class_id = true;
139 stream_class->assigns_automatic_stream_id = true;
140 stream_class->event_classes = g_ptr_array_new_with_free_func(
141 (GDestroyNotify) bt_object_try_spec_release);
142 if (!stream_class->event_classes) {
143 BT_LOGE_STR("Failed to allocate a GPtrArray.");
144 goto error;
145 }
146
147 ret = bt_object_pool_initialize(&stream_class->packet_context_field_pool,
148 (bt_object_pool_new_object_func) bt_field_wrapper_new,
149 (bt_object_pool_destroy_object_func) free_field_wrapper,
150 stream_class);
151 if (ret) {
152 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
153 ret);
154 goto error;
155 }
156
157 bt_object_set_parent(&stream_class->base, &tc->base);
158 g_ptr_array_add(tc->stream_classes, stream_class);
159 bt_trace_class_freeze(tc);
160 BT_LIB_LOGD("Created stream class object: %!+S", stream_class);
161 goto end;
162
163 error:
164 BT_OBJECT_PUT_REF_AND_RESET(stream_class);
165
166 end:
167 return stream_class;
168 }
169
170 struct bt_stream_class *bt_stream_class_create(struct bt_trace_class *tc)
171 {
172 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
173 BT_ASSERT_PRE(tc->assigns_automatic_stream_class_id,
174 "Trace class does not automatically assigns stream class IDs: "
175 "%![sc-]+T", tc);
176 return create_stream_class_with_id(tc,
177 (uint64_t) tc->stream_classes->len);
178 }
179
180 struct bt_stream_class *bt_stream_class_create_with_id(
181 struct bt_trace_class *tc, uint64_t id)
182 {
183 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
184 BT_ASSERT_PRE(!tc->assigns_automatic_stream_class_id,
185 "Trace class automatically assigns stream class IDs: "
186 "%![sc-]+T", tc);
187 return create_stream_class_with_id(tc, id);
188 }
189
190 struct bt_trace_class *bt_stream_class_borrow_trace_class(
191 struct bt_stream_class *stream_class)
192 {
193 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
194 return bt_stream_class_borrow_trace_class_inline(stream_class);
195 }
196
197 const struct bt_trace_class *bt_stream_class_borrow_trace_class_const(
198 const struct bt_stream_class *stream_class)
199 {
200 return bt_stream_class_borrow_trace_class((void *) stream_class);
201 }
202
203 const char *bt_stream_class_get_name(const struct bt_stream_class *stream_class)
204 {
205 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
206 return stream_class->name.value;
207 }
208
209 enum bt_stream_class_set_name_status bt_stream_class_set_name(
210 struct bt_stream_class *stream_class,
211 const char *name)
212 {
213 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
214 BT_ASSERT_PRE_NON_NULL(name, "Name");
215 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
216 g_string_assign(stream_class->name.str, name);
217 stream_class->name.value = stream_class->name.str->str;
218 BT_LIB_LOGD("Set stream class's name: %!+S", stream_class);
219 return BT_FUNC_STATUS_OK;
220 }
221
222 uint64_t bt_stream_class_get_id(const struct bt_stream_class *stream_class)
223 {
224 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
225 return stream_class->id;
226 }
227
228 uint64_t bt_stream_class_get_event_class_count(
229 const struct bt_stream_class *stream_class)
230 {
231 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
232 return (uint64_t) stream_class->event_classes->len;
233 }
234
235 struct bt_event_class *bt_stream_class_borrow_event_class_by_index(
236 struct bt_stream_class *stream_class, uint64_t index)
237 {
238 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
239 BT_ASSERT_PRE_VALID_INDEX(index, stream_class->event_classes->len);
240 return g_ptr_array_index(stream_class->event_classes, index);
241 }
242
243 const struct bt_event_class *
244 bt_stream_class_borrow_event_class_by_index_const(
245 const struct bt_stream_class *stream_class, uint64_t index)
246 {
247 return bt_stream_class_borrow_event_class_by_index(
248 (void *) stream_class, index);
249 }
250
251 struct bt_event_class *bt_stream_class_borrow_event_class_by_id(
252 struct bt_stream_class *stream_class, uint64_t id)
253 {
254 struct bt_event_class *event_class = NULL;
255 uint64_t i;
256
257 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
258
259 for (i = 0; i < stream_class->event_classes->len; i++) {
260 struct bt_event_class *event_class_candidate =
261 g_ptr_array_index(stream_class->event_classes, i);
262
263 if (event_class_candidate->id == id) {
264 event_class = event_class_candidate;
265 goto end;
266 }
267 }
268
269 end:
270 return event_class;
271 }
272
273 const struct bt_event_class *
274 bt_stream_class_borrow_event_class_by_id_const(
275 const struct bt_stream_class *stream_class, uint64_t id)
276 {
277 return bt_stream_class_borrow_event_class_by_id(
278 (void *) stream_class, id);
279 }
280
281 const struct bt_field_class *
282 bt_stream_class_borrow_packet_context_field_class_const(
283 const struct bt_stream_class *stream_class)
284 {
285 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
286 return stream_class->packet_context_fc;
287 }
288
289 struct bt_field_class *
290 bt_stream_class_borrow_packet_context_field_class(
291 struct bt_stream_class *stream_class)
292 {
293 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
294 return stream_class->packet_context_fc;
295 }
296
297 enum bt_stream_class_set_field_class_status
298 bt_stream_class_set_packet_context_field_class(
299 struct bt_stream_class *stream_class,
300 struct bt_field_class *field_class)
301 {
302 int ret;
303 struct bt_resolve_field_path_context resolve_ctx = {
304 .packet_context = field_class,
305 .event_common_context = NULL,
306 .event_specific_context = NULL,
307 .event_payload = NULL,
308 };
309
310 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
311 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
312 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
313 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
314 BT_FIELD_CLASS_TYPE_STRUCTURE,
315 "Packet context field class is not a structure field class: %!+F",
316 field_class);
317 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
318 if (ret) {
319 /*
320 * This is the only reason for which
321 * bt_resolve_field_paths() can fail: anything else
322 * would be because a precondition is not satisfied.
323 */
324 ret = BT_FUNC_STATUS_MEMORY_ERROR;
325 goto end;
326 }
327
328 bt_field_class_make_part_of_trace_class(field_class);
329 bt_object_put_ref(stream_class->packet_context_fc);
330 stream_class->packet_context_fc = field_class;
331 bt_object_get_no_null_check(stream_class->packet_context_fc);
332 bt_field_class_freeze(field_class);
333 BT_LIB_LOGD("Set stream class's packet context field class: %!+S",
334 stream_class);
335
336 end:
337 return ret;
338 }
339
340 const struct bt_field_class *
341 bt_stream_class_borrow_event_common_context_field_class_const(
342 const struct bt_stream_class *stream_class)
343 {
344 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
345 return stream_class->event_common_context_fc;
346 }
347
348 struct bt_field_class *
349 bt_stream_class_borrow_event_common_context_field_class(
350 struct bt_stream_class *stream_class)
351 {
352 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
353 return stream_class->event_common_context_fc;
354 }
355
356 enum bt_stream_class_set_field_class_status
357 bt_stream_class_set_event_common_context_field_class(
358 struct bt_stream_class *stream_class,
359 struct bt_field_class *field_class)
360 {
361 int ret;
362 struct bt_resolve_field_path_context resolve_ctx = {
363 .packet_context = NULL,
364 .event_common_context = field_class,
365 .event_specific_context = NULL,
366 .event_payload = NULL,
367 };
368
369 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
370 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
371 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
372 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
373 BT_FIELD_CLASS_TYPE_STRUCTURE,
374 "Event common context field class is not a structure field class: %!+F",
375 field_class);
376 resolve_ctx.packet_context = stream_class->packet_context_fc;
377 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
378 if (ret) {
379 /*
380 * This is the only reason for which
381 * bt_resolve_field_paths() can fail: anything else
382 * would be because a precondition is not satisfied.
383 */
384 ret = BT_FUNC_STATUS_MEMORY_ERROR;
385 goto end;
386 }
387
388 bt_field_class_make_part_of_trace_class(field_class);
389 bt_object_put_ref(stream_class->event_common_context_fc);
390 stream_class->event_common_context_fc = field_class;
391 bt_object_get_no_null_check(stream_class->event_common_context_fc);
392 bt_field_class_freeze(field_class);
393 BT_LIB_LOGD("Set stream class's event common context field class: %!+S",
394 stream_class);
395
396 end:
397 return ret;
398 }
399
400 BT_HIDDEN
401 void _bt_stream_class_freeze(const struct bt_stream_class *stream_class)
402 {
403 /* The field classes and default clock class are already frozen */
404 BT_ASSERT(stream_class);
405 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class);
406 ((struct bt_stream_class *) stream_class)->frozen = true;
407 }
408
409 enum bt_stream_class_set_default_clock_class_status
410 bt_stream_class_set_default_clock_class(
411 struct bt_stream_class *stream_class,
412 struct bt_clock_class *clock_class)
413 {
414 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
415 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
416 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
417 bt_object_put_ref(stream_class->default_clock_class);
418 stream_class->default_clock_class = clock_class;
419 bt_object_get_no_null_check(stream_class->default_clock_class);
420 bt_clock_class_freeze(clock_class);
421 BT_LIB_LOGD("Set stream class's default clock class: %!+S",
422 stream_class);
423 return BT_FUNC_STATUS_OK;
424 }
425
426 struct bt_clock_class *bt_stream_class_borrow_default_clock_class(
427 struct bt_stream_class *stream_class)
428 {
429 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
430 return stream_class->default_clock_class;
431 }
432
433 const struct bt_clock_class *bt_stream_class_borrow_default_clock_class_const(
434 const struct bt_stream_class *stream_class)
435 {
436 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
437 return stream_class->default_clock_class;
438 }
439
440 bt_bool bt_stream_class_assigns_automatic_event_class_id(
441 const struct bt_stream_class *stream_class)
442 {
443 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
444 return (bt_bool) stream_class->assigns_automatic_event_class_id;
445 }
446
447 void bt_stream_class_set_assigns_automatic_event_class_id(
448 struct bt_stream_class *stream_class,
449 bt_bool value)
450 {
451 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
452 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
453 stream_class->assigns_automatic_event_class_id = (bool) value;
454 BT_LIB_LOGD("Set stream class's automatic event class ID "
455 "assignment property: %!+S", stream_class);
456 }
457
458 bt_bool bt_stream_class_packets_have_beginning_default_clock_snapshot(
459 const struct bt_stream_class *stream_class)
460 {
461 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
462 return (bt_bool) stream_class->packets_have_beginning_default_clock_snapshot;
463 }
464
465 void bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
466 struct bt_stream_class *stream_class, bt_bool value)
467 {
468 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
469 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
470 BT_ASSERT_PRE(!value || stream_class->default_clock_class,
471 "Stream class has no default clock class: %!+S", stream_class);
472 stream_class->packets_have_beginning_default_clock_snapshot =
473 (bool) value;
474 BT_LIB_LOGD("Set stream class's \"packets have default beginning "
475 "clock snapshot\" property: %!+S", stream_class);
476 }
477
478 bt_bool bt_stream_class_packets_have_end_default_clock_snapshot(
479 const struct bt_stream_class *stream_class)
480 {
481 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
482 return (bt_bool) stream_class->packets_have_end_default_clock_snapshot;
483 }
484
485 void bt_stream_class_set_packets_have_end_default_clock_snapshot(
486 struct bt_stream_class *stream_class, bt_bool value)
487 {
488 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
489 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
490 BT_ASSERT_PRE(!value || stream_class->default_clock_class,
491 "Stream class has no default clock class: %!+S", stream_class);
492 stream_class->packets_have_end_default_clock_snapshot =
493 (bool) value;
494 BT_LIB_LOGD("Set stream class's \"packets have default end "
495 "clock snapshot\" property: %!+S", stream_class);
496 }
497
498 bt_bool bt_stream_class_assigns_automatic_stream_id(
499 const struct bt_stream_class *stream_class)
500 {
501 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
502 return (bt_bool) stream_class->assigns_automatic_stream_id;
503 }
504
505 void bt_stream_class_set_supports_discarded_events(
506 struct bt_stream_class *stream_class,
507 bt_bool supports_discarded_events,
508 bt_bool with_default_clock_snapshots)
509 {
510 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
511 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
512 BT_ASSERT_PRE(supports_discarded_events ||
513 !with_default_clock_snapshots,
514 "Discarded events cannot have default clock snapshots when "
515 "not supported: %!+S", stream_class);
516 BT_ASSERT_PRE(!with_default_clock_snapshots ||
517 stream_class->default_clock_class,
518 "Stream class has no default clock class: %!+S", stream_class);
519 stream_class->supports_discarded_events =
520 (bool) supports_discarded_events;
521 stream_class->discarded_events_have_default_clock_snapshots =
522 (bool) with_default_clock_snapshots;
523 BT_LIB_LOGD("Set stream class's discarded events support property: "
524 "%!+S", stream_class);
525 }
526
527 bt_bool bt_stream_class_supports_discarded_events(
528 const struct bt_stream_class *stream_class)
529 {
530 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
531 return (bt_bool) stream_class->supports_discarded_events;
532 }
533
534 bt_bool bt_stream_class_discarded_events_have_default_clock_snapshots(
535 const struct bt_stream_class *stream_class)
536 {
537 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
538 return (bt_bool) stream_class->discarded_events_have_default_clock_snapshots;
539 }
540
541 void bt_stream_class_set_supports_discarded_packets(
542 struct bt_stream_class *stream_class,
543 bt_bool supports_discarded_packets,
544 bt_bool with_default_clock_snapshots)
545 {
546 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
547 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
548 BT_ASSERT_PRE(supports_discarded_packets ||
549 !with_default_clock_snapshots,
550 "Discarded packets cannot have default clock snapshots when "
551 "not supported: %!+S", stream_class);
552 BT_ASSERT_PRE(!with_default_clock_snapshots ||
553 stream_class->default_clock_class,
554 "Stream class has no default clock class: %!+S", stream_class);
555 stream_class->supports_discarded_packets =
556 (bool) supports_discarded_packets;
557 stream_class->discarded_packets_have_default_clock_snapshots =
558 (bool) with_default_clock_snapshots;
559 BT_LIB_LOGD("Set stream class's discarded packets support property: "
560 "%!+S", stream_class);
561 }
562
563 bt_bool bt_stream_class_supports_discarded_packets(
564 const struct bt_stream_class *stream_class)
565 {
566 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
567 return (bt_bool) stream_class->supports_discarded_packets;
568 }
569
570 bt_bool bt_stream_class_discarded_packets_have_default_clock_snapshots(
571 const struct bt_stream_class *stream_class)
572 {
573 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
574 return (bt_bool) stream_class->discarded_packets_have_default_clock_snapshots;
575 }
576
577 void bt_stream_class_set_assigns_automatic_stream_id(
578 struct bt_stream_class *stream_class,
579 bt_bool value)
580 {
581 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
582 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
583 stream_class->assigns_automatic_stream_id = (bool) value;
584 BT_LIB_LOGD("Set stream class's automatic stream ID "
585 "assignment property: %!+S", stream_class);
586 }
587
588 void bt_stream_class_get_ref(const struct bt_stream_class *stream_class)
589 {
590 bt_object_get_ref(stream_class);
591 }
592
593 void bt_stream_class_put_ref(const struct bt_stream_class *stream_class)
594 {
595 bt_object_put_ref(stream_class);
596 }
This page took 0.041048 seconds and 4 git commands to generate.