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