| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _CTF_META_H |
| 8 | #define _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 ctf_field_class_type |
| 21 | { |
| 22 | CTF_FIELD_CLASS_TYPE_INT, |
| 23 | CTF_FIELD_CLASS_TYPE_ENUM, |
| 24 | CTF_FIELD_CLASS_TYPE_FLOAT, |
| 25 | CTF_FIELD_CLASS_TYPE_STRING, |
| 26 | CTF_FIELD_CLASS_TYPE_STRUCT, |
| 27 | CTF_FIELD_CLASS_TYPE_ARRAY, |
| 28 | CTF_FIELD_CLASS_TYPE_SEQUENCE, |
| 29 | CTF_FIELD_CLASS_TYPE_VARIANT, |
| 30 | }; |
| 31 | |
| 32 | enum ctf_field_class_meaning |
| 33 | { |
| 34 | CTF_FIELD_CLASS_MEANING_NONE, |
| 35 | CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME, |
| 36 | CTF_FIELD_CLASS_MEANING_PACKET_END_TIME, |
| 37 | CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID, |
| 38 | CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID, |
| 39 | CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID, |
| 40 | CTF_FIELD_CLASS_MEANING_MAGIC, |
| 41 | CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT, |
| 42 | CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT, |
| 43 | CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE, |
| 44 | CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE, |
| 45 | CTF_FIELD_CLASS_MEANING_UUID, |
| 46 | }; |
| 47 | |
| 48 | enum ctf_byte_order |
| 49 | { |
| 50 | CTF_BYTE_ORDER_UNKNOWN, |
| 51 | CTF_BYTE_ORDER_DEFAULT, |
| 52 | CTF_BYTE_ORDER_LITTLE, |
| 53 | CTF_BYTE_ORDER_BIG, |
| 54 | }; |
| 55 | |
| 56 | enum ctf_encoding |
| 57 | { |
| 58 | CTF_ENCODING_NONE, |
| 59 | CTF_ENCODING_UTF8, |
| 60 | }; |
| 61 | |
| 62 | enum ctf_scope |
| 63 | { |
| 64 | CTF_SCOPE_PACKET_UNKNOWN = -1, |
| 65 | CTF_SCOPE_PACKET_HEADER = 0, |
| 66 | CTF_SCOPE_PACKET_CONTEXT, |
| 67 | CTF_SCOPE_EVENT_HEADER, |
| 68 | CTF_SCOPE_EVENT_COMMON_CONTEXT, |
| 69 | CTF_SCOPE_EVENT_SPECIFIC_CONTEXT, |
| 70 | CTF_SCOPE_EVENT_PAYLOAD, |
| 71 | }; |
| 72 | |
| 73 | struct ctf_clock_class |
| 74 | { |
| 75 | GString *name; |
| 76 | GString *description; |
| 77 | uint64_t frequency; |
| 78 | uint64_t precision; |
| 79 | int64_t offset_seconds; |
| 80 | uint64_t offset_cycles; |
| 81 | bt_uuid_t uuid; |
| 82 | bool has_uuid; |
| 83 | bool is_absolute; |
| 84 | |
| 85 | /* Weak, set during translation */ |
| 86 | bt_clock_class *ir_cc; |
| 87 | }; |
| 88 | |
| 89 | struct ctf_field_class |
| 90 | { |
| 91 | enum ctf_field_class_type type; |
| 92 | unsigned int alignment; |
| 93 | bool is_compound; |
| 94 | bool in_ir; |
| 95 | |
| 96 | /* Weak, set during translation. NULL if `in_ir` is false below. */ |
| 97 | bt_field_class *ir_fc; |
| 98 | }; |
| 99 | |
| 100 | struct ctf_field_class_bit_array |
| 101 | { |
| 102 | struct ctf_field_class base; |
| 103 | enum ctf_byte_order byte_order; |
| 104 | unsigned int size; |
| 105 | }; |
| 106 | |
| 107 | struct ctf_field_class_int |
| 108 | { |
| 109 | struct ctf_field_class_bit_array base; |
| 110 | enum ctf_field_class_meaning meaning; |
| 111 | bool is_signed; |
| 112 | bt_field_class_integer_preferred_display_base disp_base; |
| 113 | enum ctf_encoding encoding; |
| 114 | int64_t storing_index; |
| 115 | |
| 116 | /* Weak */ |
| 117 | struct ctf_clock_class *mapped_clock_class; |
| 118 | }; |
| 119 | |
| 120 | struct ctf_range |
| 121 | { |
| 122 | union |
| 123 | { |
| 124 | uint64_t u; |
| 125 | int64_t i; |
| 126 | } lower; |
| 127 | |
| 128 | union |
| 129 | { |
| 130 | uint64_t u; |
| 131 | int64_t i; |
| 132 | } upper; |
| 133 | }; |
| 134 | |
| 135 | struct ctf_field_class_enum_mapping |
| 136 | { |
| 137 | GString *label; |
| 138 | |
| 139 | /* Array of `struct ctf_range` */ |
| 140 | GArray *ranges; |
| 141 | }; |
| 142 | |
| 143 | struct ctf_field_class_enum |
| 144 | { |
| 145 | struct ctf_field_class_int base; |
| 146 | |
| 147 | /* Array of `struct ctf_field_class_enum_mapping` */ |
| 148 | GArray *mappings; |
| 149 | }; |
| 150 | |
| 151 | struct ctf_field_class_float |
| 152 | { |
| 153 | struct ctf_field_class_bit_array base; |
| 154 | }; |
| 155 | |
| 156 | struct ctf_field_class_string |
| 157 | { |
| 158 | struct ctf_field_class base; |
| 159 | enum ctf_encoding encoding; |
| 160 | }; |
| 161 | |
| 162 | struct ctf_named_field_class |
| 163 | { |
| 164 | /* Original name which can include a leading `_` */ |
| 165 | GString *orig_name; |
| 166 | |
| 167 | /* Name as translated to trace IR (leading `_` removed) */ |
| 168 | GString *name; |
| 169 | |
| 170 | /* Owned by this */ |
| 171 | struct ctf_field_class *fc; |
| 172 | }; |
| 173 | |
| 174 | struct ctf_field_class_struct |
| 175 | { |
| 176 | struct ctf_field_class base; |
| 177 | |
| 178 | /* Array of `struct ctf_named_field_class` */ |
| 179 | GArray *members; |
| 180 | }; |
| 181 | |
| 182 | struct ctf_field_path |
| 183 | { |
| 184 | enum ctf_scope root; |
| 185 | |
| 186 | /* Array of `int64_t` */ |
| 187 | GArray *path; |
| 188 | }; |
| 189 | |
| 190 | struct ctf_field_class_variant_range |
| 191 | { |
| 192 | struct ctf_range range; |
| 193 | uint64_t option_index; |
| 194 | }; |
| 195 | |
| 196 | struct ctf_field_class_variant |
| 197 | { |
| 198 | struct ctf_field_class base; |
| 199 | GString *tag_ref; |
| 200 | struct ctf_field_path tag_path; |
| 201 | uint64_t stored_tag_index; |
| 202 | |
| 203 | /* Array of `struct ctf_named_field_class` */ |
| 204 | GArray *options; |
| 205 | |
| 206 | /* Array of `struct ctf_field_class_variant_range` */ |
| 207 | GArray *ranges; |
| 208 | |
| 209 | /* Weak */ |
| 210 | struct ctf_field_class_enum *tag_fc; |
| 211 | }; |
| 212 | |
| 213 | struct ctf_field_class_array_base |
| 214 | { |
| 215 | struct ctf_field_class base; |
| 216 | struct ctf_field_class *elem_fc; |
| 217 | bool is_text; |
| 218 | }; |
| 219 | |
| 220 | struct ctf_field_class_array |
| 221 | { |
| 222 | struct ctf_field_class_array_base base; |
| 223 | enum ctf_field_class_meaning meaning; |
| 224 | uint64_t length; |
| 225 | }; |
| 226 | |
| 227 | struct ctf_field_class_sequence |
| 228 | { |
| 229 | struct ctf_field_class_array_base base; |
| 230 | GString *length_ref; |
| 231 | struct ctf_field_path length_path; |
| 232 | uint64_t stored_length_index; |
| 233 | |
| 234 | /* Weak */ |
| 235 | struct ctf_field_class_int *length_fc; |
| 236 | }; |
| 237 | |
| 238 | struct ctf_event_class |
| 239 | { |
| 240 | GString *name; |
| 241 | uint64_t id; |
| 242 | GString *emf_uri; |
| 243 | bt_event_class_log_level log_level; |
| 244 | bool is_translated; |
| 245 | bool is_log_level_set; |
| 246 | |
| 247 | /* Owned by this */ |
| 248 | struct ctf_field_class *spec_context_fc; |
| 249 | |
| 250 | /* Owned by this */ |
| 251 | struct ctf_field_class *payload_fc; |
| 252 | |
| 253 | /* Weak, set during translation */ |
| 254 | bt_event_class *ir_ec; |
| 255 | }; |
| 256 | |
| 257 | struct ctf_stream_class |
| 258 | { |
| 259 | uint64_t id; |
| 260 | bool is_translated; |
| 261 | bool packets_have_ts_begin; |
| 262 | bool packets_have_ts_end; |
| 263 | bool has_discarded_events; |
| 264 | bool has_discarded_packets; |
| 265 | bool discarded_events_have_default_cs; |
| 266 | bool discarded_packets_have_default_cs; |
| 267 | |
| 268 | /* Owned by this */ |
| 269 | struct ctf_field_class *packet_context_fc; |
| 270 | |
| 271 | /* Owned by this */ |
| 272 | struct ctf_field_class *event_header_fc; |
| 273 | |
| 274 | /* Owned by this */ |
| 275 | struct ctf_field_class *event_common_context_fc; |
| 276 | |
| 277 | /* Array of `struct ctf_event_class *`, owned by this */ |
| 278 | GPtrArray *event_classes; |
| 279 | |
| 280 | /* |
| 281 | * Hash table mapping event class IDs to `struct ctf_event_class *`, |
| 282 | * weak. |
| 283 | */ |
| 284 | GHashTable *event_classes_by_id; |
| 285 | |
| 286 | /* Weak */ |
| 287 | struct ctf_clock_class *default_clock_class; |
| 288 | |
| 289 | /* Weak, set during translation */ |
| 290 | bt_stream_class *ir_sc; |
| 291 | }; |
| 292 | |
| 293 | enum ctf_trace_class_env_entry_type |
| 294 | { |
| 295 | CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT, |
| 296 | CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR, |
| 297 | }; |
| 298 | |
| 299 | struct ctf_trace_class_env_entry |
| 300 | { |
| 301 | enum ctf_trace_class_env_entry_type type; |
| 302 | GString *name; |
| 303 | |
| 304 | struct |
| 305 | { |
| 306 | int64_t i; |
| 307 | GString *str; |
| 308 | } value; |
| 309 | }; |
| 310 | |
| 311 | struct ctf_trace_class |
| 312 | { |
| 313 | unsigned int major; |
| 314 | unsigned int minor; |
| 315 | bt_uuid_t uuid; |
| 316 | bool is_uuid_set; |
| 317 | enum ctf_byte_order default_byte_order; |
| 318 | |
| 319 | /* Owned by this */ |
| 320 | struct ctf_field_class *packet_header_fc; |
| 321 | |
| 322 | uint64_t stored_value_count; |
| 323 | |
| 324 | /* Array of `struct ctf_clock_class *` (owned by this) */ |
| 325 | GPtrArray *clock_classes; |
| 326 | |
| 327 | /* Array of `struct ctf_stream_class *` */ |
| 328 | GPtrArray *stream_classes; |
| 329 | |
| 330 | /* Array of `struct ctf_trace_class_env_entry` */ |
| 331 | GArray *env_entries; |
| 332 | |
| 333 | bool is_translated; |
| 334 | |
| 335 | /* Weak, set during translation */ |
| 336 | bt_trace_class *ir_tc; |
| 337 | |
| 338 | struct |
| 339 | { |
| 340 | bool lttng_crash; |
| 341 | bool lttng_event_after_packet; |
| 342 | bool barectf_event_before_packet; |
| 343 | } quirks; |
| 344 | }; |
| 345 | |
| 346 | static inline ctf_field_class_bit_array *ctf_field_class_as_bit_array(ctf_field_class *fc) |
| 347 | { |
| 348 | BT_ASSERT_DBG(!fc || |
| 349 | (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM || |
| 350 | fc->type == CTF_FIELD_CLASS_TYPE_FLOAT)); |
| 351 | return (ctf_field_class_bit_array *) fc; |
| 352 | } |
| 353 | |
| 354 | static inline ctf_field_class_int *ctf_field_class_as_int(ctf_field_class *fc) |
| 355 | { |
| 356 | BT_ASSERT_DBG(!fc || |
| 357 | (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM)); |
| 358 | return (ctf_field_class_int *) fc; |
| 359 | } |
| 360 | |
| 361 | static inline ctf_field_class_enum *ctf_field_class_as_enum(ctf_field_class *fc) |
| 362 | { |
| 363 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ENUM); |
| 364 | return (ctf_field_class_enum *) fc; |
| 365 | } |
| 366 | |
| 367 | static inline ctf_field_class_float *ctf_field_class_as_float(ctf_field_class *fc) |
| 368 | { |
| 369 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_FLOAT); |
| 370 | return (ctf_field_class_float *) fc; |
| 371 | } |
| 372 | |
| 373 | static inline ctf_field_class_string *ctf_field_class_as_string(ctf_field_class *fc) |
| 374 | { |
| 375 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRING); |
| 376 | return (ctf_field_class_string *) fc; |
| 377 | } |
| 378 | |
| 379 | static inline ctf_field_class_struct *ctf_field_class_as_struct(ctf_field_class *fc) |
| 380 | { |
| 381 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRUCT); |
| 382 | return (ctf_field_class_struct *) fc; |
| 383 | } |
| 384 | |
| 385 | static inline ctf_field_class_array_base *ctf_field_class_as_array_base(ctf_field_class *fc) |
| 386 | { |
| 387 | BT_ASSERT_DBG(!fc || (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || |
| 388 | fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE)); |
| 389 | return (ctf_field_class_array_base *) fc; |
| 390 | } |
| 391 | |
| 392 | static inline ctf_field_class_array *ctf_field_class_as_array(ctf_field_class *fc) |
| 393 | { |
| 394 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ARRAY); |
| 395 | return (ctf_field_class_array *) fc; |
| 396 | } |
| 397 | |
| 398 | static inline ctf_field_class_sequence *ctf_field_class_as_sequence(ctf_field_class *fc) |
| 399 | { |
| 400 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE); |
| 401 | return (ctf_field_class_sequence *) fc; |
| 402 | } |
| 403 | |
| 404 | static inline ctf_field_class_variant *ctf_field_class_as_variant(ctf_field_class *fc) |
| 405 | { |
| 406 | BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_VARIANT); |
| 407 | return (ctf_field_class_variant *) fc; |
| 408 | } |
| 409 | |
| 410 | static inline void ctf_field_class_destroy(struct ctf_field_class *fc); |
| 411 | |
| 412 | static inline void _ctf_field_class_init(struct ctf_field_class *fc, enum ctf_field_class_type type, |
| 413 | unsigned int alignment) |
| 414 | { |
| 415 | BT_ASSERT(fc); |
| 416 | fc->type = type; |
| 417 | fc->alignment = alignment; |
| 418 | fc->in_ir = false; |
| 419 | } |
| 420 | |
| 421 | static inline void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc, |
| 422 | enum ctf_field_class_type type) |
| 423 | { |
| 424 | _ctf_field_class_init(&fc->base, type, 1); |
| 425 | } |
| 426 | |
| 427 | static inline void _ctf_field_class_int_init(struct ctf_field_class_int *fc, |
| 428 | enum ctf_field_class_type type) |
| 429 | { |
| 430 | _ctf_field_class_bit_array_init(&fc->base, type); |
| 431 | fc->meaning = CTF_FIELD_CLASS_MEANING_NONE; |
| 432 | fc->storing_index = -1; |
| 433 | } |
| 434 | |
| 435 | static inline void ctf_field_path_init(struct ctf_field_path *field_path) |
| 436 | { |
| 437 | BT_ASSERT(field_path); |
| 438 | field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t)); |
| 439 | BT_ASSERT(field_path->path); |
| 440 | } |
| 441 | |
| 442 | static inline void ctf_field_path_fini(struct ctf_field_path *field_path) |
| 443 | { |
| 444 | BT_ASSERT(field_path); |
| 445 | |
| 446 | if (field_path->path) { |
| 447 | g_array_free(field_path->path, TRUE); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static inline void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc) |
| 452 | { |
| 453 | BT_ASSERT(named_fc); |
| 454 | named_fc->name = g_string_new(NULL); |
| 455 | BT_ASSERT(named_fc->name); |
| 456 | named_fc->orig_name = g_string_new(NULL); |
| 457 | BT_ASSERT(named_fc->orig_name); |
| 458 | } |
| 459 | |
| 460 | static inline void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc) |
| 461 | { |
| 462 | BT_ASSERT(named_fc); |
| 463 | |
| 464 | if (named_fc->name) { |
| 465 | g_string_free(named_fc->name, TRUE); |
| 466 | } |
| 467 | |
| 468 | if (named_fc->orig_name) { |
| 469 | g_string_free(named_fc->orig_name, TRUE); |
| 470 | } |
| 471 | |
| 472 | ctf_field_class_destroy(named_fc->fc); |
| 473 | } |
| 474 | |
| 475 | static inline void _ctf_field_class_enum_mapping_init(struct ctf_field_class_enum_mapping *mapping) |
| 476 | { |
| 477 | BT_ASSERT(mapping); |
| 478 | mapping->label = g_string_new(NULL); |
| 479 | BT_ASSERT(mapping->label); |
| 480 | mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range)); |
| 481 | BT_ASSERT(mapping->ranges); |
| 482 | } |
| 483 | |
| 484 | static inline void _ctf_field_class_enum_mapping_fini(struct ctf_field_class_enum_mapping *mapping) |
| 485 | { |
| 486 | BT_ASSERT(mapping); |
| 487 | |
| 488 | if (mapping->label) { |
| 489 | g_string_free(mapping->label, TRUE); |
| 490 | } |
| 491 | |
| 492 | if (mapping->ranges) { |
| 493 | g_array_free(mapping->ranges, TRUE); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | static inline struct ctf_field_class_int *ctf_field_class_int_create(void) |
| 498 | { |
| 499 | struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1); |
| 500 | |
| 501 | BT_ASSERT(fc); |
| 502 | _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT); |
| 503 | return fc; |
| 504 | } |
| 505 | |
| 506 | static inline struct ctf_field_class_float *ctf_field_class_float_create(void) |
| 507 | { |
| 508 | struct ctf_field_class_float *fc = g_new0(struct ctf_field_class_float, 1); |
| 509 | |
| 510 | BT_ASSERT(fc); |
| 511 | _ctf_field_class_bit_array_init(&fc->base, CTF_FIELD_CLASS_TYPE_FLOAT); |
| 512 | return fc; |
| 513 | } |
| 514 | |
| 515 | static inline struct ctf_field_class_string *ctf_field_class_string_create(void) |
| 516 | { |
| 517 | struct ctf_field_class_string *fc = g_new0(struct ctf_field_class_string, 1); |
| 518 | |
| 519 | BT_ASSERT(fc); |
| 520 | _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRING, 8); |
| 521 | return fc; |
| 522 | } |
| 523 | |
| 524 | static inline struct ctf_field_class_enum *ctf_field_class_enum_create(void) |
| 525 | { |
| 526 | struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1); |
| 527 | |
| 528 | BT_ASSERT(fc); |
| 529 | _ctf_field_class_int_init(&fc->base, CTF_FIELD_CLASS_TYPE_ENUM); |
| 530 | fc->mappings = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_enum_mapping)); |
| 531 | BT_ASSERT(fc->mappings); |
| 532 | return fc; |
| 533 | } |
| 534 | |
| 535 | static inline struct ctf_field_class_struct *ctf_field_class_struct_create(void) |
| 536 | { |
| 537 | struct ctf_field_class_struct *fc = g_new0(struct ctf_field_class_struct, 1); |
| 538 | |
| 539 | BT_ASSERT(fc); |
| 540 | _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRUCT, 1); |
| 541 | fc->members = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class)); |
| 542 | BT_ASSERT(fc->members); |
| 543 | fc->base.is_compound = true; |
| 544 | return fc; |
| 545 | } |
| 546 | |
| 547 | static inline struct ctf_field_class_variant *ctf_field_class_variant_create(void) |
| 548 | { |
| 549 | struct ctf_field_class_variant *fc = g_new0(struct ctf_field_class_variant, 1); |
| 550 | |
| 551 | BT_ASSERT(fc); |
| 552 | _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_VARIANT, 1); |
| 553 | fc->options = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class)); |
| 554 | BT_ASSERT(fc->options); |
| 555 | fc->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_variant_range)); |
| 556 | BT_ASSERT(fc->ranges); |
| 557 | fc->tag_ref = g_string_new(NULL); |
| 558 | BT_ASSERT(fc->tag_ref); |
| 559 | ctf_field_path_init(&fc->tag_path); |
| 560 | fc->base.is_compound = true; |
| 561 | return fc; |
| 562 | } |
| 563 | |
| 564 | static inline struct ctf_field_class_array *ctf_field_class_array_create(void) |
| 565 | { |
| 566 | struct ctf_field_class_array *fc = g_new0(struct ctf_field_class_array, 1); |
| 567 | |
| 568 | BT_ASSERT(fc); |
| 569 | _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_ARRAY, 1); |
| 570 | fc->base.base.is_compound = true; |
| 571 | return fc; |
| 572 | } |
| 573 | |
| 574 | static inline struct ctf_field_class_sequence *ctf_field_class_sequence_create(void) |
| 575 | { |
| 576 | struct ctf_field_class_sequence *fc = g_new0(struct ctf_field_class_sequence, 1); |
| 577 | |
| 578 | BT_ASSERT(fc); |
| 579 | _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1); |
| 580 | fc->length_ref = g_string_new(NULL); |
| 581 | BT_ASSERT(fc->length_ref); |
| 582 | ctf_field_path_init(&fc->length_path); |
| 583 | fc->base.base.is_compound = true; |
| 584 | return fc; |
| 585 | } |
| 586 | |
| 587 | static inline void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc) |
| 588 | { |
| 589 | BT_ASSERT(fc); |
| 590 | g_free(fc); |
| 591 | } |
| 592 | |
| 593 | static inline void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc) |
| 594 | { |
| 595 | BT_ASSERT(fc); |
| 596 | |
| 597 | if (fc->mappings) { |
| 598 | uint64_t i; |
| 599 | |
| 600 | for (i = 0; i < fc->mappings->len; i++) { |
| 601 | struct ctf_field_class_enum_mapping *mapping = |
| 602 | &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i); |
| 603 | |
| 604 | _ctf_field_class_enum_mapping_fini(mapping); |
| 605 | } |
| 606 | |
| 607 | g_array_free(fc->mappings, TRUE); |
| 608 | } |
| 609 | |
| 610 | g_free(fc); |
| 611 | } |
| 612 | |
| 613 | static inline void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc) |
| 614 | { |
| 615 | BT_ASSERT(fc); |
| 616 | g_free(fc); |
| 617 | } |
| 618 | |
| 619 | static inline void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc) |
| 620 | { |
| 621 | BT_ASSERT(fc); |
| 622 | g_free(fc); |
| 623 | } |
| 624 | |
| 625 | static inline void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc) |
| 626 | { |
| 627 | BT_ASSERT(fc); |
| 628 | |
| 629 | if (fc->members) { |
| 630 | uint64_t i; |
| 631 | |
| 632 | for (i = 0; i < fc->members->len; i++) { |
| 633 | struct ctf_named_field_class *named_fc = |
| 634 | &bt_g_array_index(fc->members, struct ctf_named_field_class, i); |
| 635 | |
| 636 | _ctf_named_field_class_fini(named_fc); |
| 637 | } |
| 638 | |
| 639 | g_array_free(fc->members, TRUE); |
| 640 | } |
| 641 | |
| 642 | g_free(fc); |
| 643 | } |
| 644 | |
| 645 | static inline void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc) |
| 646 | { |
| 647 | BT_ASSERT(fc); |
| 648 | ctf_field_class_destroy(fc->elem_fc); |
| 649 | } |
| 650 | |
| 651 | static inline void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc) |
| 652 | { |
| 653 | BT_ASSERT(fc); |
| 654 | _ctf_field_class_array_base_fini(&fc->base); |
| 655 | g_free(fc); |
| 656 | } |
| 657 | |
| 658 | static inline void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc) |
| 659 | { |
| 660 | BT_ASSERT(fc); |
| 661 | _ctf_field_class_array_base_fini(&fc->base); |
| 662 | |
| 663 | if (fc->length_ref) { |
| 664 | g_string_free(fc->length_ref, TRUE); |
| 665 | } |
| 666 | |
| 667 | ctf_field_path_fini(&fc->length_path); |
| 668 | g_free(fc); |
| 669 | } |
| 670 | |
| 671 | static inline void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc) |
| 672 | { |
| 673 | BT_ASSERT(fc); |
| 674 | |
| 675 | if (fc->options) { |
| 676 | uint64_t i; |
| 677 | |
| 678 | for (i = 0; i < fc->options->len; i++) { |
| 679 | struct ctf_named_field_class *named_fc = |
| 680 | &bt_g_array_index(fc->options, struct ctf_named_field_class, i); |
| 681 | |
| 682 | _ctf_named_field_class_fini(named_fc); |
| 683 | } |
| 684 | |
| 685 | g_array_free(fc->options, TRUE); |
| 686 | } |
| 687 | |
| 688 | if (fc->ranges) { |
| 689 | g_array_free(fc->ranges, TRUE); |
| 690 | } |
| 691 | |
| 692 | if (fc->tag_ref) { |
| 693 | g_string_free(fc->tag_ref, TRUE); |
| 694 | } |
| 695 | |
| 696 | ctf_field_path_fini(&fc->tag_path); |
| 697 | g_free(fc); |
| 698 | } |
| 699 | |
| 700 | static inline void ctf_field_class_destroy(struct ctf_field_class *fc) |
| 701 | { |
| 702 | if (!fc) { |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | switch (fc->type) { |
| 707 | case CTF_FIELD_CLASS_TYPE_INT: |
| 708 | _ctf_field_class_int_destroy(ctf_field_class_as_int(fc)); |
| 709 | break; |
| 710 | case CTF_FIELD_CLASS_TYPE_ENUM: |
| 711 | _ctf_field_class_enum_destroy(ctf_field_class_as_enum(fc)); |
| 712 | break; |
| 713 | case CTF_FIELD_CLASS_TYPE_FLOAT: |
| 714 | _ctf_field_class_float_destroy(ctf_field_class_as_float(fc)); |
| 715 | break; |
| 716 | case CTF_FIELD_CLASS_TYPE_STRING: |
| 717 | _ctf_field_class_string_destroy(ctf_field_class_as_string(fc)); |
| 718 | break; |
| 719 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 720 | _ctf_field_class_struct_destroy(ctf_field_class_as_struct(fc)); |
| 721 | break; |
| 722 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 723 | _ctf_field_class_array_destroy(ctf_field_class_as_array(fc)); |
| 724 | break; |
| 725 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 726 | _ctf_field_class_sequence_destroy(ctf_field_class_as_sequence(fc)); |
| 727 | break; |
| 728 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 729 | _ctf_field_class_variant_destroy(ctf_field_class_as_variant(fc)); |
| 730 | break; |
| 731 | default: |
| 732 | bt_common_abort(); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | static inline struct ctf_range * |
| 737 | ctf_field_class_enum_mapping_borrow_range_by_index(struct ctf_field_class_enum_mapping *mapping, |
| 738 | uint64_t index) |
| 739 | { |
| 740 | BT_ASSERT_DBG(mapping); |
| 741 | BT_ASSERT_DBG(index < mapping->ranges->len); |
| 742 | return &bt_g_array_index(mapping->ranges, struct ctf_range, index); |
| 743 | } |
| 744 | |
| 745 | static inline struct ctf_field_class_enum_mapping * |
| 746 | ctf_field_class_enum_borrow_mapping_by_index(struct ctf_field_class_enum *fc, uint64_t index) |
| 747 | { |
| 748 | BT_ASSERT_DBG(fc); |
| 749 | BT_ASSERT_DBG(index < fc->mappings->len); |
| 750 | return &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, index); |
| 751 | } |
| 752 | |
| 753 | static inline struct ctf_field_class_enum_mapping * |
| 754 | ctf_field_class_enum_borrow_mapping_by_label(struct ctf_field_class_enum *fc, const char *label) |
| 755 | { |
| 756 | struct ctf_field_class_enum_mapping *ret_mapping = NULL; |
| 757 | uint64_t i; |
| 758 | |
| 759 | BT_ASSERT_DBG(fc); |
| 760 | BT_ASSERT_DBG(label); |
| 761 | |
| 762 | for (i = 0; i < fc->mappings->len; i++) { |
| 763 | struct ctf_field_class_enum_mapping *mapping = |
| 764 | ctf_field_class_enum_borrow_mapping_by_index(fc, i); |
| 765 | |
| 766 | if (strcmp(mapping->label->str, label) == 0) { |
| 767 | ret_mapping = mapping; |
| 768 | goto end; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | end: |
| 773 | return ret_mapping; |
| 774 | } |
| 775 | |
| 776 | static inline void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc, |
| 777 | const char *label, uint64_t u_lower, |
| 778 | uint64_t u_upper) |
| 779 | { |
| 780 | struct ctf_field_class_enum_mapping *mapping = NULL; |
| 781 | struct ctf_range range = { |
| 782 | .lower = |
| 783 | { |
| 784 | .u = u_lower, |
| 785 | }, |
| 786 | .upper = |
| 787 | { |
| 788 | .u = u_upper, |
| 789 | }, |
| 790 | }; |
| 791 | uint64_t i; |
| 792 | |
| 793 | BT_ASSERT(fc); |
| 794 | BT_ASSERT(label); |
| 795 | |
| 796 | for (i = 0; i < fc->mappings->len; i++) { |
| 797 | mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, i); |
| 798 | |
| 799 | if (strcmp(mapping->label->str, label) == 0) { |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | if (i == fc->mappings->len) { |
| 805 | mapping = NULL; |
| 806 | } |
| 807 | |
| 808 | if (!mapping) { |
| 809 | g_array_set_size(fc->mappings, fc->mappings->len + 1); |
| 810 | mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, fc->mappings->len - 1); |
| 811 | _ctf_field_class_enum_mapping_init(mapping); |
| 812 | g_string_assign(mapping->label, label); |
| 813 | } |
| 814 | |
| 815 | g_array_append_val(mapping->ranges, range); |
| 816 | } |
| 817 | |
| 818 | static inline struct ctf_named_field_class * |
| 819 | ctf_field_class_struct_borrow_member_by_index(struct ctf_field_class_struct *fc, uint64_t index) |
| 820 | { |
| 821 | BT_ASSERT_DBG(fc); |
| 822 | BT_ASSERT_DBG(index < fc->members->len); |
| 823 | return &bt_g_array_index(fc->members, struct ctf_named_field_class, index); |
| 824 | } |
| 825 | |
| 826 | static inline struct ctf_named_field_class * |
| 827 | ctf_field_class_struct_borrow_member_by_name(struct ctf_field_class_struct *fc, const char *name) |
| 828 | { |
| 829 | uint64_t i; |
| 830 | struct ctf_named_field_class *ret_named_fc = NULL; |
| 831 | |
| 832 | BT_ASSERT_DBG(fc); |
| 833 | BT_ASSERT_DBG(name); |
| 834 | |
| 835 | for (i = 0; i < fc->members->len; i++) { |
| 836 | struct ctf_named_field_class *named_fc = |
| 837 | ctf_field_class_struct_borrow_member_by_index(fc, i); |
| 838 | |
| 839 | if (strcmp(name, named_fc->name->str) == 0) { |
| 840 | ret_named_fc = named_fc; |
| 841 | goto end; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | end: |
| 846 | return ret_named_fc; |
| 847 | } |
| 848 | |
| 849 | static inline struct ctf_field_class * |
| 850 | ctf_field_class_struct_borrow_member_field_class_by_name(struct ctf_field_class_struct *struct_fc, |
| 851 | const char *name) |
| 852 | { |
| 853 | struct ctf_named_field_class *named_fc = NULL; |
| 854 | struct ctf_field_class *fc = NULL; |
| 855 | |
| 856 | if (!struct_fc) { |
| 857 | goto end; |
| 858 | } |
| 859 | |
| 860 | named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name); |
| 861 | if (!named_fc) { |
| 862 | goto end; |
| 863 | } |
| 864 | |
| 865 | fc = named_fc->fc; |
| 866 | |
| 867 | end: |
| 868 | return fc; |
| 869 | } |
| 870 | |
| 871 | static inline struct ctf_field_class_int * |
| 872 | ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 873 | struct ctf_field_class_struct *struct_fc, const char *name) |
| 874 | { |
| 875 | ctf_field_class *member_fc = |
| 876 | ctf_field_class_struct_borrow_member_field_class_by_name(struct_fc, name); |
| 877 | |
| 878 | if (!member_fc) { |
| 879 | return nullptr; |
| 880 | } |
| 881 | |
| 882 | if (member_fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 883 | member_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 884 | return nullptr; |
| 885 | } |
| 886 | |
| 887 | return ctf_field_class_as_int(member_fc); |
| 888 | } |
| 889 | |
| 890 | static inline void _ctf_named_field_class_unescape_orig_name(struct ctf_named_field_class *named_fc) |
| 891 | { |
| 892 | const char *name = named_fc->orig_name->str; |
| 893 | |
| 894 | if (name[0] == '_') { |
| 895 | name++; |
| 896 | } |
| 897 | |
| 898 | g_string_assign(named_fc->name, name); |
| 899 | } |
| 900 | |
| 901 | static inline void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc, |
| 902 | const char *orig_name, |
| 903 | struct ctf_field_class *member_fc) |
| 904 | { |
| 905 | struct ctf_named_field_class *named_fc; |
| 906 | |
| 907 | BT_ASSERT(fc); |
| 908 | BT_ASSERT(orig_name); |
| 909 | g_array_set_size(fc->members, fc->members->len + 1); |
| 910 | |
| 911 | named_fc = &bt_g_array_index(fc->members, struct ctf_named_field_class, fc->members->len - 1); |
| 912 | _ctf_named_field_class_init(named_fc); |
| 913 | g_string_assign(named_fc->orig_name, orig_name); |
| 914 | _ctf_named_field_class_unescape_orig_name(named_fc); |
| 915 | named_fc->fc = member_fc; |
| 916 | |
| 917 | if (member_fc->alignment > fc->base.alignment) { |
| 918 | fc->base.alignment = member_fc->alignment; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | static inline struct ctf_named_field_class * |
| 923 | ctf_field_class_variant_borrow_option_by_index(struct ctf_field_class_variant *fc, uint64_t index) |
| 924 | { |
| 925 | BT_ASSERT_DBG(fc); |
| 926 | BT_ASSERT_DBG(index < fc->options->len); |
| 927 | return &bt_g_array_index(fc->options, struct ctf_named_field_class, index); |
| 928 | } |
| 929 | |
| 930 | static inline struct ctf_named_field_class * |
| 931 | ctf_field_class_variant_borrow_option_by_name(struct ctf_field_class_variant *fc, const char *name) |
| 932 | { |
| 933 | uint64_t i; |
| 934 | struct ctf_named_field_class *ret_named_fc = NULL; |
| 935 | |
| 936 | BT_ASSERT_DBG(fc); |
| 937 | BT_ASSERT_DBG(name); |
| 938 | |
| 939 | for (i = 0; i < fc->options->len; i++) { |
| 940 | struct ctf_named_field_class *named_fc = |
| 941 | ctf_field_class_variant_borrow_option_by_index(fc, i); |
| 942 | |
| 943 | if (strcmp(name, named_fc->name->str) == 0) { |
| 944 | ret_named_fc = named_fc; |
| 945 | goto end; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | end: |
| 950 | return ret_named_fc; |
| 951 | } |
| 952 | |
| 953 | static inline struct ctf_field_class_variant_range * |
| 954 | ctf_field_class_variant_borrow_range_by_index(struct ctf_field_class_variant *fc, uint64_t index) |
| 955 | { |
| 956 | BT_ASSERT_DBG(fc); |
| 957 | BT_ASSERT_DBG(index < fc->ranges->len); |
| 958 | return &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, index); |
| 959 | } |
| 960 | |
| 961 | static inline void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc, |
| 962 | const char *orig_name, |
| 963 | struct ctf_field_class *option_fc) |
| 964 | { |
| 965 | struct ctf_named_field_class *named_fc; |
| 966 | |
| 967 | BT_ASSERT(fc); |
| 968 | BT_ASSERT(orig_name); |
| 969 | g_array_set_size(fc->options, fc->options->len + 1); |
| 970 | |
| 971 | named_fc = &bt_g_array_index(fc->options, struct ctf_named_field_class, fc->options->len - 1); |
| 972 | _ctf_named_field_class_init(named_fc); |
| 973 | g_string_assign(named_fc->orig_name, orig_name); |
| 974 | _ctf_named_field_class_unescape_orig_name(named_fc); |
| 975 | named_fc->fc = option_fc; |
| 976 | } |
| 977 | |
| 978 | static inline void ctf_field_class_variant_set_tag_field_class(struct ctf_field_class_variant *fc, |
| 979 | struct ctf_field_class_enum *tag_fc) |
| 980 | { |
| 981 | uint64_t option_i; |
| 982 | |
| 983 | BT_ASSERT(fc); |
| 984 | BT_ASSERT(tag_fc); |
| 985 | fc->tag_fc = tag_fc; |
| 986 | |
| 987 | for (option_i = 0; option_i < fc->options->len; option_i++) { |
| 988 | uint64_t range_i; |
| 989 | struct ctf_named_field_class *named_fc = |
| 990 | ctf_field_class_variant_borrow_option_by_index(fc, option_i); |
| 991 | struct ctf_field_class_enum_mapping *mapping; |
| 992 | |
| 993 | mapping = ctf_field_class_enum_borrow_mapping_by_label(tag_fc, named_fc->orig_name->str); |
| 994 | if (!mapping) { |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | for (range_i = 0; range_i < mapping->ranges->len; range_i++) { |
| 999 | struct ctf_range *range = |
| 1000 | ctf_field_class_enum_mapping_borrow_range_by_index(mapping, range_i); |
| 1001 | struct ctf_field_class_variant_range var_range; |
| 1002 | |
| 1003 | var_range.range = *range; |
| 1004 | var_range.option_index = option_i; |
| 1005 | g_array_append_val(fc->ranges, var_range); |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | static inline struct ctf_field_class * |
| 1011 | ctf_field_class_compound_borrow_field_class_by_index(struct ctf_field_class *comp_fc, |
| 1012 | uint64_t index) |
| 1013 | { |
| 1014 | struct ctf_field_class *fc = NULL; |
| 1015 | |
| 1016 | switch (comp_fc->type) { |
| 1017 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 1018 | { |
| 1019 | struct ctf_named_field_class *named_fc = ctf_field_class_struct_borrow_member_by_index( |
| 1020 | (struct ctf_field_class_struct *) comp_fc, index); |
| 1021 | |
| 1022 | BT_ASSERT_DBG(named_fc); |
| 1023 | fc = named_fc->fc; |
| 1024 | break; |
| 1025 | } |
| 1026 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1027 | { |
| 1028 | struct ctf_named_field_class *named_fc = ctf_field_class_variant_borrow_option_by_index( |
| 1029 | (struct ctf_field_class_variant *) comp_fc, index); |
| 1030 | |
| 1031 | BT_ASSERT_DBG(named_fc); |
| 1032 | fc = named_fc->fc; |
| 1033 | break; |
| 1034 | } |
| 1035 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 1036 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1037 | { |
| 1038 | struct ctf_field_class_array_base *array_fc = (struct ctf_field_class_array_base *) comp_fc; |
| 1039 | |
| 1040 | fc = array_fc->elem_fc; |
| 1041 | break; |
| 1042 | } |
| 1043 | default: |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | return fc; |
| 1048 | } |
| 1049 | |
| 1050 | static inline uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc) |
| 1051 | { |
| 1052 | uint64_t field_count; |
| 1053 | |
| 1054 | switch (fc->type) { |
| 1055 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 1056 | { |
| 1057 | struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc; |
| 1058 | |
| 1059 | field_count = struct_fc->members->len; |
| 1060 | break; |
| 1061 | } |
| 1062 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1063 | { |
| 1064 | struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc; |
| 1065 | |
| 1066 | field_count = var_fc->options->len; |
| 1067 | break; |
| 1068 | } |
| 1069 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 1070 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1071 | /* |
| 1072 | * Array and sequence types always contain a single |
| 1073 | * member (the element type). |
| 1074 | */ |
| 1075 | field_count = 1; |
| 1076 | break; |
| 1077 | default: |
| 1078 | bt_common_abort(); |
| 1079 | } |
| 1080 | |
| 1081 | return field_count; |
| 1082 | } |
| 1083 | |
| 1084 | static inline int64_t |
| 1085 | ctf_field_class_compound_get_field_class_index_from_orig_name(struct ctf_field_class *fc, |
| 1086 | const char *orig_name) |
| 1087 | { |
| 1088 | int64_t ret_index = -1; |
| 1089 | uint64_t i; |
| 1090 | |
| 1091 | switch (fc->type) { |
| 1092 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 1093 | { |
| 1094 | struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc; |
| 1095 | |
| 1096 | for (i = 0; i < struct_fc->members->len; i++) { |
| 1097 | struct ctf_named_field_class *named_fc = |
| 1098 | ctf_field_class_struct_borrow_member_by_index(struct_fc, i); |
| 1099 | |
| 1100 | if (strcmp(orig_name, named_fc->orig_name->str) == 0) { |
| 1101 | ret_index = (int64_t) i; |
| 1102 | goto end; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | break; |
| 1107 | } |
| 1108 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1109 | { |
| 1110 | struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc; |
| 1111 | |
| 1112 | for (i = 0; i < var_fc->options->len; i++) { |
| 1113 | struct ctf_named_field_class *named_fc = |
| 1114 | ctf_field_class_variant_borrow_option_by_index(var_fc, i); |
| 1115 | |
| 1116 | if (strcmp(orig_name, named_fc->orig_name->str) == 0) { |
| 1117 | ret_index = (int64_t) i; |
| 1118 | goto end; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | break; |
| 1123 | } |
| 1124 | default: |
| 1125 | break; |
| 1126 | } |
| 1127 | |
| 1128 | end: |
| 1129 | return ret_index; |
| 1130 | } |
| 1131 | |
| 1132 | static inline void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index) |
| 1133 | { |
| 1134 | BT_ASSERT(fp); |
| 1135 | g_array_append_val(fp->path, index); |
| 1136 | } |
| 1137 | |
| 1138 | static inline int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path *fp, |
| 1139 | uint64_t index) |
| 1140 | { |
| 1141 | BT_ASSERT_DBG(fp); |
| 1142 | BT_ASSERT_DBG(index < fp->path->len); |
| 1143 | return bt_g_array_index(fp->path, int64_t, index); |
| 1144 | } |
| 1145 | |
| 1146 | static inline void ctf_field_path_clear(struct ctf_field_path *fp) |
| 1147 | { |
| 1148 | BT_ASSERT(fp); |
| 1149 | g_array_set_size(fp->path, 0); |
| 1150 | } |
| 1151 | |
| 1152 | static inline const char *ctf_scope_string(enum ctf_scope scope) |
| 1153 | { |
| 1154 | switch (scope) { |
| 1155 | case CTF_SCOPE_PACKET_HEADER: |
| 1156 | return "PACKET_HEADER"; |
| 1157 | case CTF_SCOPE_PACKET_CONTEXT: |
| 1158 | return "PACKET_CONTEXT"; |
| 1159 | case CTF_SCOPE_EVENT_HEADER: |
| 1160 | return "EVENT_HEADER"; |
| 1161 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: |
| 1162 | return "EVENT_COMMON_CONTEXT"; |
| 1163 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 1164 | return "EVENT_SPECIFIC_CONTEXT"; |
| 1165 | case CTF_SCOPE_EVENT_PAYLOAD: |
| 1166 | return "EVENT_PAYLOAD"; |
| 1167 | default: |
| 1168 | bt_common_abort(); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | static inline GString *ctf_field_path_string(struct ctf_field_path *path) |
| 1173 | { |
| 1174 | GString *str = g_string_new(NULL); |
| 1175 | uint64_t i; |
| 1176 | |
| 1177 | BT_ASSERT(path); |
| 1178 | |
| 1179 | if (!str) { |
| 1180 | goto end; |
| 1181 | } |
| 1182 | |
| 1183 | g_string_append_printf(str, "[%s", ctf_scope_string(path->root)); |
| 1184 | |
| 1185 | for (i = 0; i < path->path->len; i++) { |
| 1186 | g_string_append_printf(str, ", %" PRId64, ctf_field_path_borrow_index_by_index(path, i)); |
| 1187 | } |
| 1188 | |
| 1189 | g_string_append(str, "]"); |
| 1190 | |
| 1191 | end: |
| 1192 | return str; |
| 1193 | } |
| 1194 | |
| 1195 | static inline struct ctf_field_class * |
| 1196 | ctf_field_path_borrow_field_class(struct ctf_field_path *field_path, struct ctf_trace_class *tc, |
| 1197 | struct ctf_stream_class *sc, struct ctf_event_class *ec) |
| 1198 | { |
| 1199 | uint64_t i; |
| 1200 | struct ctf_field_class *fc; |
| 1201 | |
| 1202 | switch (field_path->root) { |
| 1203 | case CTF_SCOPE_PACKET_HEADER: |
| 1204 | fc = tc->packet_header_fc; |
| 1205 | break; |
| 1206 | case CTF_SCOPE_PACKET_CONTEXT: |
| 1207 | fc = sc->packet_context_fc; |
| 1208 | break; |
| 1209 | case CTF_SCOPE_EVENT_HEADER: |
| 1210 | fc = sc->event_header_fc; |
| 1211 | break; |
| 1212 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: |
| 1213 | fc = sc->event_common_context_fc; |
| 1214 | break; |
| 1215 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 1216 | fc = ec->spec_context_fc; |
| 1217 | break; |
| 1218 | case CTF_SCOPE_EVENT_PAYLOAD: |
| 1219 | fc = ec->payload_fc; |
| 1220 | break; |
| 1221 | default: |
| 1222 | bt_common_abort(); |
| 1223 | } |
| 1224 | |
| 1225 | BT_ASSERT_DBG(fc); |
| 1226 | |
| 1227 | for (i = 0; i < field_path->path->len; i++) { |
| 1228 | int64_t child_index = ctf_field_path_borrow_index_by_index(field_path, i); |
| 1229 | struct ctf_field_class *child_fc = |
| 1230 | ctf_field_class_compound_borrow_field_class_by_index(fc, child_index); |
| 1231 | BT_ASSERT_DBG(child_fc); |
| 1232 | fc = child_fc; |
| 1233 | } |
| 1234 | |
| 1235 | BT_ASSERT_DBG(fc); |
| 1236 | return fc; |
| 1237 | } |
| 1238 | |
| 1239 | static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc); |
| 1240 | |
| 1241 | static inline void ctf_field_class_bit_array_copy_content(struct ctf_field_class_bit_array *dst_fc, |
| 1242 | struct ctf_field_class_bit_array *src_fc) |
| 1243 | { |
| 1244 | BT_ASSERT(dst_fc); |
| 1245 | BT_ASSERT(src_fc); |
| 1246 | dst_fc->byte_order = src_fc->byte_order; |
| 1247 | dst_fc->size = src_fc->size; |
| 1248 | } |
| 1249 | |
| 1250 | static inline void ctf_field_class_int_copy_content(struct ctf_field_class_int *dst_fc, |
| 1251 | struct ctf_field_class_int *src_fc) |
| 1252 | { |
| 1253 | ctf_field_class_bit_array_copy_content(&dst_fc->base, &src_fc->base); |
| 1254 | dst_fc->meaning = src_fc->meaning; |
| 1255 | dst_fc->is_signed = src_fc->is_signed; |
| 1256 | dst_fc->disp_base = src_fc->disp_base; |
| 1257 | dst_fc->encoding = src_fc->encoding; |
| 1258 | dst_fc->mapped_clock_class = src_fc->mapped_clock_class; |
| 1259 | dst_fc->storing_index = src_fc->storing_index; |
| 1260 | } |
| 1261 | |
| 1262 | static inline struct ctf_field_class_int *_ctf_field_class_int_copy(struct ctf_field_class_int *fc) |
| 1263 | { |
| 1264 | struct ctf_field_class_int *copy_fc = ctf_field_class_int_create(); |
| 1265 | |
| 1266 | BT_ASSERT(copy_fc); |
| 1267 | ctf_field_class_int_copy_content(copy_fc, fc); |
| 1268 | return copy_fc; |
| 1269 | } |
| 1270 | |
| 1271 | static inline struct ctf_field_class_enum * |
| 1272 | _ctf_field_class_enum_copy(struct ctf_field_class_enum *fc) |
| 1273 | { |
| 1274 | struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create(); |
| 1275 | uint64_t i; |
| 1276 | |
| 1277 | BT_ASSERT(copy_fc); |
| 1278 | ctf_field_class_int_copy_content(©_fc->base, &fc->base); |
| 1279 | |
| 1280 | for (i = 0; i < fc->mappings->len; i++) { |
| 1281 | uint64_t range_i; |
| 1282 | |
| 1283 | struct ctf_field_class_enum_mapping *mapping = |
| 1284 | &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i); |
| 1285 | |
| 1286 | for (range_i = 0; range_i < mapping->ranges->len; range_i++) { |
| 1287 | struct ctf_range *range = &bt_g_array_index(mapping->ranges, struct ctf_range, range_i); |
| 1288 | |
| 1289 | ctf_field_class_enum_map_range(copy_fc, mapping->label->str, range->lower.u, |
| 1290 | range->upper.u); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | return copy_fc; |
| 1295 | } |
| 1296 | |
| 1297 | static inline struct ctf_field_class_float * |
| 1298 | _ctf_field_class_float_copy(struct ctf_field_class_float *fc) |
| 1299 | { |
| 1300 | struct ctf_field_class_float *copy_fc = ctf_field_class_float_create(); |
| 1301 | |
| 1302 | BT_ASSERT(copy_fc); |
| 1303 | ctf_field_class_bit_array_copy_content(©_fc->base, &fc->base); |
| 1304 | return copy_fc; |
| 1305 | } |
| 1306 | |
| 1307 | static inline struct ctf_field_class_string * |
| 1308 | _ctf_field_class_string_copy(struct ctf_field_class_string *) |
| 1309 | { |
| 1310 | struct ctf_field_class_string *copy_fc = ctf_field_class_string_create(); |
| 1311 | |
| 1312 | BT_ASSERT(copy_fc); |
| 1313 | return copy_fc; |
| 1314 | } |
| 1315 | |
| 1316 | static inline struct ctf_field_class_struct * |
| 1317 | _ctf_field_class_struct_copy(struct ctf_field_class_struct *fc) |
| 1318 | { |
| 1319 | struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create(); |
| 1320 | uint64_t i; |
| 1321 | |
| 1322 | BT_ASSERT(copy_fc); |
| 1323 | |
| 1324 | for (i = 0; i < fc->members->len; i++) { |
| 1325 | struct ctf_named_field_class *named_fc = |
| 1326 | &bt_g_array_index(fc->members, struct ctf_named_field_class, i); |
| 1327 | |
| 1328 | ctf_field_class_struct_append_member(copy_fc, named_fc->name->str, |
| 1329 | ctf_field_class_copy(named_fc->fc)); |
| 1330 | } |
| 1331 | |
| 1332 | return copy_fc; |
| 1333 | } |
| 1334 | |
| 1335 | static inline void ctf_field_path_copy_content(struct ctf_field_path *dst_fp, |
| 1336 | struct ctf_field_path *src_fp) |
| 1337 | { |
| 1338 | uint64_t i; |
| 1339 | |
| 1340 | BT_ASSERT(dst_fp); |
| 1341 | BT_ASSERT(src_fp); |
| 1342 | dst_fp->root = src_fp->root; |
| 1343 | ctf_field_path_clear(dst_fp); |
| 1344 | |
| 1345 | for (i = 0; i < src_fp->path->len; i++) { |
| 1346 | int64_t index = ctf_field_path_borrow_index_by_index(src_fp, i); |
| 1347 | |
| 1348 | ctf_field_path_append_index(dst_fp, index); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | static inline struct ctf_field_class_variant * |
| 1353 | _ctf_field_class_variant_copy(struct ctf_field_class_variant *fc) |
| 1354 | { |
| 1355 | struct ctf_field_class_variant *copy_fc = ctf_field_class_variant_create(); |
| 1356 | uint64_t i; |
| 1357 | |
| 1358 | BT_ASSERT(copy_fc); |
| 1359 | |
| 1360 | for (i = 0; i < fc->options->len; i++) { |
| 1361 | struct ctf_named_field_class *named_fc = |
| 1362 | &bt_g_array_index(fc->options, struct ctf_named_field_class, i); |
| 1363 | |
| 1364 | ctf_field_class_variant_append_option(copy_fc, named_fc->name->str, |
| 1365 | ctf_field_class_copy(named_fc->fc)); |
| 1366 | } |
| 1367 | |
| 1368 | for (i = 0; i < fc->ranges->len; i++) { |
| 1369 | struct ctf_field_class_variant_range *range = |
| 1370 | &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, i); |
| 1371 | |
| 1372 | g_array_append_val(copy_fc->ranges, *range); |
| 1373 | } |
| 1374 | |
| 1375 | ctf_field_path_copy_content(©_fc->tag_path, &fc->tag_path); |
| 1376 | g_string_assign(copy_fc->tag_ref, fc->tag_ref->str); |
| 1377 | copy_fc->stored_tag_index = fc->stored_tag_index; |
| 1378 | return copy_fc; |
| 1379 | } |
| 1380 | |
| 1381 | static inline void |
| 1382 | ctf_field_class_array_base_copy_content(struct ctf_field_class_array_base *dst_fc, |
| 1383 | struct ctf_field_class_array_base *src_fc) |
| 1384 | { |
| 1385 | BT_ASSERT(dst_fc); |
| 1386 | BT_ASSERT(src_fc); |
| 1387 | dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc); |
| 1388 | dst_fc->is_text = src_fc->is_text; |
| 1389 | } |
| 1390 | |
| 1391 | static inline struct ctf_field_class_array * |
| 1392 | _ctf_field_class_array_copy(struct ctf_field_class_array *fc) |
| 1393 | { |
| 1394 | struct ctf_field_class_array *copy_fc = ctf_field_class_array_create(); |
| 1395 | |
| 1396 | BT_ASSERT(copy_fc); |
| 1397 | ctf_field_class_array_base_copy_content(©_fc->base, &fc->base); |
| 1398 | copy_fc->length = fc->length; |
| 1399 | return copy_fc; |
| 1400 | } |
| 1401 | |
| 1402 | static inline struct ctf_field_class_sequence * |
| 1403 | _ctf_field_class_sequence_copy(struct ctf_field_class_sequence *fc) |
| 1404 | { |
| 1405 | struct ctf_field_class_sequence *copy_fc = ctf_field_class_sequence_create(); |
| 1406 | |
| 1407 | BT_ASSERT(copy_fc); |
| 1408 | ctf_field_class_array_base_copy_content(©_fc->base, &fc->base); |
| 1409 | ctf_field_path_copy_content(©_fc->length_path, &fc->length_path); |
| 1410 | g_string_assign(copy_fc->length_ref, fc->length_ref->str); |
| 1411 | copy_fc->stored_length_index = fc->stored_length_index; |
| 1412 | return copy_fc; |
| 1413 | } |
| 1414 | |
| 1415 | static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc) |
| 1416 | { |
| 1417 | struct ctf_field_class *copy_fc = NULL; |
| 1418 | |
| 1419 | if (!fc) { |
| 1420 | goto end; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Translation should not have happened yet. |
| 1425 | */ |
| 1426 | BT_ASSERT(!fc->ir_fc); |
| 1427 | |
| 1428 | switch (fc->type) { |
| 1429 | case CTF_FIELD_CLASS_TYPE_INT: |
| 1430 | copy_fc = &_ctf_field_class_int_copy(ctf_field_class_as_int(fc))->base.base; |
| 1431 | break; |
| 1432 | case CTF_FIELD_CLASS_TYPE_ENUM: |
| 1433 | copy_fc = &_ctf_field_class_enum_copy(ctf_field_class_as_enum(fc))->base.base.base; |
| 1434 | break; |
| 1435 | case CTF_FIELD_CLASS_TYPE_FLOAT: |
| 1436 | copy_fc = &_ctf_field_class_float_copy(ctf_field_class_as_float(fc))->base.base; |
| 1437 | break; |
| 1438 | case CTF_FIELD_CLASS_TYPE_STRING: |
| 1439 | copy_fc = &_ctf_field_class_string_copy(ctf_field_class_as_string(fc))->base; |
| 1440 | break; |
| 1441 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 1442 | copy_fc = &_ctf_field_class_struct_copy(ctf_field_class_as_struct(fc))->base; |
| 1443 | break; |
| 1444 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 1445 | copy_fc = &_ctf_field_class_array_copy(ctf_field_class_as_array(fc))->base.base; |
| 1446 | break; |
| 1447 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1448 | copy_fc = &_ctf_field_class_sequence_copy(ctf_field_class_as_sequence(fc))->base.base; |
| 1449 | break; |
| 1450 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1451 | copy_fc = &_ctf_field_class_variant_copy(ctf_field_class_as_variant(fc))->base; |
| 1452 | break; |
| 1453 | default: |
| 1454 | bt_common_abort(); |
| 1455 | } |
| 1456 | |
| 1457 | copy_fc->type = fc->type; |
| 1458 | copy_fc->alignment = fc->alignment; |
| 1459 | copy_fc->in_ir = fc->in_ir; |
| 1460 | |
| 1461 | end: |
| 1462 | return copy_fc; |
| 1463 | } |
| 1464 | |
| 1465 | static inline struct ctf_event_class *ctf_event_class_create(void) |
| 1466 | { |
| 1467 | struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1); |
| 1468 | |
| 1469 | BT_ASSERT(ec); |
| 1470 | ec->name = g_string_new(NULL); |
| 1471 | BT_ASSERT(ec->name); |
| 1472 | ec->emf_uri = g_string_new(NULL); |
| 1473 | BT_ASSERT(ec->emf_uri); |
| 1474 | ec->is_log_level_set = false; |
| 1475 | return ec; |
| 1476 | } |
| 1477 | |
| 1478 | static inline void ctf_event_class_set_log_level(struct ctf_event_class *ec, |
| 1479 | enum bt_event_class_log_level log_level) |
| 1480 | { |
| 1481 | BT_ASSERT(ec); |
| 1482 | ec->log_level = log_level; |
| 1483 | ec->is_log_level_set = true; |
| 1484 | } |
| 1485 | |
| 1486 | static inline void ctf_event_class_destroy(struct ctf_event_class *ec) |
| 1487 | { |
| 1488 | if (!ec) { |
| 1489 | return; |
| 1490 | } |
| 1491 | |
| 1492 | if (ec->name) { |
| 1493 | g_string_free(ec->name, TRUE); |
| 1494 | } |
| 1495 | |
| 1496 | if (ec->emf_uri) { |
| 1497 | g_string_free(ec->emf_uri, TRUE); |
| 1498 | } |
| 1499 | |
| 1500 | ctf_field_class_destroy(ec->spec_context_fc); |
| 1501 | ctf_field_class_destroy(ec->payload_fc); |
| 1502 | g_free(ec); |
| 1503 | } |
| 1504 | |
| 1505 | static inline struct ctf_stream_class *ctf_stream_class_create(void) |
| 1506 | { |
| 1507 | struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1); |
| 1508 | |
| 1509 | BT_ASSERT(sc); |
| 1510 | sc->event_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_event_class_destroy); |
| 1511 | BT_ASSERT(sc->event_classes); |
| 1512 | sc->event_classes_by_id = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 1513 | BT_ASSERT(sc->event_classes_by_id); |
| 1514 | return sc; |
| 1515 | } |
| 1516 | |
| 1517 | static inline void ctf_stream_class_destroy(struct ctf_stream_class *sc) |
| 1518 | { |
| 1519 | if (!sc) { |
| 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | if (sc->event_classes) { |
| 1524 | g_ptr_array_free(sc->event_classes, TRUE); |
| 1525 | } |
| 1526 | |
| 1527 | if (sc->event_classes_by_id) { |
| 1528 | g_hash_table_destroy(sc->event_classes_by_id); |
| 1529 | } |
| 1530 | |
| 1531 | ctf_field_class_destroy(sc->packet_context_fc); |
| 1532 | ctf_field_class_destroy(sc->event_header_fc); |
| 1533 | ctf_field_class_destroy(sc->event_common_context_fc); |
| 1534 | g_free(sc); |
| 1535 | } |
| 1536 | |
| 1537 | static inline void ctf_stream_class_append_event_class(struct ctf_stream_class *sc, |
| 1538 | struct ctf_event_class *ec) |
| 1539 | { |
| 1540 | g_ptr_array_add(sc->event_classes, ec); |
| 1541 | g_hash_table_insert(sc->event_classes_by_id, GUINT_TO_POINTER((guint) ec->id), ec); |
| 1542 | } |
| 1543 | |
| 1544 | static inline struct ctf_event_class * |
| 1545 | ctf_stream_class_borrow_event_class_by_id(struct ctf_stream_class *sc, uint64_t type) |
| 1546 | { |
| 1547 | BT_ASSERT_DBG(sc); |
| 1548 | return (struct ctf_event_class *) g_hash_table_lookup(sc->event_classes_by_id, |
| 1549 | GUINT_TO_POINTER((guint) type)); |
| 1550 | } |
| 1551 | |
| 1552 | static inline void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry) |
| 1553 | { |
| 1554 | BT_ASSERT(entry); |
| 1555 | entry->name = g_string_new(NULL); |
| 1556 | BT_ASSERT(entry->name); |
| 1557 | entry->value.str = g_string_new(NULL); |
| 1558 | BT_ASSERT(entry->value.str); |
| 1559 | } |
| 1560 | |
| 1561 | static inline void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry) |
| 1562 | { |
| 1563 | BT_ASSERT(entry); |
| 1564 | |
| 1565 | if (entry->name) { |
| 1566 | g_string_free(entry->name, TRUE); |
| 1567 | } |
| 1568 | |
| 1569 | if (entry->value.str) { |
| 1570 | g_string_free(entry->value.str, TRUE); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | static inline struct ctf_clock_class *ctf_clock_class_create(void) |
| 1575 | { |
| 1576 | struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1); |
| 1577 | |
| 1578 | BT_ASSERT(cc); |
| 1579 | cc->name = g_string_new(NULL); |
| 1580 | BT_ASSERT(cc->name); |
| 1581 | cc->description = g_string_new(NULL); |
| 1582 | BT_ASSERT(cc->description); |
| 1583 | return cc; |
| 1584 | } |
| 1585 | |
| 1586 | static inline void ctf_clock_class_destroy(struct ctf_clock_class *cc) |
| 1587 | { |
| 1588 | if (!cc) { |
| 1589 | return; |
| 1590 | } |
| 1591 | |
| 1592 | if (cc->name) { |
| 1593 | g_string_free(cc->name, TRUE); |
| 1594 | } |
| 1595 | |
| 1596 | if (cc->description) { |
| 1597 | g_string_free(cc->description, TRUE); |
| 1598 | } |
| 1599 | |
| 1600 | bt_clock_class_put_ref(cc->ir_cc); |
| 1601 | g_free(cc); |
| 1602 | } |
| 1603 | |
| 1604 | static inline struct ctf_trace_class *ctf_trace_class_create(void) |
| 1605 | { |
| 1606 | struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1); |
| 1607 | |
| 1608 | BT_ASSERT(tc); |
| 1609 | tc->default_byte_order = CTF_BYTE_ORDER_UNKNOWN; |
| 1610 | tc->clock_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_clock_class_destroy); |
| 1611 | BT_ASSERT(tc->clock_classes); |
| 1612 | tc->stream_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_stream_class_destroy); |
| 1613 | BT_ASSERT(tc->stream_classes); |
| 1614 | tc->env_entries = g_array_new(FALSE, TRUE, sizeof(struct ctf_trace_class_env_entry)); |
| 1615 | return tc; |
| 1616 | } |
| 1617 | |
| 1618 | static inline void ctf_trace_class_destroy(struct ctf_trace_class *tc) |
| 1619 | { |
| 1620 | if (!tc) { |
| 1621 | return; |
| 1622 | } |
| 1623 | |
| 1624 | ctf_field_class_destroy(tc->packet_header_fc); |
| 1625 | |
| 1626 | if (tc->clock_classes) { |
| 1627 | g_ptr_array_free(tc->clock_classes, TRUE); |
| 1628 | } |
| 1629 | |
| 1630 | if (tc->stream_classes) { |
| 1631 | g_ptr_array_free(tc->stream_classes, TRUE); |
| 1632 | } |
| 1633 | |
| 1634 | if (tc->env_entries) { |
| 1635 | uint64_t i; |
| 1636 | |
| 1637 | for (i = 0; i < tc->env_entries->len; i++) { |
| 1638 | struct ctf_trace_class_env_entry *entry = |
| 1639 | &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, i); |
| 1640 | |
| 1641 | _ctf_trace_class_env_entry_fini(entry); |
| 1642 | } |
| 1643 | |
| 1644 | g_array_free(tc->env_entries, TRUE); |
| 1645 | } |
| 1646 | |
| 1647 | g_free(tc); |
| 1648 | } |
| 1649 | |
| 1650 | static inline void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc, const char *name, |
| 1651 | enum ctf_trace_class_env_entry_type type, |
| 1652 | const char *str_value, int64_t i_value) |
| 1653 | { |
| 1654 | struct ctf_trace_class_env_entry *entry; |
| 1655 | |
| 1656 | BT_ASSERT(tc); |
| 1657 | BT_ASSERT(name); |
| 1658 | g_array_set_size(tc->env_entries, tc->env_entries->len + 1); |
| 1659 | |
| 1660 | entry = &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, |
| 1661 | tc->env_entries->len - 1); |
| 1662 | entry->type = type; |
| 1663 | _ctf_trace_class_env_entry_init(entry); |
| 1664 | g_string_assign(entry->name, name); |
| 1665 | |
| 1666 | if (str_value) { |
| 1667 | g_string_assign(entry->value.str, str_value); |
| 1668 | } |
| 1669 | |
| 1670 | entry->value.i = i_value; |
| 1671 | } |
| 1672 | |
| 1673 | static inline struct ctf_stream_class * |
| 1674 | ctf_trace_class_borrow_stream_class_by_id(struct ctf_trace_class *tc, uint64_t id) |
| 1675 | { |
| 1676 | uint64_t i; |
| 1677 | struct ctf_stream_class *ret_sc = NULL; |
| 1678 | |
| 1679 | BT_ASSERT_DBG(tc); |
| 1680 | |
| 1681 | for (i = 0; i < tc->stream_classes->len; i++) { |
| 1682 | struct ctf_stream_class *sc = (struct ctf_stream_class *) tc->stream_classes->pdata[i]; |
| 1683 | |
| 1684 | if (sc->id == id) { |
| 1685 | ret_sc = sc; |
| 1686 | goto end; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | end: |
| 1691 | return ret_sc; |
| 1692 | } |
| 1693 | |
| 1694 | static inline struct ctf_clock_class * |
| 1695 | ctf_trace_class_borrow_clock_class_by_name(struct ctf_trace_class *tc, const char *name) |
| 1696 | { |
| 1697 | uint64_t i; |
| 1698 | struct ctf_clock_class *ret_cc = NULL; |
| 1699 | |
| 1700 | BT_ASSERT_DBG(tc); |
| 1701 | BT_ASSERT_DBG(name); |
| 1702 | |
| 1703 | for (i = 0; i < tc->clock_classes->len; i++) { |
| 1704 | struct ctf_clock_class *cc = (struct ctf_clock_class *) tc->clock_classes->pdata[i]; |
| 1705 | |
| 1706 | BT_ASSERT_DBG(cc->name); |
| 1707 | if (strcmp(cc->name->str, name) == 0) { |
| 1708 | ret_cc = cc; |
| 1709 | goto end; |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | end: |
| 1714 | return ret_cc; |
| 1715 | } |
| 1716 | |
| 1717 | static inline struct ctf_trace_class_env_entry * |
| 1718 | ctf_trace_class_borrow_env_entry_by_index(struct ctf_trace_class *tc, uint64_t index) |
| 1719 | { |
| 1720 | BT_ASSERT_DBG(tc); |
| 1721 | BT_ASSERT_DBG(index < tc->env_entries->len); |
| 1722 | return &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, index); |
| 1723 | } |
| 1724 | |
| 1725 | static inline struct ctf_trace_class_env_entry * |
| 1726 | ctf_trace_class_borrow_env_entry_by_name(struct ctf_trace_class *tc, const char *name) |
| 1727 | { |
| 1728 | struct ctf_trace_class_env_entry *ret_entry = NULL; |
| 1729 | uint64_t i; |
| 1730 | |
| 1731 | BT_ASSERT_DBG(tc); |
| 1732 | BT_ASSERT_DBG(name); |
| 1733 | |
| 1734 | for (i = 0; i < tc->env_entries->len; i++) { |
| 1735 | struct ctf_trace_class_env_entry *env_entry = |
| 1736 | ctf_trace_class_borrow_env_entry_by_index(tc, i); |
| 1737 | |
| 1738 | if (strcmp(env_entry->name->str, name) == 0) { |
| 1739 | ret_entry = env_entry; |
| 1740 | goto end; |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | end: |
| 1745 | return ret_entry; |
| 1746 | } |
| 1747 | |
| 1748 | #endif /* _CTF_META_H */ |