| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2018-2019 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H |
| 8 | #define BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H |
| 9 | |
| 10 | #include <glib.h> |
| 11 | #include <stdint.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <babeltrace2/babeltrace.h> |
| 15 | |
| 16 | #include "common/assert.h" |
| 17 | #include "common/common.h" |
| 18 | #include "common/uuid.h" |
| 19 | |
| 20 | enum fs_sink_ctf_field_class_type |
| 21 | { |
| 22 | FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL, |
| 23 | FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY, |
| 24 | FS_SINK_CTF_FIELD_CLASS_TYPE_INT, |
| 25 | FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT, |
| 26 | FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, |
| 27 | FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, |
| 28 | FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, |
| 29 | FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE, |
| 30 | FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION, |
| 31 | FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, |
| 32 | }; |
| 33 | |
| 34 | struct fs_sink_ctf_field_class |
| 35 | { |
| 36 | enum fs_sink_ctf_field_class_type type; |
| 37 | |
| 38 | /* Weak */ |
| 39 | const bt_field_class *ir_fc; |
| 40 | |
| 41 | unsigned int alignment; |
| 42 | }; |
| 43 | |
| 44 | struct fs_sink_ctf_field_class_bit_array |
| 45 | { |
| 46 | struct fs_sink_ctf_field_class base; |
| 47 | unsigned int size; |
| 48 | }; |
| 49 | |
| 50 | struct fs_sink_ctf_field_class_bool |
| 51 | { |
| 52 | struct fs_sink_ctf_field_class_bit_array base; |
| 53 | }; |
| 54 | |
| 55 | struct fs_sink_ctf_field_class_int |
| 56 | { |
| 57 | struct fs_sink_ctf_field_class_bit_array base; |
| 58 | bool is_signed; |
| 59 | }; |
| 60 | |
| 61 | struct fs_sink_ctf_field_class_float |
| 62 | { |
| 63 | struct fs_sink_ctf_field_class_bit_array base; |
| 64 | }; |
| 65 | |
| 66 | struct fs_sink_ctf_field_class_string |
| 67 | { |
| 68 | struct fs_sink_ctf_field_class base; |
| 69 | }; |
| 70 | |
| 71 | struct fs_sink_ctf_named_field_class |
| 72 | { |
| 73 | GString *name; |
| 74 | |
| 75 | /* Owned by this */ |
| 76 | struct fs_sink_ctf_field_class *fc; |
| 77 | }; |
| 78 | |
| 79 | struct fs_sink_ctf_field_class_struct |
| 80 | { |
| 81 | struct fs_sink_ctf_field_class base; |
| 82 | |
| 83 | /* Array of `struct fs_sink_ctf_named_field_class` */ |
| 84 | GArray *members; |
| 85 | }; |
| 86 | |
| 87 | struct fs_sink_ctf_field_class_option |
| 88 | { |
| 89 | struct fs_sink_ctf_field_class base; |
| 90 | struct fs_sink_ctf_field_class *content_fc; |
| 91 | GString *tag_ref; |
| 92 | }; |
| 93 | |
| 94 | struct fs_sink_ctf_field_class_variant |
| 95 | { |
| 96 | struct fs_sink_ctf_field_class base; |
| 97 | GString *tag_ref; |
| 98 | bool tag_is_before; |
| 99 | |
| 100 | /* Array of `struct fs_sink_ctf_named_field_class` */ |
| 101 | GArray *options; |
| 102 | }; |
| 103 | |
| 104 | struct fs_sink_ctf_field_class_array_base |
| 105 | { |
| 106 | struct fs_sink_ctf_field_class base; |
| 107 | struct fs_sink_ctf_field_class *elem_fc; |
| 108 | }; |
| 109 | |
| 110 | struct fs_sink_ctf_field_class_array |
| 111 | { |
| 112 | struct fs_sink_ctf_field_class_array_base base; |
| 113 | uint64_t length; |
| 114 | }; |
| 115 | |
| 116 | struct fs_sink_ctf_field_class_sequence |
| 117 | { |
| 118 | struct fs_sink_ctf_field_class_array_base base; |
| 119 | GString *length_ref; |
| 120 | bool length_is_before; |
| 121 | }; |
| 122 | |
| 123 | static inline fs_sink_ctf_field_class_bit_array * |
| 124 | fs_sink_ctf_field_class_as_bit_array(fs_sink_ctf_field_class *fc) |
| 125 | { |
| 126 | BT_ASSERT_DBG(!fc || (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY || |
| 127 | fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT || |
| 128 | fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL || |
| 129 | fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT)); |
| 130 | |
| 131 | return (fs_sink_ctf_field_class_bit_array *) fc; |
| 132 | } |
| 133 | |
| 134 | static inline fs_sink_ctf_field_class_bool * |
| 135 | fs_sink_ctf_field_class_as_bool(fs_sink_ctf_field_class *fc) |
| 136 | { |
| 137 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL); |
| 138 | |
| 139 | return (fs_sink_ctf_field_class_bool *) fc; |
| 140 | } |
| 141 | |
| 142 | static inline fs_sink_ctf_field_class_int * |
| 143 | fs_sink_ctf_field_class_as_int(fs_sink_ctf_field_class *fc) |
| 144 | { |
| 145 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT); |
| 146 | |
| 147 | return (fs_sink_ctf_field_class_int *) fc; |
| 148 | } |
| 149 | |
| 150 | static inline fs_sink_ctf_field_class_float * |
| 151 | fs_sink_ctf_field_class_as_float(fs_sink_ctf_field_class *fc) |
| 152 | { |
| 153 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT); |
| 154 | |
| 155 | return (fs_sink_ctf_field_class_float *) fc; |
| 156 | } |
| 157 | |
| 158 | static inline fs_sink_ctf_field_class_string * |
| 159 | fs_sink_ctf_field_class_as_string(fs_sink_ctf_field_class *fc) |
| 160 | { |
| 161 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRING); |
| 162 | |
| 163 | return (fs_sink_ctf_field_class_string *) fc; |
| 164 | } |
| 165 | |
| 166 | static inline fs_sink_ctf_field_class_struct * |
| 167 | fs_sink_ctf_field_class_as_struct(fs_sink_ctf_field_class *fc) |
| 168 | { |
| 169 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT); |
| 170 | |
| 171 | return (fs_sink_ctf_field_class_struct *) fc; |
| 172 | } |
| 173 | |
| 174 | static inline fs_sink_ctf_field_class_array_base * |
| 175 | fs_sink_ctf_field_class_as_array_base(fs_sink_ctf_field_class *fc) |
| 176 | { |
| 177 | BT_ASSERT_DBG(!fc || (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY || |
| 178 | fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE)); |
| 179 | |
| 180 | return (fs_sink_ctf_field_class_array_base *) fc; |
| 181 | } |
| 182 | |
| 183 | static inline fs_sink_ctf_field_class_array * |
| 184 | fs_sink_ctf_field_class_as_array(fs_sink_ctf_field_class *fc) |
| 185 | { |
| 186 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY); |
| 187 | |
| 188 | return (fs_sink_ctf_field_class_array *) fc; |
| 189 | } |
| 190 | |
| 191 | static inline fs_sink_ctf_field_class_sequence * |
| 192 | fs_sink_ctf_field_class_as_sequence(fs_sink_ctf_field_class *fc) |
| 193 | { |
| 194 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE); |
| 195 | |
| 196 | return (fs_sink_ctf_field_class_sequence *) fc; |
| 197 | } |
| 198 | |
| 199 | static inline fs_sink_ctf_field_class_option * |
| 200 | fs_sink_ctf_field_class_as_option(fs_sink_ctf_field_class *fc) |
| 201 | { |
| 202 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION); |
| 203 | |
| 204 | return (fs_sink_ctf_field_class_option *) fc; |
| 205 | } |
| 206 | |
| 207 | static inline fs_sink_ctf_field_class_variant * |
| 208 | fs_sink_ctf_field_class_as_variant(fs_sink_ctf_field_class *fc) |
| 209 | { |
| 210 | BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT); |
| 211 | |
| 212 | return (fs_sink_ctf_field_class_variant *) fc; |
| 213 | } |
| 214 | |
| 215 | struct fs_sink_ctf_stream_class; |
| 216 | |
| 217 | struct fs_sink_ctf_event_class |
| 218 | { |
| 219 | /* Weak */ |
| 220 | const bt_event_class *ir_ec; |
| 221 | |
| 222 | /* Weak */ |
| 223 | struct fs_sink_ctf_stream_class *sc; |
| 224 | |
| 225 | /* Owned by this */ |
| 226 | struct fs_sink_ctf_field_class *spec_context_fc; |
| 227 | |
| 228 | /* Owned by this */ |
| 229 | struct fs_sink_ctf_field_class *payload_fc; |
| 230 | }; |
| 231 | |
| 232 | struct fs_sink_ctf_trace; |
| 233 | |
| 234 | struct fs_sink_ctf_stream_class |
| 235 | { |
| 236 | /* Weak */ |
| 237 | struct fs_sink_ctf_trace *trace; |
| 238 | |
| 239 | /* Weak */ |
| 240 | const bt_stream_class *ir_sc; |
| 241 | |
| 242 | /* Weak */ |
| 243 | const bt_clock_class *default_clock_class; |
| 244 | |
| 245 | GString *default_clock_class_name; |
| 246 | bool has_packets; |
| 247 | bool packets_have_ts_begin; |
| 248 | bool packets_have_ts_end; |
| 249 | bool has_discarded_events; |
| 250 | bool discarded_events_has_ts; |
| 251 | bool discarded_packets_has_ts; |
| 252 | |
| 253 | /* Owned by this */ |
| 254 | struct fs_sink_ctf_field_class *packet_context_fc; |
| 255 | |
| 256 | /* Owned by this */ |
| 257 | struct fs_sink_ctf_field_class *event_common_context_fc; |
| 258 | |
| 259 | /* Array of `struct fs_sink_ctf_event_class *` (owned by this) */ |
| 260 | GPtrArray *event_classes; |
| 261 | |
| 262 | /* |
| 263 | * `const bt_event_class *` (weak) -> |
| 264 | * `struct fs_sink_ctf_event_class *` (weak) |
| 265 | */ |
| 266 | GHashTable *event_classes_from_ir; |
| 267 | }; |
| 268 | |
| 269 | struct fs_sink_ctf_trace |
| 270 | { |
| 271 | /* Weak */ |
| 272 | const bt_trace *ir_trace; |
| 273 | |
| 274 | /* Weak */ |
| 275 | const bt_trace_class *ir_tc; |
| 276 | |
| 277 | bt_uuid_t uuid; |
| 278 | |
| 279 | /* Array of `struct fs_sink_ctf_stream_class *` (owned by this) */ |
| 280 | GPtrArray *stream_classes; |
| 281 | }; |
| 282 | |
| 283 | static inline void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc); |
| 284 | |
| 285 | static inline void _fs_sink_ctf_field_class_init(struct fs_sink_ctf_field_class *fc, |
| 286 | enum fs_sink_ctf_field_class_type type, |
| 287 | const bt_field_class *ir_fc, |
| 288 | unsigned int alignment) |
| 289 | { |
| 290 | BT_ASSERT(fc); |
| 291 | fc->type = type; |
| 292 | fc->ir_fc = ir_fc; |
| 293 | fc->alignment = alignment; |
| 294 | } |
| 295 | |
| 296 | static inline void |
| 297 | _fs_sink_ctf_field_class_bit_array_init(struct fs_sink_ctf_field_class_bit_array *fc, |
| 298 | enum fs_sink_ctf_field_class_type type, |
| 299 | const bt_field_class *ir_fc, unsigned int size) |
| 300 | { |
| 301 | _fs_sink_ctf_field_class_init(&fc->base, type, ir_fc, size % 8 == 0 ? 8 : 1); |
| 302 | fc->size = size; |
| 303 | } |
| 304 | |
| 305 | static inline void _fs_sink_ctf_field_class_int_init(struct fs_sink_ctf_field_class_int *fc, |
| 306 | enum fs_sink_ctf_field_class_type type, |
| 307 | const bt_field_class *ir_fc) |
| 308 | { |
| 309 | bt_field_class_type ir_fc_type = bt_field_class_get_type(ir_fc); |
| 310 | |
| 311 | _fs_sink_ctf_field_class_bit_array_init( |
| 312 | &fc->base, type, ir_fc, (unsigned int) bt_field_class_integer_get_field_value_range(ir_fc)); |
| 313 | fc->is_signed = bt_field_class_type_is(ir_fc_type, BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); |
| 314 | } |
| 315 | |
| 316 | static inline void |
| 317 | _fs_sink_ctf_named_field_class_init(struct fs_sink_ctf_named_field_class *named_fc) |
| 318 | { |
| 319 | BT_ASSERT(named_fc); |
| 320 | named_fc->name = g_string_new(NULL); |
| 321 | BT_ASSERT(named_fc->name); |
| 322 | } |
| 323 | |
| 324 | static inline void |
| 325 | _fs_sink_ctf_named_field_class_fini(struct fs_sink_ctf_named_field_class *named_fc) |
| 326 | { |
| 327 | BT_ASSERT(named_fc); |
| 328 | |
| 329 | if (named_fc->name) { |
| 330 | g_string_free(named_fc->name, TRUE); |
| 331 | named_fc->name = NULL; |
| 332 | } |
| 333 | |
| 334 | fs_sink_ctf_field_class_destroy(named_fc->fc); |
| 335 | named_fc->fc = NULL; |
| 336 | } |
| 337 | |
| 338 | static inline struct fs_sink_ctf_field_class_bit_array * |
| 339 | fs_sink_ctf_field_class_bit_array_create(const bt_field_class *ir_fc) |
| 340 | { |
| 341 | struct fs_sink_ctf_field_class_bit_array *fc = |
| 342 | g_new0(struct fs_sink_ctf_field_class_bit_array, 1); |
| 343 | |
| 344 | BT_ASSERT(fc); |
| 345 | _fs_sink_ctf_field_class_bit_array_init( |
| 346 | fc, FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY, ir_fc, |
| 347 | (unsigned int) bt_field_class_bit_array_get_length(ir_fc)); |
| 348 | return fc; |
| 349 | } |
| 350 | |
| 351 | static inline struct fs_sink_ctf_field_class_bool * |
| 352 | fs_sink_ctf_field_class_bool_create(const bt_field_class *ir_fc) |
| 353 | { |
| 354 | struct fs_sink_ctf_field_class_bool *fc = g_new0(struct fs_sink_ctf_field_class_bool, 1); |
| 355 | |
| 356 | BT_ASSERT(fc); |
| 357 | |
| 358 | /* |
| 359 | * CTF 1.8 has no boolean field class type, so this component |
| 360 | * translates it to an 8-bit unsigned integer field class. |
| 361 | */ |
| 362 | _fs_sink_ctf_field_class_bit_array_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL, ir_fc, 8); |
| 363 | return fc; |
| 364 | } |
| 365 | |
| 366 | static inline struct fs_sink_ctf_field_class_int * |
| 367 | fs_sink_ctf_field_class_int_create(const bt_field_class *ir_fc) |
| 368 | { |
| 369 | struct fs_sink_ctf_field_class_int *fc = g_new0(struct fs_sink_ctf_field_class_int, 1); |
| 370 | |
| 371 | BT_ASSERT(fc); |
| 372 | _fs_sink_ctf_field_class_int_init(fc, FS_SINK_CTF_FIELD_CLASS_TYPE_INT, ir_fc); |
| 373 | return fc; |
| 374 | } |
| 375 | |
| 376 | static inline struct fs_sink_ctf_field_class_float * |
| 377 | fs_sink_ctf_field_class_float_create(const bt_field_class *ir_fc) |
| 378 | { |
| 379 | struct fs_sink_ctf_field_class_float *fc = g_new0(struct fs_sink_ctf_field_class_float, 1); |
| 380 | |
| 381 | BT_ASSERT(fc); |
| 382 | _fs_sink_ctf_field_class_bit_array_init( |
| 383 | &fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT, ir_fc, |
| 384 | bt_field_class_get_type(ir_fc) == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL ? 32 : 64); |
| 385 | return fc; |
| 386 | } |
| 387 | |
| 388 | static inline struct fs_sink_ctf_field_class_string * |
| 389 | fs_sink_ctf_field_class_string_create(const bt_field_class *ir_fc) |
| 390 | { |
| 391 | struct fs_sink_ctf_field_class_string *fc = g_new0(struct fs_sink_ctf_field_class_string, 1); |
| 392 | |
| 393 | BT_ASSERT(fc); |
| 394 | _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, ir_fc, 8); |
| 395 | return fc; |
| 396 | } |
| 397 | |
| 398 | static inline struct fs_sink_ctf_field_class_struct * |
| 399 | fs_sink_ctf_field_class_struct_create_empty(const bt_field_class *ir_fc) |
| 400 | { |
| 401 | struct fs_sink_ctf_field_class_struct *fc = g_new0(struct fs_sink_ctf_field_class_struct, 1); |
| 402 | |
| 403 | BT_ASSERT(fc); |
| 404 | _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, ir_fc, 1); |
| 405 | fc->members = g_array_new(FALSE, TRUE, sizeof(struct fs_sink_ctf_named_field_class)); |
| 406 | BT_ASSERT(fc->members); |
| 407 | return fc; |
| 408 | } |
| 409 | |
| 410 | static inline struct fs_sink_ctf_field_class_option * |
| 411 | fs_sink_ctf_field_class_option_create_empty(const bt_field_class *ir_fc) |
| 412 | { |
| 413 | struct fs_sink_ctf_field_class_option *fc = g_new0(struct fs_sink_ctf_field_class_option, 1); |
| 414 | |
| 415 | BT_ASSERT(fc); |
| 416 | _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION, ir_fc, 1); |
| 417 | fc->tag_ref = g_string_new(NULL); |
| 418 | BT_ASSERT(fc->tag_ref); |
| 419 | return fc; |
| 420 | } |
| 421 | |
| 422 | static inline struct fs_sink_ctf_field_class_variant * |
| 423 | fs_sink_ctf_field_class_variant_create_empty(const bt_field_class *ir_fc) |
| 424 | { |
| 425 | struct fs_sink_ctf_field_class_variant *fc = g_new0(struct fs_sink_ctf_field_class_variant, 1); |
| 426 | |
| 427 | BT_ASSERT(fc); |
| 428 | _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, ir_fc, 1); |
| 429 | fc->options = g_array_new(FALSE, TRUE, sizeof(struct fs_sink_ctf_named_field_class)); |
| 430 | BT_ASSERT(fc->options); |
| 431 | fc->tag_ref = g_string_new(NULL); |
| 432 | BT_ASSERT(fc->tag_ref); |
| 433 | fc->tag_is_before = bt_field_class_get_type(fc->base.ir_fc) == |
| 434 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD; |
| 435 | return fc; |
| 436 | } |
| 437 | |
| 438 | static inline struct fs_sink_ctf_field_class_array * |
| 439 | fs_sink_ctf_field_class_array_create_empty(const bt_field_class *ir_fc) |
| 440 | { |
| 441 | struct fs_sink_ctf_field_class_array *fc = g_new0(struct fs_sink_ctf_field_class_array, 1); |
| 442 | |
| 443 | BT_ASSERT(fc); |
| 444 | _fs_sink_ctf_field_class_init(&fc->base.base, FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, ir_fc, 1); |
| 445 | fc->length = bt_field_class_array_static_get_length(ir_fc); |
| 446 | return fc; |
| 447 | } |
| 448 | |
| 449 | static inline struct fs_sink_ctf_field_class_sequence * |
| 450 | fs_sink_ctf_field_class_sequence_create_empty(const bt_field_class *ir_fc) |
| 451 | { |
| 452 | struct fs_sink_ctf_field_class_sequence *fc = |
| 453 | g_new0(struct fs_sink_ctf_field_class_sequence, 1); |
| 454 | |
| 455 | BT_ASSERT(fc); |
| 456 | _fs_sink_ctf_field_class_init(&fc->base.base, FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE, ir_fc, 1); |
| 457 | fc->length_ref = g_string_new(NULL); |
| 458 | BT_ASSERT(fc->length_ref); |
| 459 | fc->length_is_before = |
| 460 | bt_field_class_get_type(ir_fc) == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD; |
| 461 | return fc; |
| 462 | } |
| 463 | |
| 464 | static inline struct fs_sink_ctf_named_field_class * |
| 465 | fs_sink_ctf_field_class_struct_borrow_member_by_index(struct fs_sink_ctf_field_class_struct *fc, |
| 466 | uint64_t index); |
| 467 | |
| 468 | static inline struct fs_sink_ctf_named_field_class * |
| 469 | fs_sink_ctf_field_class_variant_borrow_option_by_index(struct fs_sink_ctf_field_class_variant *fc, |
| 470 | uint64_t index); |
| 471 | |
| 472 | static inline void _fs_sink_ctf_field_class_fini(struct fs_sink_ctf_field_class *fc) |
| 473 | { |
| 474 | BT_ASSERT(fc); |
| 475 | } |
| 476 | |
| 477 | static inline void |
| 478 | _fs_sink_ctf_field_class_bit_array_destroy(struct fs_sink_ctf_field_class_bit_array *fc) |
| 479 | { |
| 480 | BT_ASSERT(fc); |
| 481 | _fs_sink_ctf_field_class_fini(&fc->base); |
| 482 | g_free(fc); |
| 483 | } |
| 484 | |
| 485 | static inline void _fs_sink_ctf_field_class_bool_destroy(struct fs_sink_ctf_field_class_bool *fc) |
| 486 | { |
| 487 | BT_ASSERT(fc); |
| 488 | _fs_sink_ctf_field_class_fini(&fc->base.base); |
| 489 | g_free(fc); |
| 490 | } |
| 491 | |
| 492 | static inline void _fs_sink_ctf_field_class_int_destroy(struct fs_sink_ctf_field_class_int *fc) |
| 493 | { |
| 494 | BT_ASSERT(fc); |
| 495 | _fs_sink_ctf_field_class_fini(&fc->base.base); |
| 496 | g_free(fc); |
| 497 | } |
| 498 | |
| 499 | static inline void _fs_sink_ctf_field_class_float_destroy(struct fs_sink_ctf_field_class_float *fc) |
| 500 | { |
| 501 | BT_ASSERT(fc); |
| 502 | _fs_sink_ctf_field_class_fini(&fc->base.base); |
| 503 | g_free(fc); |
| 504 | } |
| 505 | |
| 506 | static inline void |
| 507 | _fs_sink_ctf_field_class_string_destroy(struct fs_sink_ctf_field_class_string *fc) |
| 508 | { |
| 509 | BT_ASSERT(fc); |
| 510 | _fs_sink_ctf_field_class_fini(&fc->base); |
| 511 | g_free(fc); |
| 512 | } |
| 513 | |
| 514 | static inline void |
| 515 | _fs_sink_ctf_field_class_struct_destroy(struct fs_sink_ctf_field_class_struct *fc) |
| 516 | { |
| 517 | BT_ASSERT(fc); |
| 518 | _fs_sink_ctf_field_class_fini(&fc->base); |
| 519 | |
| 520 | if (fc->members) { |
| 521 | uint64_t i; |
| 522 | |
| 523 | for (i = 0; i < fc->members->len; i++) { |
| 524 | struct fs_sink_ctf_named_field_class *named_fc = |
| 525 | fs_sink_ctf_field_class_struct_borrow_member_by_index(fc, i); |
| 526 | |
| 527 | _fs_sink_ctf_named_field_class_fini(named_fc); |
| 528 | } |
| 529 | |
| 530 | g_array_free(fc->members, TRUE); |
| 531 | fc->members = NULL; |
| 532 | } |
| 533 | |
| 534 | g_free(fc); |
| 535 | } |
| 536 | |
| 537 | static inline void |
| 538 | _fs_sink_ctf_field_class_array_base_fini(struct fs_sink_ctf_field_class_array_base *fc) |
| 539 | { |
| 540 | BT_ASSERT(fc); |
| 541 | _fs_sink_ctf_field_class_fini(&fc->base); |
| 542 | fs_sink_ctf_field_class_destroy(fc->elem_fc); |
| 543 | fc->elem_fc = NULL; |
| 544 | } |
| 545 | |
| 546 | static inline void _fs_sink_ctf_field_class_array_destroy(struct fs_sink_ctf_field_class_array *fc) |
| 547 | { |
| 548 | BT_ASSERT(fc); |
| 549 | _fs_sink_ctf_field_class_array_base_fini(&fc->base); |
| 550 | g_free(fc); |
| 551 | } |
| 552 | |
| 553 | static inline void |
| 554 | _fs_sink_ctf_field_class_sequence_destroy(struct fs_sink_ctf_field_class_sequence *fc) |
| 555 | { |
| 556 | BT_ASSERT(fc); |
| 557 | _fs_sink_ctf_field_class_array_base_fini(&fc->base); |
| 558 | |
| 559 | if (fc->length_ref) { |
| 560 | g_string_free(fc->length_ref, TRUE); |
| 561 | fc->length_ref = NULL; |
| 562 | } |
| 563 | |
| 564 | g_free(fc); |
| 565 | } |
| 566 | |
| 567 | static inline void |
| 568 | _fs_sink_ctf_field_class_option_destroy(struct fs_sink_ctf_field_class_option *fc) |
| 569 | { |
| 570 | BT_ASSERT(fc); |
| 571 | _fs_sink_ctf_field_class_fini(&fc->base); |
| 572 | fs_sink_ctf_field_class_destroy(fc->content_fc); |
| 573 | |
| 574 | if (fc->tag_ref) { |
| 575 | g_string_free(fc->tag_ref, TRUE); |
| 576 | fc->tag_ref = NULL; |
| 577 | } |
| 578 | |
| 579 | g_free(fc); |
| 580 | } |
| 581 | |
| 582 | static inline void |
| 583 | _fs_sink_ctf_field_class_variant_destroy(struct fs_sink_ctf_field_class_variant *fc) |
| 584 | { |
| 585 | BT_ASSERT(fc); |
| 586 | _fs_sink_ctf_field_class_fini(&fc->base); |
| 587 | |
| 588 | if (fc->options) { |
| 589 | uint64_t i; |
| 590 | |
| 591 | for (i = 0; i < fc->options->len; i++) { |
| 592 | struct fs_sink_ctf_named_field_class *named_fc = |
| 593 | fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, i); |
| 594 | |
| 595 | _fs_sink_ctf_named_field_class_fini(named_fc); |
| 596 | } |
| 597 | |
| 598 | g_array_free(fc->options, TRUE); |
| 599 | fc->options = NULL; |
| 600 | } |
| 601 | |
| 602 | if (fc->tag_ref) { |
| 603 | g_string_free(fc->tag_ref, TRUE); |
| 604 | fc->tag_ref = NULL; |
| 605 | } |
| 606 | |
| 607 | g_free(fc); |
| 608 | } |
| 609 | |
| 610 | static inline void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc) |
| 611 | { |
| 612 | if (!fc) { |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | switch (fc->type) { |
| 617 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL: |
| 618 | _fs_sink_ctf_field_class_bool_destroy(fs_sink_ctf_field_class_as_bool(fc)); |
| 619 | break; |
| 620 | case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY: |
| 621 | _fs_sink_ctf_field_class_bit_array_destroy(fs_sink_ctf_field_class_as_bit_array(fc)); |
| 622 | break; |
| 623 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: |
| 624 | _fs_sink_ctf_field_class_int_destroy(fs_sink_ctf_field_class_as_int(fc)); |
| 625 | break; |
| 626 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: |
| 627 | _fs_sink_ctf_field_class_float_destroy(fs_sink_ctf_field_class_as_float(fc)); |
| 628 | break; |
| 629 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: |
| 630 | _fs_sink_ctf_field_class_string_destroy(fs_sink_ctf_field_class_as_string(fc)); |
| 631 | break; |
| 632 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 633 | _fs_sink_ctf_field_class_struct_destroy(fs_sink_ctf_field_class_as_struct(fc)); |
| 634 | break; |
| 635 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 636 | _fs_sink_ctf_field_class_array_destroy(fs_sink_ctf_field_class_as_array(fc)); |
| 637 | break; |
| 638 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 639 | _fs_sink_ctf_field_class_sequence_destroy(fs_sink_ctf_field_class_as_sequence(fc)); |
| 640 | break; |
| 641 | case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION: |
| 642 | _fs_sink_ctf_field_class_option_destroy(fs_sink_ctf_field_class_as_option(fc)); |
| 643 | break; |
| 644 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 645 | _fs_sink_ctf_field_class_variant_destroy(fs_sink_ctf_field_class_as_variant(fc)); |
| 646 | break; |
| 647 | default: |
| 648 | bt_common_abort(); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | static inline struct fs_sink_ctf_named_field_class * |
| 653 | fs_sink_ctf_field_class_struct_borrow_member_by_index(struct fs_sink_ctf_field_class_struct *fc, |
| 654 | uint64_t index) |
| 655 | { |
| 656 | BT_ASSERT_DBG(fc); |
| 657 | BT_ASSERT_DBG(index < fc->members->len); |
| 658 | return &bt_g_array_index(fc->members, struct fs_sink_ctf_named_field_class, index); |
| 659 | } |
| 660 | |
| 661 | static inline struct fs_sink_ctf_named_field_class * |
| 662 | fs_sink_ctf_field_class_struct_borrow_member_by_name(struct fs_sink_ctf_field_class_struct *fc, |
| 663 | const char *name) |
| 664 | { |
| 665 | uint64_t i; |
| 666 | struct fs_sink_ctf_named_field_class *ret_named_fc = NULL; |
| 667 | |
| 668 | BT_ASSERT_DBG(fc); |
| 669 | BT_ASSERT_DBG(name); |
| 670 | |
| 671 | for (i = 0; i < fc->members->len; i++) { |
| 672 | struct fs_sink_ctf_named_field_class *named_fc = |
| 673 | fs_sink_ctf_field_class_struct_borrow_member_by_index(fc, i); |
| 674 | |
| 675 | if (strcmp(name, named_fc->name->str) == 0) { |
| 676 | ret_named_fc = named_fc; |
| 677 | goto end; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | end: |
| 682 | return ret_named_fc; |
| 683 | } |
| 684 | |
| 685 | static inline struct fs_sink_ctf_field_class * |
| 686 | fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name( |
| 687 | struct fs_sink_ctf_field_class_struct *struct_fc, const char *name) |
| 688 | { |
| 689 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
| 690 | struct fs_sink_ctf_field_class *fc = NULL; |
| 691 | |
| 692 | if (!struct_fc) { |
| 693 | goto end; |
| 694 | } |
| 695 | |
| 696 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_name(struct_fc, name); |
| 697 | if (!named_fc) { |
| 698 | goto end; |
| 699 | } |
| 700 | |
| 701 | fc = named_fc->fc; |
| 702 | |
| 703 | end: |
| 704 | return fc; |
| 705 | } |
| 706 | |
| 707 | static inline struct fs_sink_ctf_field_class_int * |
| 708 | fs_sink_ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 709 | struct fs_sink_ctf_field_class_struct *struct_fc, const char *name) |
| 710 | { |
| 711 | struct fs_sink_ctf_field_class_int *int_fc = NULL; |
| 712 | |
| 713 | int_fc = fs_sink_ctf_field_class_as_int( |
| 714 | fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(struct_fc, name)); |
| 715 | if (!int_fc) { |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | if (int_fc->base.base.type != FS_SINK_CTF_FIELD_CLASS_TYPE_INT) { |
| 720 | int_fc = NULL; |
| 721 | goto end; |
| 722 | } |
| 723 | |
| 724 | end: |
| 725 | return int_fc; |
| 726 | } |
| 727 | |
| 728 | static inline void |
| 729 | fs_sink_ctf_field_class_struct_align_at_least(struct fs_sink_ctf_field_class_struct *fc, |
| 730 | unsigned int alignment) |
| 731 | { |
| 732 | if (alignment > fc->base.alignment) { |
| 733 | fc->base.alignment = alignment; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | static inline void |
| 738 | fs_sink_ctf_field_class_struct_append_member(struct fs_sink_ctf_field_class_struct *fc, |
| 739 | const char *name, |
| 740 | struct fs_sink_ctf_field_class *member_fc) |
| 741 | { |
| 742 | struct fs_sink_ctf_named_field_class *named_fc; |
| 743 | |
| 744 | BT_ASSERT(fc); |
| 745 | BT_ASSERT(name); |
| 746 | g_array_set_size(fc->members, fc->members->len + 1); |
| 747 | |
| 748 | named_fc = |
| 749 | &bt_g_array_index(fc->members, struct fs_sink_ctf_named_field_class, fc->members->len - 1); |
| 750 | _fs_sink_ctf_named_field_class_init(named_fc); |
| 751 | g_string_assign(named_fc->name, name); |
| 752 | named_fc->fc = member_fc; |
| 753 | fs_sink_ctf_field_class_struct_align_at_least(fc, member_fc->alignment); |
| 754 | } |
| 755 | |
| 756 | static inline struct fs_sink_ctf_named_field_class * |
| 757 | fs_sink_ctf_field_class_variant_borrow_option_by_index(struct fs_sink_ctf_field_class_variant *fc, |
| 758 | uint64_t index) |
| 759 | { |
| 760 | BT_ASSERT_DBG(fc); |
| 761 | BT_ASSERT_DBG(index < fc->options->len); |
| 762 | return &bt_g_array_index(fc->options, struct fs_sink_ctf_named_field_class, index); |
| 763 | } |
| 764 | |
| 765 | static inline struct fs_sink_ctf_named_field_class * |
| 766 | fs_sink_ctf_field_class_variant_borrow_option_by_name(struct fs_sink_ctf_field_class_variant *fc, |
| 767 | const char *name) |
| 768 | { |
| 769 | uint64_t i; |
| 770 | struct fs_sink_ctf_named_field_class *ret_named_fc = NULL; |
| 771 | |
| 772 | BT_ASSERT_DBG(fc); |
| 773 | BT_ASSERT_DBG(name); |
| 774 | |
| 775 | for (i = 0; i < fc->options->len; i++) { |
| 776 | struct fs_sink_ctf_named_field_class *named_fc = |
| 777 | fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, i); |
| 778 | |
| 779 | if (strcmp(name, named_fc->name->str) == 0) { |
| 780 | ret_named_fc = named_fc; |
| 781 | goto end; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | end: |
| 786 | return ret_named_fc; |
| 787 | } |
| 788 | |
| 789 | static inline void |
| 790 | fs_sink_ctf_field_class_variant_append_option(struct fs_sink_ctf_field_class_variant *fc, |
| 791 | const char *name, |
| 792 | struct fs_sink_ctf_field_class *option_fc) |
| 793 | { |
| 794 | struct fs_sink_ctf_named_field_class *named_fc; |
| 795 | |
| 796 | BT_ASSERT(fc); |
| 797 | BT_ASSERT(name); |
| 798 | g_array_set_size(fc->options, fc->options->len + 1); |
| 799 | |
| 800 | named_fc = |
| 801 | &bt_g_array_index(fc->options, struct fs_sink_ctf_named_field_class, fc->options->len - 1); |
| 802 | _fs_sink_ctf_named_field_class_init(named_fc); |
| 803 | g_string_assign(named_fc->name, name); |
| 804 | named_fc->fc = option_fc; |
| 805 | } |
| 806 | |
| 807 | static inline struct fs_sink_ctf_event_class * |
| 808 | fs_sink_ctf_event_class_create(struct fs_sink_ctf_stream_class *sc, const bt_event_class *ir_ec) |
| 809 | { |
| 810 | struct fs_sink_ctf_event_class *ec = g_new0(struct fs_sink_ctf_event_class, 1); |
| 811 | |
| 812 | BT_ASSERT(sc); |
| 813 | BT_ASSERT(ir_ec); |
| 814 | BT_ASSERT(ec); |
| 815 | ec->ir_ec = ir_ec; |
| 816 | ec->sc = sc; |
| 817 | g_ptr_array_add(sc->event_classes, ec); |
| 818 | g_hash_table_insert(sc->event_classes_from_ir, (gpointer) ir_ec, ec); |
| 819 | return ec; |
| 820 | } |
| 821 | |
| 822 | static inline void fs_sink_ctf_event_class_destroy(struct fs_sink_ctf_event_class *ec) |
| 823 | { |
| 824 | if (!ec) { |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | fs_sink_ctf_field_class_destroy(ec->spec_context_fc); |
| 829 | ec->spec_context_fc = NULL; |
| 830 | fs_sink_ctf_field_class_destroy(ec->payload_fc); |
| 831 | ec->payload_fc = NULL; |
| 832 | g_free(ec); |
| 833 | } |
| 834 | |
| 835 | static inline struct fs_sink_ctf_stream_class * |
| 836 | fs_sink_ctf_stream_class_create(struct fs_sink_ctf_trace *trace, const bt_stream_class *ir_sc) |
| 837 | { |
| 838 | struct fs_sink_ctf_stream_class *sc = g_new0(struct fs_sink_ctf_stream_class, 1); |
| 839 | |
| 840 | BT_ASSERT(trace); |
| 841 | BT_ASSERT(ir_sc); |
| 842 | BT_ASSERT(sc); |
| 843 | sc->trace = trace; |
| 844 | sc->ir_sc = ir_sc; |
| 845 | sc->default_clock_class = bt_stream_class_borrow_default_clock_class_const(ir_sc); |
| 846 | sc->default_clock_class_name = g_string_new(NULL); |
| 847 | BT_ASSERT(sc->default_clock_class_name); |
| 848 | sc->event_classes = |
| 849 | g_ptr_array_new_with_free_func((GDestroyNotify) fs_sink_ctf_event_class_destroy); |
| 850 | BT_ASSERT(sc->event_classes); |
| 851 | sc->event_classes_from_ir = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 852 | BT_ASSERT(sc->event_classes_from_ir); |
| 853 | sc->has_packets = bt_stream_class_supports_packets(ir_sc); |
| 854 | sc->packets_have_ts_begin = |
| 855 | bt_stream_class_packets_have_beginning_default_clock_snapshot(ir_sc); |
| 856 | sc->packets_have_ts_end = bt_stream_class_packets_have_end_default_clock_snapshot(ir_sc); |
| 857 | sc->has_discarded_events = bt_stream_class_supports_discarded_events(ir_sc); |
| 858 | |
| 859 | if (sc->has_discarded_events) { |
| 860 | sc->discarded_events_has_ts = |
| 861 | bt_stream_class_discarded_events_have_default_clock_snapshots(ir_sc); |
| 862 | } |
| 863 | |
| 864 | if (bt_stream_class_supports_discarded_packets(ir_sc)) { |
| 865 | sc->discarded_packets_has_ts = |
| 866 | bt_stream_class_discarded_packets_have_default_clock_snapshots(ir_sc); |
| 867 | } |
| 868 | |
| 869 | g_ptr_array_add(trace->stream_classes, sc); |
| 870 | return sc; |
| 871 | } |
| 872 | |
| 873 | static inline void fs_sink_ctf_stream_class_destroy(struct fs_sink_ctf_stream_class *sc) |
| 874 | { |
| 875 | if (!sc) { |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | if (sc->default_clock_class_name) { |
| 880 | g_string_free(sc->default_clock_class_name, TRUE); |
| 881 | sc->default_clock_class_name = NULL; |
| 882 | } |
| 883 | |
| 884 | if (sc->event_classes) { |
| 885 | g_ptr_array_free(sc->event_classes, TRUE); |
| 886 | sc->event_classes = NULL; |
| 887 | } |
| 888 | |
| 889 | if (sc->event_classes_from_ir) { |
| 890 | g_hash_table_destroy(sc->event_classes_from_ir); |
| 891 | sc->event_classes_from_ir = NULL; |
| 892 | } |
| 893 | |
| 894 | fs_sink_ctf_field_class_destroy(sc->packet_context_fc); |
| 895 | sc->packet_context_fc = NULL; |
| 896 | fs_sink_ctf_field_class_destroy(sc->event_common_context_fc); |
| 897 | sc->event_common_context_fc = NULL; |
| 898 | g_free(sc); |
| 899 | } |
| 900 | |
| 901 | static inline void fs_sink_ctf_stream_class_append_event_class(struct fs_sink_ctf_stream_class *sc, |
| 902 | struct fs_sink_ctf_event_class *ec) |
| 903 | { |
| 904 | g_ptr_array_add(sc->event_classes, ec); |
| 905 | } |
| 906 | |
| 907 | static inline void fs_sink_ctf_trace_destroy(struct fs_sink_ctf_trace *trace) |
| 908 | { |
| 909 | if (!trace) { |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | if (trace->stream_classes) { |
| 914 | g_ptr_array_free(trace->stream_classes, TRUE); |
| 915 | trace->stream_classes = NULL; |
| 916 | } |
| 917 | |
| 918 | g_free(trace); |
| 919 | } |
| 920 | |
| 921 | static inline struct fs_sink_ctf_trace *fs_sink_ctf_trace_create(const bt_trace *ir_trace) |
| 922 | { |
| 923 | struct fs_sink_ctf_trace *trace = g_new0(struct fs_sink_ctf_trace, 1); |
| 924 | |
| 925 | BT_ASSERT(trace); |
| 926 | |
| 927 | bt_uuid_generate(trace->uuid); |
| 928 | |
| 929 | trace->ir_trace = ir_trace; |
| 930 | trace->ir_tc = bt_trace_borrow_class_const(ir_trace); |
| 931 | trace->stream_classes = |
| 932 | g_ptr_array_new_with_free_func((GDestroyNotify) fs_sink_ctf_stream_class_destroy); |
| 933 | BT_ASSERT(trace->stream_classes); |
| 934 | |
| 935 | return trace; |
| 936 | } |
| 937 | |
| 938 | #endif /* BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H */ |