| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define BT_LOG_TAG "LIB/FIELD-CLASS" |
| 25 | #include "lib/logging.h" |
| 26 | |
| 27 | #include "lib/assert-pre.h" |
| 28 | #include <babeltrace2/trace-ir/field-class.h> |
| 29 | #include <babeltrace2/trace-ir/field-class-const.h> |
| 30 | #include <babeltrace2/trace-ir/field-const.h> |
| 31 | #include <babeltrace2/trace-ir/field.h> |
| 32 | #include <babeltrace2/trace-ir/clock-class.h> |
| 33 | #include "lib/object.h" |
| 34 | #include "compat/compiler.h" |
| 35 | #include "compat/endian.h" |
| 36 | #include "common/assert.h" |
| 37 | #include "compat/glib.h" |
| 38 | #include <float.h> |
| 39 | #include <inttypes.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #include "clock-class.h" |
| 43 | #include "field-class.h" |
| 44 | #include "field.h" |
| 45 | #include "field-path.h" |
| 46 | #include "utils.h" |
| 47 | #include "lib/func-status.h" |
| 48 | #include "lib/integer-range-set.h" |
| 49 | |
| 50 | enum bt_field_class_type bt_field_class_get_type( |
| 51 | const struct bt_field_class *fc) |
| 52 | { |
| 53 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 54 | return fc->type; |
| 55 | } |
| 56 | |
| 57 | static |
| 58 | void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, |
| 59 | bt_object_release_func release_func) |
| 60 | { |
| 61 | BT_ASSERT(fc); |
| 62 | BT_ASSERT(release_func); |
| 63 | bt_object_init_shared(&fc->base, release_func); |
| 64 | fc->type = type; |
| 65 | } |
| 66 | |
| 67 | static |
| 68 | void destroy_bool_field_class(struct bt_object *obj) |
| 69 | { |
| 70 | BT_ASSERT(obj); |
| 71 | BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj); |
| 72 | g_free(obj); |
| 73 | } |
| 74 | |
| 75 | struct bt_field_class *bt_field_class_bool_create( |
| 76 | bt_trace_class *trace_class) |
| 77 | { |
| 78 | struct bt_field_class_bool *bool_fc = NULL; |
| 79 | |
| 80 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 81 | BT_LOGD("Creating default boolean field class object."); |
| 82 | bool_fc = g_new0(struct bt_field_class_bool, 1); |
| 83 | if (!bool_fc) { |
| 84 | BT_LIB_LOGE_APPEND_CAUSE( |
| 85 | "Failed to allocate one boolean field class."); |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | init_field_class((void *) bool_fc, BT_FIELD_CLASS_TYPE_BOOL, |
| 90 | destroy_bool_field_class); |
| 91 | BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc); |
| 92 | goto end; |
| 93 | |
| 94 | error: |
| 95 | BT_OBJECT_PUT_REF_AND_RESET(bool_fc); |
| 96 | |
| 97 | end: |
| 98 | return (void *) bool_fc; |
| 99 | } |
| 100 | |
| 101 | static |
| 102 | void init_integer_field_class(struct bt_field_class_integer *fc, |
| 103 | enum bt_field_class_type type, |
| 104 | bt_object_release_func release_func) |
| 105 | { |
| 106 | init_field_class((void *) fc, type, release_func); |
| 107 | fc->range = 64; |
| 108 | fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
| 109 | } |
| 110 | |
| 111 | static |
| 112 | void destroy_integer_field_class(struct bt_object *obj) |
| 113 | { |
| 114 | BT_ASSERT(obj); |
| 115 | BT_LIB_LOGD("Destroying integer field class object: %!+F", obj); |
| 116 | g_free(obj); |
| 117 | } |
| 118 | |
| 119 | static inline |
| 120 | struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class, |
| 121 | enum bt_field_class_type type) |
| 122 | { |
| 123 | struct bt_field_class_integer *int_fc = NULL; |
| 124 | |
| 125 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 126 | BT_LOGD("Creating default integer field class object: type=%s", |
| 127 | bt_common_field_class_type_string(type)); |
| 128 | int_fc = g_new0(struct bt_field_class_integer, 1); |
| 129 | if (!int_fc) { |
| 130 | BT_LIB_LOGE_APPEND_CAUSE( |
| 131 | "Failed to allocate one integer field class."); |
| 132 | goto error; |
| 133 | } |
| 134 | |
| 135 | init_integer_field_class(int_fc, type, destroy_integer_field_class); |
| 136 | BT_LIB_LOGD("Created integer field class object: %!+F", int_fc); |
| 137 | goto end; |
| 138 | |
| 139 | error: |
| 140 | BT_OBJECT_PUT_REF_AND_RESET(int_fc); |
| 141 | |
| 142 | end: |
| 143 | return (void *) int_fc; |
| 144 | } |
| 145 | |
| 146 | struct bt_field_class *bt_field_class_integer_unsigned_create( |
| 147 | bt_trace_class *trace_class) |
| 148 | { |
| 149 | return create_integer_field_class(trace_class, |
| 150 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER); |
| 151 | } |
| 152 | |
| 153 | struct bt_field_class *bt_field_class_integer_signed_create( |
| 154 | bt_trace_class *trace_class) |
| 155 | { |
| 156 | return create_integer_field_class(trace_class, |
| 157 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); |
| 158 | } |
| 159 | |
| 160 | uint64_t bt_field_class_integer_get_field_value_range( |
| 161 | const struct bt_field_class *fc) |
| 162 | { |
| 163 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
| 164 | |
| 165 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 166 | BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class"); |
| 167 | return int_fc->range; |
| 168 | } |
| 169 | |
| 170 | static |
| 171 | bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc, |
| 172 | uint64_t size) |
| 173 | { |
| 174 | // TODO |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | void bt_field_class_integer_set_field_value_range( |
| 179 | struct bt_field_class *fc, uint64_t size) |
| 180 | { |
| 181 | struct bt_field_class_integer *int_fc = (void *) fc; |
| 182 | |
| 183 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 184 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); |
| 185 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
| 186 | BT_ASSERT_PRE(size <= 64, |
| 187 | "Unsupported size for integer field class's field value range " |
| 188 | "(maximum is 64): size=%" PRIu64, size); |
| 189 | BT_ASSERT_PRE( |
| 190 | int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || |
| 191 | int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || |
| 192 | size_is_valid_for_enumeration_field_class(fc, size), |
| 193 | "Invalid field value range for enumeration field class: " |
| 194 | "at least one of the current mapping ranges contains values " |
| 195 | "which are outside this range: %!+F, size=%" PRIu64, fc, size); |
| 196 | int_fc->range = size; |
| 197 | BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc); |
| 198 | } |
| 199 | |
| 200 | enum bt_field_class_integer_preferred_display_base |
| 201 | bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc) |
| 202 | { |
| 203 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
| 204 | |
| 205 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 206 | BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class"); |
| 207 | return int_fc->base; |
| 208 | } |
| 209 | |
| 210 | void bt_field_class_integer_set_preferred_display_base( |
| 211 | struct bt_field_class *fc, |
| 212 | enum bt_field_class_integer_preferred_display_base base) |
| 213 | { |
| 214 | struct bt_field_class_integer *int_fc = (void *) fc; |
| 215 | |
| 216 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 217 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); |
| 218 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
| 219 | int_fc->base = base; |
| 220 | BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc); |
| 221 | } |
| 222 | |
| 223 | static |
| 224 | void finalize_enumeration_field_class_mapping( |
| 225 | struct bt_field_class_enumeration_mapping *mapping) |
| 226 | { |
| 227 | BT_ASSERT(mapping); |
| 228 | |
| 229 | if (mapping->label) { |
| 230 | g_string_free(mapping->label, TRUE); |
| 231 | mapping->label = NULL; |
| 232 | } |
| 233 | |
| 234 | BT_OBJECT_PUT_REF_AND_RESET(mapping->range_set); |
| 235 | } |
| 236 | |
| 237 | static |
| 238 | void destroy_enumeration_field_class(struct bt_object *obj) |
| 239 | { |
| 240 | struct bt_field_class_enumeration *fc = (void *) obj; |
| 241 | |
| 242 | BT_ASSERT(fc); |
| 243 | BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc); |
| 244 | |
| 245 | if (fc->mappings) { |
| 246 | uint64_t i; |
| 247 | |
| 248 | for (i = 0; i < fc->mappings->len; i++) { |
| 249 | finalize_enumeration_field_class_mapping( |
| 250 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i)); |
| 251 | } |
| 252 | |
| 253 | g_array_free(fc->mappings, TRUE); |
| 254 | fc->mappings = NULL; |
| 255 | } |
| 256 | |
| 257 | if (fc->label_buf) { |
| 258 | g_ptr_array_free(fc->label_buf, TRUE); |
| 259 | fc->label_buf = NULL; |
| 260 | } |
| 261 | |
| 262 | g_free(fc); |
| 263 | } |
| 264 | |
| 265 | static |
| 266 | struct bt_field_class *create_enumeration_field_class( |
| 267 | bt_trace_class *trace_class, enum bt_field_class_type type) |
| 268 | { |
| 269 | struct bt_field_class_enumeration *enum_fc = NULL; |
| 270 | |
| 271 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 272 | BT_LOGD("Creating default enumeration field class object: type=%s", |
| 273 | bt_common_field_class_type_string(type)); |
| 274 | enum_fc = g_new0(struct bt_field_class_enumeration, 1); |
| 275 | if (!enum_fc) { |
| 276 | BT_LIB_LOGE_APPEND_CAUSE( |
| 277 | "Failed to allocate one enumeration field class."); |
| 278 | goto error; |
| 279 | } |
| 280 | |
| 281 | init_integer_field_class((void *) enum_fc, type, |
| 282 | destroy_enumeration_field_class); |
| 283 | enum_fc->mappings = g_array_new(FALSE, TRUE, |
| 284 | sizeof(struct bt_field_class_enumeration_mapping)); |
| 285 | if (!enum_fc->mappings) { |
| 286 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
| 287 | goto error; |
| 288 | } |
| 289 | |
| 290 | enum_fc->label_buf = g_ptr_array_new(); |
| 291 | if (!enum_fc->label_buf) { |
| 292 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc); |
| 297 | goto end; |
| 298 | |
| 299 | error: |
| 300 | BT_OBJECT_PUT_REF_AND_RESET(enum_fc); |
| 301 | |
| 302 | end: |
| 303 | return (void *) enum_fc; |
| 304 | } |
| 305 | |
| 306 | struct bt_field_class *bt_field_class_enumeration_unsigned_create( |
| 307 | bt_trace_class *trace_class) |
| 308 | { |
| 309 | return create_enumeration_field_class(trace_class, |
| 310 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
| 311 | } |
| 312 | |
| 313 | struct bt_field_class *bt_field_class_enumeration_signed_create( |
| 314 | bt_trace_class *trace_class) |
| 315 | { |
| 316 | return create_enumeration_field_class(trace_class, |
| 317 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
| 318 | } |
| 319 | |
| 320 | uint64_t bt_field_class_enumeration_get_mapping_count( |
| 321 | const struct bt_field_class *fc) |
| 322 | { |
| 323 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 324 | |
| 325 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 326 | BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc, "Field class"); |
| 327 | return (uint64_t) enum_fc->mappings->len; |
| 328 | } |
| 329 | |
| 330 | const struct bt_field_class_enumeration_unsigned_mapping * |
| 331 | bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
| 332 | const struct bt_field_class *fc, uint64_t index) |
| 333 | { |
| 334 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 335 | |
| 336 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 337 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len); |
| 338 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
| 339 | "Field class"); |
| 340 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
| 341 | } |
| 342 | |
| 343 | const struct bt_field_class_enumeration_signed_mapping * |
| 344 | bt_field_class_enumeration_signed_borrow_mapping_by_index_const( |
| 345 | const struct bt_field_class *fc, uint64_t index) |
| 346 | { |
| 347 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 348 | |
| 349 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 350 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len); |
| 351 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 352 | "Field class"); |
| 353 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
| 354 | } |
| 355 | |
| 356 | static |
| 357 | const struct bt_field_class_enumeration_mapping * |
| 358 | borrow_enumeration_field_class_mapping_by_label( |
| 359 | const struct bt_field_class_enumeration *fc, const char *label) |
| 360 | { |
| 361 | struct bt_field_class_enumeration_mapping *mapping = NULL; |
| 362 | uint64_t i; |
| 363 | |
| 364 | BT_ASSERT(fc); |
| 365 | BT_ASSERT_PRE_DEV_NON_NULL(label, "Label"); |
| 366 | |
| 367 | for (i = 0; i < fc->mappings->len; i++) { |
| 368 | struct bt_field_class_enumeration_mapping *this_mapping = |
| 369 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i); |
| 370 | |
| 371 | if (strcmp(this_mapping->label->str, label) == 0) { |
| 372 | mapping = this_mapping; |
| 373 | goto end; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | end: |
| 378 | return mapping; |
| 379 | } |
| 380 | |
| 381 | const struct bt_field_class_enumeration_signed_mapping * |
| 382 | bt_field_class_enumeration_signed_borrow_mapping_by_label_const( |
| 383 | const struct bt_field_class *fc, const char *label) |
| 384 | { |
| 385 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 386 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 387 | "Field class"); |
| 388 | return (const void *) borrow_enumeration_field_class_mapping_by_label( |
| 389 | (const void *) fc, label); |
| 390 | } |
| 391 | |
| 392 | const struct bt_field_class_enumeration_unsigned_mapping * |
| 393 | bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const( |
| 394 | const struct bt_field_class *fc, const char *label) |
| 395 | { |
| 396 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 397 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
| 398 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field class"); |
| 399 | return (const void *) borrow_enumeration_field_class_mapping_by_label( |
| 400 | (const void *) fc, label); |
| 401 | } |
| 402 | |
| 403 | const char *bt_field_class_enumeration_mapping_get_label( |
| 404 | const struct bt_field_class_enumeration_mapping *mapping) |
| 405 | { |
| 406 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); |
| 407 | return mapping->label->str; |
| 408 | } |
| 409 | |
| 410 | const struct bt_integer_range_set_unsigned * |
| 411 | bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
| 412 | const struct bt_field_class_enumeration_unsigned_mapping *u_mapping) |
| 413 | { |
| 414 | const struct bt_field_class_enumeration_mapping *mapping = |
| 415 | (const void *) u_mapping; |
| 416 | |
| 417 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); |
| 418 | return (const void *) mapping->range_set; |
| 419 | } |
| 420 | |
| 421 | const struct bt_integer_range_set_signed * |
| 422 | bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
| 423 | const struct bt_field_class_enumeration_signed_mapping *s_mapping) |
| 424 | { |
| 425 | const struct bt_field_class_enumeration_mapping *mapping = |
| 426 | (const void *) s_mapping; |
| 427 | |
| 428 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); |
| 429 | return (const void *) mapping->range_set; |
| 430 | } |
| 431 | |
| 432 | enum bt_field_class_enumeration_get_mapping_labels_for_value_status |
| 433 | bt_field_class_enumeration_unsigned_get_mapping_labels_for_value( |
| 434 | const struct bt_field_class *fc, uint64_t value, |
| 435 | bt_field_class_enumeration_mapping_label_array *label_array, |
| 436 | uint64_t *count) |
| 437 | { |
| 438 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 439 | uint64_t i; |
| 440 | |
| 441 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 442 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); |
| 443 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); |
| 444 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
| 445 | "Field class"); |
| 446 | g_ptr_array_set_size(enum_fc->label_buf, 0); |
| 447 | |
| 448 | for (i = 0; i < enum_fc->mappings->len; i++) { |
| 449 | uint64_t j; |
| 450 | const struct bt_field_class_enumeration_mapping *mapping = |
| 451 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
| 452 | |
| 453 | for (j = 0; j < mapping->range_set->ranges->len; j++) { |
| 454 | const struct bt_integer_range *range = (const void *) |
| 455 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( |
| 456 | mapping->range_set, j); |
| 457 | |
| 458 | if (value >= range->lower.u && |
| 459 | value <= range->upper.u) { |
| 460 | g_ptr_array_add(enum_fc->label_buf, |
| 461 | mapping->label->str); |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | *label_array = (void *) enum_fc->label_buf->pdata; |
| 468 | *count = (uint64_t) enum_fc->label_buf->len; |
| 469 | return BT_FUNC_STATUS_OK; |
| 470 | } |
| 471 | |
| 472 | enum bt_field_class_enumeration_get_mapping_labels_for_value_status |
| 473 | bt_field_class_enumeration_signed_get_mapping_labels_for_value( |
| 474 | const struct bt_field_class *fc, int64_t value, |
| 475 | bt_field_class_enumeration_mapping_label_array *label_array, |
| 476 | uint64_t *count) |
| 477 | { |
| 478 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 479 | uint64_t i; |
| 480 | |
| 481 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 482 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); |
| 483 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); |
| 484 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 485 | "Field class"); |
| 486 | g_ptr_array_set_size(enum_fc->label_buf, 0); |
| 487 | |
| 488 | for (i = 0; i < enum_fc->mappings->len; i++) { |
| 489 | uint64_t j; |
| 490 | const struct bt_field_class_enumeration_mapping *mapping = |
| 491 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
| 492 | |
| 493 | for (j = 0; j < mapping->range_set->ranges->len; j++) { |
| 494 | const struct bt_integer_range *range = (const void *) |
| 495 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( |
| 496 | mapping->range_set, j); |
| 497 | |
| 498 | if (value >= range->lower.i && |
| 499 | value <= range->upper.i) { |
| 500 | g_ptr_array_add(enum_fc->label_buf, |
| 501 | mapping->label->str); |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | *label_array = (void *) enum_fc->label_buf->pdata; |
| 508 | *count = (uint64_t) enum_fc->label_buf->len; |
| 509 | return BT_FUNC_STATUS_OK; |
| 510 | } |
| 511 | |
| 512 | static |
| 513 | bool enumeration_field_class_has_mapping_with_label( |
| 514 | const struct bt_field_class_enumeration *enum_fc, |
| 515 | const char *label) |
| 516 | { |
| 517 | uint64_t i; |
| 518 | bool exists = false; |
| 519 | |
| 520 | BT_ASSERT(enum_fc); |
| 521 | BT_ASSERT(label); |
| 522 | |
| 523 | for (i = 0; i < enum_fc->mappings->len; i++) { |
| 524 | struct bt_field_class_enumeration_mapping *mapping_candidate = |
| 525 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
| 526 | |
| 527 | if (strcmp(mapping_candidate->label->str, label) == 0) { |
| 528 | exists = true; |
| 529 | goto end; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | end: |
| 534 | return exists; |
| 535 | } |
| 536 | |
| 537 | static inline |
| 538 | enum bt_field_class_enumeration_add_mapping_status |
| 539 | add_mapping_to_enumeration_field_class(struct bt_field_class *fc, |
| 540 | const char *label, const struct bt_integer_range_set *range_set) |
| 541 | { |
| 542 | enum bt_field_class_enumeration_add_mapping_status status = |
| 543 | BT_FUNC_STATUS_OK; |
| 544 | struct bt_field_class_enumeration *enum_fc = (void *) fc; |
| 545 | struct bt_field_class_enumeration_mapping mapping = { 0 }; |
| 546 | |
| 547 | BT_ASSERT(fc); |
| 548 | BT_ASSERT_PRE_NON_NULL(label, "Label"); |
| 549 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
| 550 | BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label( |
| 551 | enum_fc, label), |
| 552 | "Duplicate mapping name in enumeration field class: " |
| 553 | "%![enum-fc-]+F, label=\"%s\"", fc, label); |
| 554 | mapping.range_set = range_set; |
| 555 | bt_object_get_ref(mapping.range_set); |
| 556 | mapping.label = g_string_new(label); |
| 557 | if (!mapping.label) { |
| 558 | finalize_enumeration_field_class_mapping(&mapping); |
| 559 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 560 | goto end; |
| 561 | } |
| 562 | |
| 563 | g_array_append_val(enum_fc->mappings, mapping); |
| 564 | BT_LIB_LOGD("Added mapping to enumeration field class: " |
| 565 | "%![fc-]+F, label=\"%s\"", fc, label); |
| 566 | |
| 567 | end: |
| 568 | return status; |
| 569 | } |
| 570 | |
| 571 | enum bt_field_class_enumeration_add_mapping_status |
| 572 | bt_field_class_enumeration_unsigned_add_mapping( |
| 573 | struct bt_field_class *fc, const char *label, |
| 574 | const struct bt_integer_range_set_unsigned *range_set) |
| 575 | { |
| 576 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 577 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
| 578 | "Field class"); |
| 579 | return add_mapping_to_enumeration_field_class(fc, label, |
| 580 | (const void *) range_set); |
| 581 | } |
| 582 | |
| 583 | enum bt_field_class_enumeration_add_mapping_status |
| 584 | bt_field_class_enumeration_signed_add_mapping( |
| 585 | struct bt_field_class *fc, const char *label, |
| 586 | const struct bt_integer_range_set_signed *range_set) |
| 587 | { |
| 588 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 589 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 590 | "Field class"); |
| 591 | return add_mapping_to_enumeration_field_class(fc, label, |
| 592 | (const void *) range_set); |
| 593 | } |
| 594 | |
| 595 | static |
| 596 | void destroy_real_field_class(struct bt_object *obj) |
| 597 | { |
| 598 | BT_ASSERT(obj); |
| 599 | BT_LIB_LOGD("Destroying real field class object: %!+F", obj); |
| 600 | g_free(obj); |
| 601 | } |
| 602 | |
| 603 | struct bt_field_class *bt_field_class_real_create(bt_trace_class *trace_class) |
| 604 | { |
| 605 | struct bt_field_class_real *real_fc = NULL; |
| 606 | |
| 607 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 608 | BT_LOGD_STR("Creating default real field class object."); |
| 609 | real_fc = g_new0(struct bt_field_class_real, 1); |
| 610 | if (!real_fc) { |
| 611 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class."); |
| 612 | goto error; |
| 613 | } |
| 614 | |
| 615 | init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL, |
| 616 | destroy_real_field_class); |
| 617 | BT_LIB_LOGD("Created real field class object: %!+F", real_fc); |
| 618 | goto end; |
| 619 | |
| 620 | error: |
| 621 | BT_OBJECT_PUT_REF_AND_RESET(real_fc); |
| 622 | |
| 623 | end: |
| 624 | return (void *) real_fc; |
| 625 | } |
| 626 | |
| 627 | bt_bool bt_field_class_real_is_single_precision(const struct bt_field_class *fc) |
| 628 | { |
| 629 | const struct bt_field_class_real *real_fc = (const void *) fc; |
| 630 | |
| 631 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 632 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
| 633 | return real_fc->is_single_precision; |
| 634 | } |
| 635 | |
| 636 | void bt_field_class_real_set_is_single_precision(struct bt_field_class *fc, |
| 637 | bt_bool is_single_precision) |
| 638 | { |
| 639 | struct bt_field_class_real *real_fc = (void *) fc; |
| 640 | |
| 641 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 642 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
| 643 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
| 644 | real_fc->is_single_precision = (bool) is_single_precision; |
| 645 | BT_LIB_LOGD("Set real field class's \"is single precision\" property: " |
| 646 | "%!+F", fc); |
| 647 | } |
| 648 | |
| 649 | static |
| 650 | int init_named_field_classes_container( |
| 651 | struct bt_field_class_named_field_class_container *fc, |
| 652 | enum bt_field_class_type type, |
| 653 | bt_object_release_func fc_release_func, |
| 654 | GDestroyNotify named_fc_destroy_func) |
| 655 | { |
| 656 | int ret = 0; |
| 657 | |
| 658 | init_field_class((void *) fc, type, fc_release_func); |
| 659 | fc->named_fcs = g_ptr_array_new_with_free_func(named_fc_destroy_func); |
| 660 | if (!fc->named_fcs) { |
| 661 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
| 662 | ret = -1; |
| 663 | goto end; |
| 664 | } |
| 665 | |
| 666 | fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal); |
| 667 | if (!fc->name_to_index) { |
| 668 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable."); |
| 669 | ret = -1; |
| 670 | goto end; |
| 671 | } |
| 672 | |
| 673 | end: |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static |
| 678 | void finalize_named_field_class(struct bt_named_field_class *named_fc) |
| 679 | { |
| 680 | BT_ASSERT(named_fc); |
| 681 | BT_LIB_LOGD("Finalizing named field class: " |
| 682 | "addr=%p, name=\"%s\", %![fc-]+F", |
| 683 | named_fc, named_fc->name ? named_fc->name->str : NULL, |
| 684 | named_fc->fc); |
| 685 | |
| 686 | if (named_fc->name) { |
| 687 | g_string_free(named_fc->name, TRUE); |
| 688 | named_fc->name = NULL; |
| 689 | } |
| 690 | |
| 691 | BT_LOGD_STR("Putting named field class's field class."); |
| 692 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc); |
| 693 | } |
| 694 | |
| 695 | static |
| 696 | void destroy_named_field_class(gpointer ptr) |
| 697 | { |
| 698 | if (ptr) { |
| 699 | finalize_named_field_class(ptr); |
| 700 | g_free(ptr); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | static |
| 705 | void destroy_variant_with_selector_option(gpointer ptr) |
| 706 | { |
| 707 | struct bt_field_class_variant_with_selector_option *opt = ptr; |
| 708 | |
| 709 | if (ptr) { |
| 710 | finalize_named_field_class(&opt->common); |
| 711 | BT_OBJECT_PUT_REF_AND_RESET(opt->range_set); |
| 712 | g_free(ptr); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static |
| 717 | void finalize_named_field_classes_container( |
| 718 | struct bt_field_class_named_field_class_container *fc) |
| 719 | { |
| 720 | BT_ASSERT(fc); |
| 721 | |
| 722 | if (fc->named_fcs) { |
| 723 | g_ptr_array_free(fc->named_fcs, TRUE); |
| 724 | fc->named_fcs = NULL; |
| 725 | |
| 726 | } |
| 727 | |
| 728 | if (fc->name_to_index) { |
| 729 | g_hash_table_destroy(fc->name_to_index); |
| 730 | fc->name_to_index = NULL; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | static |
| 735 | void destroy_structure_field_class(struct bt_object *obj) |
| 736 | { |
| 737 | BT_ASSERT(obj); |
| 738 | BT_LIB_LOGD("Destroying structure field class object: %!+F", obj); |
| 739 | finalize_named_field_classes_container((void *) obj); |
| 740 | g_free(obj); |
| 741 | } |
| 742 | |
| 743 | struct bt_field_class *bt_field_class_structure_create( |
| 744 | bt_trace_class *trace_class) |
| 745 | { |
| 746 | int ret; |
| 747 | struct bt_field_class_structure *struct_fc = NULL; |
| 748 | |
| 749 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 750 | BT_LOGD_STR("Creating default structure field class object."); |
| 751 | struct_fc = g_new0(struct bt_field_class_structure, 1); |
| 752 | if (!struct_fc) { |
| 753 | BT_LIB_LOGE_APPEND_CAUSE( |
| 754 | "Failed to allocate one structure field class."); |
| 755 | goto error; |
| 756 | } |
| 757 | |
| 758 | ret = init_named_field_classes_container((void *) struct_fc, |
| 759 | BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class, |
| 760 | destroy_named_field_class); |
| 761 | if (ret) { |
| 762 | /* init_named_field_classes_container() logs errors */ |
| 763 | goto error; |
| 764 | } |
| 765 | |
| 766 | BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc); |
| 767 | goto end; |
| 768 | |
| 769 | error: |
| 770 | BT_OBJECT_PUT_REF_AND_RESET(struct_fc); |
| 771 | |
| 772 | end: |
| 773 | return (void *) struct_fc; |
| 774 | } |
| 775 | |
| 776 | static |
| 777 | int init_named_field_class(struct bt_named_field_class *named_fc, |
| 778 | const char *name, struct bt_field_class *fc) |
| 779 | { |
| 780 | int status = BT_FUNC_STATUS_OK; |
| 781 | |
| 782 | BT_ASSERT(named_fc); |
| 783 | BT_ASSERT(name); |
| 784 | BT_ASSERT(fc); |
| 785 | named_fc->name = g_string_new(name); |
| 786 | if (!named_fc->name) { |
| 787 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
| 788 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 789 | goto end; |
| 790 | } |
| 791 | |
| 792 | named_fc->fc = fc; |
| 793 | bt_object_get_no_null_check(named_fc->fc); |
| 794 | bt_named_field_class_freeze(named_fc); |
| 795 | |
| 796 | end: |
| 797 | return status; |
| 798 | } |
| 799 | |
| 800 | static |
| 801 | struct bt_named_field_class *create_named_field_class(const char *name, |
| 802 | struct bt_field_class *fc) |
| 803 | { |
| 804 | struct bt_named_field_class *named_fc = g_new0( |
| 805 | struct bt_named_field_class, 1); |
| 806 | |
| 807 | if (!named_fc) { |
| 808 | BT_LIB_LOGE_APPEND_CAUSE( |
| 809 | "Failed to allocate a named field class."); |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | if (init_named_field_class(named_fc, name, fc)) { |
| 814 | /* init_named_field_class() logs errors */ |
| 815 | goto error; |
| 816 | } |
| 817 | |
| 818 | goto end; |
| 819 | |
| 820 | error: |
| 821 | destroy_named_field_class(named_fc); |
| 822 | named_fc = NULL; |
| 823 | |
| 824 | end: |
| 825 | return named_fc; |
| 826 | } |
| 827 | |
| 828 | static |
| 829 | struct bt_field_class_variant_with_selector_option * |
| 830 | create_variant_with_selector_option( |
| 831 | const char *name, struct bt_field_class *fc, |
| 832 | const struct bt_integer_range_set *range_set) |
| 833 | { |
| 834 | struct bt_field_class_variant_with_selector_option *opt = g_new0( |
| 835 | struct bt_field_class_variant_with_selector_option, 1); |
| 836 | |
| 837 | BT_ASSERT(range_set); |
| 838 | |
| 839 | if (!opt) { |
| 840 | BT_LIB_LOGE_APPEND_CAUSE( |
| 841 | "Failed to allocate a named field class."); |
| 842 | goto error; |
| 843 | } |
| 844 | |
| 845 | if (init_named_field_class(&opt->common, name, fc)) { |
| 846 | goto error; |
| 847 | } |
| 848 | |
| 849 | opt->range_set = range_set; |
| 850 | bt_object_get_no_null_check(opt->range_set); |
| 851 | bt_integer_range_set_freeze(range_set); |
| 852 | goto end; |
| 853 | |
| 854 | error: |
| 855 | destroy_variant_with_selector_option(opt); |
| 856 | opt = NULL; |
| 857 | |
| 858 | end: |
| 859 | return opt; |
| 860 | } |
| 861 | |
| 862 | static |
| 863 | int append_named_field_class_to_container_field_class( |
| 864 | struct bt_field_class_named_field_class_container *container_fc, |
| 865 | struct bt_named_field_class *named_fc) |
| 866 | { |
| 867 | BT_ASSERT(container_fc); |
| 868 | BT_ASSERT(named_fc); |
| 869 | BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class"); |
| 870 | BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index, |
| 871 | named_fc->name->str), |
| 872 | "Duplicate member/option name in structure/variant field class: " |
| 873 | "%![container-fc-]+F, name=\"%s\"", container_fc, |
| 874 | named_fc->name->str); |
| 875 | g_ptr_array_add(container_fc->named_fcs, named_fc); |
| 876 | g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, |
| 877 | GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); |
| 878 | return BT_FUNC_STATUS_OK; |
| 879 | } |
| 880 | |
| 881 | enum bt_field_class_structure_append_member_status |
| 882 | bt_field_class_structure_append_member( |
| 883 | struct bt_field_class *fc, const char *name, |
| 884 | struct bt_field_class *member_fc) |
| 885 | { |
| 886 | enum bt_field_class_structure_append_member_status status; |
| 887 | struct bt_named_field_class *named_fc = NULL; |
| 888 | |
| 889 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 890 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 891 | "Field class"); |
| 892 | named_fc = create_named_field_class(name, member_fc); |
| 893 | if (!named_fc) { |
| 894 | /* create_named_field_class() logs errors */ |
| 895 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 896 | goto end; |
| 897 | } |
| 898 | |
| 899 | status = append_named_field_class_to_container_field_class((void *) fc, |
| 900 | named_fc); |
| 901 | if (status == BT_FUNC_STATUS_OK) { |
| 902 | /* Moved to the container */ |
| 903 | named_fc = NULL; |
| 904 | } |
| 905 | |
| 906 | end: |
| 907 | return status; |
| 908 | } |
| 909 | |
| 910 | uint64_t bt_field_class_structure_get_member_count( |
| 911 | const struct bt_field_class *fc) |
| 912 | { |
| 913 | struct bt_field_class_structure *struct_fc = (void *) fc; |
| 914 | |
| 915 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 916 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 917 | "Field class"); |
| 918 | return (uint64_t) struct_fc->common.named_fcs->len; |
| 919 | } |
| 920 | |
| 921 | static |
| 922 | struct bt_named_field_class * |
| 923 | borrow_named_field_class_from_container_field_class_at_index( |
| 924 | struct bt_field_class_named_field_class_container *fc, |
| 925 | uint64_t index) |
| 926 | { |
| 927 | BT_ASSERT(fc); |
| 928 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len); |
| 929 | return fc->named_fcs->pdata[index]; |
| 930 | } |
| 931 | |
| 932 | const struct bt_field_class_structure_member * |
| 933 | bt_field_class_structure_borrow_member_by_index_const( |
| 934 | const struct bt_field_class *fc, uint64_t index) |
| 935 | { |
| 936 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 937 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 938 | "Field class"); |
| 939 | return (const void *) |
| 940 | borrow_named_field_class_from_container_field_class_at_index( |
| 941 | (void *) fc, index); |
| 942 | } |
| 943 | |
| 944 | struct bt_field_class_structure_member * |
| 945 | bt_field_class_structure_borrow_member_by_index( |
| 946 | struct bt_field_class *fc, uint64_t index) |
| 947 | { |
| 948 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 949 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 950 | "Field class"); |
| 951 | return (void *) |
| 952 | borrow_named_field_class_from_container_field_class_at_index( |
| 953 | (void *) fc, index); |
| 954 | } |
| 955 | |
| 956 | static |
| 957 | struct bt_named_field_class * |
| 958 | borrow_named_field_class_from_container_field_class_by_name( |
| 959 | struct bt_field_class_named_field_class_container *fc, |
| 960 | const char *name) |
| 961 | { |
| 962 | struct bt_named_field_class *named_fc = NULL; |
| 963 | gpointer orig_key; |
| 964 | gpointer value; |
| 965 | |
| 966 | BT_ASSERT(fc); |
| 967 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Name"); |
| 968 | if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key, |
| 969 | &value)) { |
| 970 | goto end; |
| 971 | } |
| 972 | |
| 973 | named_fc = fc->named_fcs->pdata[GPOINTER_TO_UINT(value)]; |
| 974 | |
| 975 | end: |
| 976 | return named_fc; |
| 977 | } |
| 978 | |
| 979 | const struct bt_field_class_structure_member * |
| 980 | bt_field_class_structure_borrow_member_by_name_const( |
| 981 | const struct bt_field_class *fc, const char *name) |
| 982 | { |
| 983 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 984 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 985 | "Field class"); |
| 986 | return (const void *) |
| 987 | borrow_named_field_class_from_container_field_class_by_name( |
| 988 | (void *) fc, name); |
| 989 | } |
| 990 | |
| 991 | struct bt_field_class_structure_member * |
| 992 | bt_field_class_structure_borrow_member_by_name( |
| 993 | struct bt_field_class *fc, const char *name) |
| 994 | { |
| 995 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 996 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 997 | "Field class"); |
| 998 | return (void *) |
| 999 | borrow_named_field_class_from_container_field_class_by_name( |
| 1000 | (void *) fc, name); |
| 1001 | } |
| 1002 | |
| 1003 | const char *bt_field_class_structure_member_get_name( |
| 1004 | const struct bt_field_class_structure_member *member) |
| 1005 | { |
| 1006 | const struct bt_named_field_class *named_fc = (const void *) member; |
| 1007 | |
| 1008 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); |
| 1009 | return named_fc->name->str; |
| 1010 | } |
| 1011 | |
| 1012 | const struct bt_field_class * |
| 1013 | bt_field_class_structure_member_borrow_field_class_const( |
| 1014 | const struct bt_field_class_structure_member *member) |
| 1015 | { |
| 1016 | const struct bt_named_field_class *named_fc = (const void *) member; |
| 1017 | |
| 1018 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); |
| 1019 | return named_fc->fc; |
| 1020 | } |
| 1021 | |
| 1022 | static |
| 1023 | void finalize_variant_field_class(struct bt_field_class_variant *var_fc) |
| 1024 | { |
| 1025 | BT_ASSERT(var_fc); |
| 1026 | BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc); |
| 1027 | finalize_named_field_classes_container((void *) var_fc); |
| 1028 | } |
| 1029 | |
| 1030 | static |
| 1031 | void destroy_variant_field_class(struct bt_object *obj) |
| 1032 | { |
| 1033 | struct bt_field_class_variant *fc = (void *) obj; |
| 1034 | |
| 1035 | BT_ASSERT(fc); |
| 1036 | finalize_variant_field_class(fc); |
| 1037 | g_free(fc); |
| 1038 | } |
| 1039 | |
| 1040 | static |
| 1041 | void destroy_variant_with_selector_field_class(struct bt_object *obj) |
| 1042 | { |
| 1043 | struct bt_field_class_variant_with_selector *fc = (void *) obj; |
| 1044 | |
| 1045 | BT_ASSERT(fc); |
| 1046 | finalize_variant_field_class(&fc->common); |
| 1047 | BT_LOGD_STR("Putting selector field path."); |
| 1048 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path); |
| 1049 | BT_LOGD_STR("Putting selector field class."); |
| 1050 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc); |
| 1051 | g_free(fc); |
| 1052 | } |
| 1053 | |
| 1054 | struct bt_field_class *bt_field_class_variant_create( |
| 1055 | bt_trace_class *trace_class, bt_field_class *selector_fc) |
| 1056 | { |
| 1057 | int ret; |
| 1058 | struct bt_field_class_variant *var_fc = NULL; |
| 1059 | struct bt_field_class_variant_with_selector *var_with_sel_fc = NULL; |
| 1060 | enum bt_field_class_type fc_type; |
| 1061 | |
| 1062 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1063 | |
| 1064 | if (selector_fc) { |
| 1065 | BT_ASSERT_PRE_FC_IS_INT(selector_fc, "Selector field class"); |
| 1066 | } |
| 1067 | |
| 1068 | BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F", |
| 1069 | selector_fc); |
| 1070 | |
| 1071 | if (selector_fc) { |
| 1072 | var_with_sel_fc = g_new0( |
| 1073 | struct bt_field_class_variant_with_selector, 1); |
| 1074 | if (!var_with_sel_fc) { |
| 1075 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1076 | "Failed to allocate one variant field class with selector."); |
| 1077 | goto error; |
| 1078 | } |
| 1079 | |
| 1080 | if (selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || |
| 1081 | selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) { |
| 1082 | fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR; |
| 1083 | } else { |
| 1084 | fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR; |
| 1085 | } |
| 1086 | |
| 1087 | ret = init_named_field_classes_container( |
| 1088 | (void *) var_with_sel_fc, fc_type, |
| 1089 | destroy_variant_with_selector_field_class, |
| 1090 | destroy_variant_with_selector_option); |
| 1091 | if (ret) { |
| 1092 | /* init_named_field_classes_container() logs errors */ |
| 1093 | goto error; |
| 1094 | } |
| 1095 | |
| 1096 | var_with_sel_fc->selector_fc = selector_fc; |
| 1097 | bt_object_get_no_null_check(var_with_sel_fc->selector_fc); |
| 1098 | bt_field_class_freeze(selector_fc); |
| 1099 | var_fc = (void *) var_with_sel_fc; |
| 1100 | } else { |
| 1101 | var_fc = g_new0(struct bt_field_class_variant, 1); |
| 1102 | if (!var_fc) { |
| 1103 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1104 | "Failed to allocate one variant field class without selector."); |
| 1105 | goto error; |
| 1106 | } |
| 1107 | |
| 1108 | ret = init_named_field_classes_container((void *) var_fc, |
| 1109 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR, |
| 1110 | destroy_variant_field_class, destroy_named_field_class); |
| 1111 | if (ret) { |
| 1112 | /* init_named_field_classes_container() logs errors */ |
| 1113 | goto error; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | BT_ASSERT(var_fc); |
| 1118 | BT_LIB_LOGD("Created default variant field class with selector object: " |
| 1119 | "%![var-fc-]+F, %![sel-fc-]+F", var_fc, selector_fc); |
| 1120 | goto end; |
| 1121 | |
| 1122 | error: |
| 1123 | BT_OBJECT_PUT_REF_AND_RESET(var_fc); |
| 1124 | |
| 1125 | end: |
| 1126 | return (void *) var_fc; |
| 1127 | } |
| 1128 | |
| 1129 | enum bt_field_class_variant_without_selector_append_option_status |
| 1130 | bt_field_class_variant_without_selector_append_option(struct bt_field_class *fc, |
| 1131 | const char *name, struct bt_field_class *option_fc) |
| 1132 | { |
| 1133 | enum bt_field_class_variant_without_selector_append_option_status status; |
| 1134 | struct bt_named_field_class *named_fc = NULL; |
| 1135 | |
| 1136 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1137 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 1138 | BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class"); |
| 1139 | BT_ASSERT_PRE_FC_HAS_ID(fc, |
| 1140 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR, "Field class"); |
| 1141 | named_fc = create_named_field_class(name, option_fc); |
| 1142 | if (!named_fc) { |
| 1143 | /* create_named_field_class() logs errors */ |
| 1144 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1145 | goto end; |
| 1146 | } |
| 1147 | |
| 1148 | status = append_named_field_class_to_container_field_class((void *) fc, |
| 1149 | named_fc); |
| 1150 | if (status == BT_FUNC_STATUS_OK) { |
| 1151 | /* Moved to the container */ |
| 1152 | named_fc = NULL; |
| 1153 | } |
| 1154 | |
| 1155 | end: |
| 1156 | if (named_fc) { |
| 1157 | destroy_named_field_class(named_fc); |
| 1158 | } |
| 1159 | |
| 1160 | return status; |
| 1161 | } |
| 1162 | |
| 1163 | static |
| 1164 | int ranges_overlap(GPtrArray *var_fc_opts, const struct bt_integer_range_set *range_set, |
| 1165 | bool is_signed, bool *has_overlap) |
| 1166 | { |
| 1167 | int status = BT_FUNC_STATUS_OK; |
| 1168 | struct bt_integer_range_set *full_range_set; |
| 1169 | uint64_t i; |
| 1170 | |
| 1171 | *has_overlap = false; |
| 1172 | |
| 1173 | /* |
| 1174 | * Build a single range set with all the ranges and test for |
| 1175 | * overlaps. |
| 1176 | */ |
| 1177 | if (is_signed) { |
| 1178 | full_range_set = (void *) bt_integer_range_set_signed_create(); |
| 1179 | } else { |
| 1180 | full_range_set = (void *) bt_integer_range_set_unsigned_create(); |
| 1181 | } |
| 1182 | |
| 1183 | if (!full_range_set) { |
| 1184 | BT_LOGE_STR("Failed to create a range set."); |
| 1185 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1186 | goto end; |
| 1187 | } |
| 1188 | |
| 1189 | /* Add existing option ranges */ |
| 1190 | for (i = 0; i < var_fc_opts->len; i++) { |
| 1191 | struct bt_field_class_variant_with_selector_option *opt = |
| 1192 | var_fc_opts->pdata[i]; |
| 1193 | uint64_t j; |
| 1194 | |
| 1195 | for (j = 0; j < opt->range_set->ranges->len; j++) { |
| 1196 | struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( |
| 1197 | opt->range_set, j); |
| 1198 | |
| 1199 | if (is_signed) { |
| 1200 | status = bt_integer_range_set_signed_add_range( |
| 1201 | (void *) full_range_set, range->lower.i, |
| 1202 | range->upper.i); |
| 1203 | } else { |
| 1204 | status = bt_integer_range_set_unsigned_add_range( |
| 1205 | (void *) full_range_set, range->lower.u, |
| 1206 | range->upper.u); |
| 1207 | } |
| 1208 | |
| 1209 | if (status) { |
| 1210 | goto end; |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | /* Add new ranges */ |
| 1216 | for (i = 0; i < range_set->ranges->len; i++) { |
| 1217 | struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( |
| 1218 | range_set, i); |
| 1219 | |
| 1220 | if (is_signed) { |
| 1221 | status = bt_integer_range_set_signed_add_range( |
| 1222 | (void *) full_range_set, range->lower.i, |
| 1223 | range->upper.i); |
| 1224 | } else { |
| 1225 | status = bt_integer_range_set_unsigned_add_range( |
| 1226 | (void *) full_range_set, range->lower.u, |
| 1227 | range->upper.u); |
| 1228 | } |
| 1229 | |
| 1230 | if (status) { |
| 1231 | goto end; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* Check overlaps */ |
| 1236 | if (is_signed) { |
| 1237 | *has_overlap = bt_integer_range_set_signed_has_overlaps(full_range_set); |
| 1238 | } else { |
| 1239 | *has_overlap = bt_integer_range_set_unsigned_has_overlaps( |
| 1240 | full_range_set); |
| 1241 | } |
| 1242 | |
| 1243 | end: |
| 1244 | bt_object_put_ref(full_range_set); |
| 1245 | return status; |
| 1246 | } |
| 1247 | |
| 1248 | static |
| 1249 | int append_option_to_variant_with_selector_field_class( |
| 1250 | struct bt_field_class *fc, const char *name, |
| 1251 | struct bt_field_class *option_fc, |
| 1252 | const struct bt_integer_range_set *range_set, |
| 1253 | enum bt_field_class_type expected_type) |
| 1254 | { |
| 1255 | int status; |
| 1256 | struct bt_field_class_variant_with_selector *var_fc = (void *) fc; |
| 1257 | struct bt_field_class_variant_with_selector_option *opt = NULL; |
| 1258 | bool has_overlap; |
| 1259 | |
| 1260 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1261 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 1262 | BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class"); |
| 1263 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
| 1264 | BT_ASSERT_PRE_FC_HAS_ID(fc, expected_type, "Field class"); |
| 1265 | BT_ASSERT_PRE(range_set->ranges->len > 0, |
| 1266 | "Integer range set is empty: %!+R", range_set); |
| 1267 | status = ranges_overlap(var_fc->common.common.named_fcs, range_set, |
| 1268 | expected_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, |
| 1269 | &has_overlap); |
| 1270 | if (status) { |
| 1271 | /* ranges_overlap() logs errors */ |
| 1272 | goto end; |
| 1273 | } |
| 1274 | |
| 1275 | BT_ASSERT_PRE(!has_overlap, |
| 1276 | "Integer range set's ranges and existing ranges have an overlap: " |
| 1277 | "%!+R", range_set); |
| 1278 | opt = create_variant_with_selector_option(name, option_fc, range_set); |
| 1279 | if (!opt) { |
| 1280 | /* create_variant_with_selector_option() logs errors */ |
| 1281 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 1282 | goto end; |
| 1283 | } |
| 1284 | |
| 1285 | status = append_named_field_class_to_container_field_class((void *) fc, |
| 1286 | &opt->common); |
| 1287 | if (status == BT_FUNC_STATUS_OK) { |
| 1288 | /* Moved to the container */ |
| 1289 | opt = NULL; |
| 1290 | } |
| 1291 | |
| 1292 | end: |
| 1293 | if (opt) { |
| 1294 | destroy_variant_with_selector_option(opt); |
| 1295 | } |
| 1296 | |
| 1297 | return status; |
| 1298 | } |
| 1299 | |
| 1300 | enum bt_field_class_variant_with_selector_append_option_status |
| 1301 | bt_field_class_variant_with_selector_unsigned_append_option( |
| 1302 | struct bt_field_class *fc, const char *name, |
| 1303 | struct bt_field_class *option_fc, |
| 1304 | const struct bt_integer_range_set_unsigned *range_set) |
| 1305 | { |
| 1306 | return append_option_to_variant_with_selector_field_class(fc, |
| 1307 | name, option_fc, (const void *) range_set, |
| 1308 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR); |
| 1309 | } |
| 1310 | |
| 1311 | enum bt_field_class_variant_with_selector_append_option_status |
| 1312 | bt_field_class_variant_with_selector_signed_append_option( |
| 1313 | struct bt_field_class *fc, const char *name, |
| 1314 | struct bt_field_class *option_fc, |
| 1315 | const struct bt_integer_range_set_signed *range_set) |
| 1316 | { |
| 1317 | return append_option_to_variant_with_selector_field_class(fc, |
| 1318 | name, option_fc, (const void *) range_set, |
| 1319 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR); |
| 1320 | } |
| 1321 | |
| 1322 | uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc) |
| 1323 | { |
| 1324 | const struct bt_field_class_variant *var_fc = (const void *) fc; |
| 1325 | |
| 1326 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1327 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
| 1328 | return (uint64_t) var_fc->common.named_fcs->len; |
| 1329 | } |
| 1330 | |
| 1331 | const struct bt_field_class_variant_option * |
| 1332 | bt_field_class_variant_borrow_option_by_name_const( |
| 1333 | const struct bt_field_class *fc, const char *name) |
| 1334 | { |
| 1335 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1336 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
| 1337 | return (const void *) |
| 1338 | borrow_named_field_class_from_container_field_class_by_name( |
| 1339 | (void *) fc, name); |
| 1340 | } |
| 1341 | |
| 1342 | const struct bt_field_class_variant_option * |
| 1343 | bt_field_class_variant_borrow_option_by_index_const( |
| 1344 | const struct bt_field_class *fc, uint64_t index) |
| 1345 | { |
| 1346 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1347 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
| 1348 | return (const void *) |
| 1349 | borrow_named_field_class_from_container_field_class_at_index( |
| 1350 | (void *) fc, index); |
| 1351 | } |
| 1352 | |
| 1353 | const struct bt_field_class_variant_with_selector_unsigned_option * |
| 1354 | bt_field_class_variant_with_selector_unsigned_borrow_option_by_name_const( |
| 1355 | const struct bt_field_class *fc, const char *name) |
| 1356 | { |
| 1357 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1358 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
| 1359 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR, |
| 1360 | "Field class"); |
| 1361 | return (const void *) |
| 1362 | borrow_named_field_class_from_container_field_class_by_name( |
| 1363 | (void *) fc, name); |
| 1364 | } |
| 1365 | |
| 1366 | const struct bt_field_class_variant_with_selector_unsigned_option * |
| 1367 | bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const( |
| 1368 | const struct bt_field_class *fc, uint64_t index) |
| 1369 | { |
| 1370 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1371 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
| 1372 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR, |
| 1373 | "Field class"); |
| 1374 | return (const void *) |
| 1375 | borrow_named_field_class_from_container_field_class_at_index( |
| 1376 | (void *) fc, index); |
| 1377 | } |
| 1378 | |
| 1379 | const struct bt_field_class_variant_with_selector_signed_option * |
| 1380 | bt_field_class_variant_with_selector_signed_borrow_option_by_name_const( |
| 1381 | const struct bt_field_class *fc, const char *name) |
| 1382 | { |
| 1383 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1384 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
| 1385 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, |
| 1386 | "Field class"); |
| 1387 | return (const void *) |
| 1388 | borrow_named_field_class_from_container_field_class_by_name( |
| 1389 | (void *) fc, name); |
| 1390 | } |
| 1391 | |
| 1392 | const struct bt_field_class_variant_with_selector_signed_option * |
| 1393 | bt_field_class_variant_with_selector_signed_borrow_option_by_index_const( |
| 1394 | const struct bt_field_class *fc, uint64_t index) |
| 1395 | { |
| 1396 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1397 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
| 1398 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, |
| 1399 | "Field class"); |
| 1400 | return (const void *) |
| 1401 | borrow_named_field_class_from_container_field_class_at_index( |
| 1402 | (void *) fc, index); |
| 1403 | } |
| 1404 | |
| 1405 | const char *bt_field_class_variant_option_get_name( |
| 1406 | const struct bt_field_class_variant_option *option) |
| 1407 | { |
| 1408 | const struct bt_named_field_class *named_fc = (const void *) option; |
| 1409 | |
| 1410 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
| 1411 | return named_fc->name->str; |
| 1412 | } |
| 1413 | |
| 1414 | const struct bt_field_class * |
| 1415 | bt_field_class_variant_option_borrow_field_class_const( |
| 1416 | const struct bt_field_class_variant_option *option) |
| 1417 | { |
| 1418 | const struct bt_named_field_class *named_fc = (const void *) option; |
| 1419 | |
| 1420 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
| 1421 | return named_fc->fc; |
| 1422 | } |
| 1423 | |
| 1424 | const struct bt_integer_range_set_unsigned * |
| 1425 | bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const( |
| 1426 | const struct bt_field_class_variant_with_selector_unsigned_option *option) |
| 1427 | { |
| 1428 | const struct bt_field_class_variant_with_selector_option *opt = |
| 1429 | (const void *) option; |
| 1430 | |
| 1431 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
| 1432 | return (const void *) opt->range_set; |
| 1433 | } |
| 1434 | |
| 1435 | const struct bt_integer_range_set_signed * |
| 1436 | bt_field_class_variant_with_selector_signed_option_borrow_ranges_const( |
| 1437 | const struct bt_field_class_variant_with_selector_signed_option *option) |
| 1438 | { |
| 1439 | const struct bt_field_class_variant_with_selector_option *opt = |
| 1440 | (const void *) option; |
| 1441 | |
| 1442 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
| 1443 | return (const void *) opt->range_set; |
| 1444 | } |
| 1445 | |
| 1446 | const struct bt_field_path * |
| 1447 | bt_field_class_variant_with_selector_borrow_selector_field_path_const( |
| 1448 | const struct bt_field_class *fc) |
| 1449 | { |
| 1450 | const struct bt_field_class_variant_with_selector *var_fc = |
| 1451 | (const void *) fc; |
| 1452 | |
| 1453 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1454 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc, "Field class"); |
| 1455 | return var_fc->selector_field_path; |
| 1456 | } |
| 1457 | |
| 1458 | static |
| 1459 | void init_array_field_class(struct bt_field_class_array *fc, |
| 1460 | enum bt_field_class_type type, bt_object_release_func release_func, |
| 1461 | struct bt_field_class *element_fc) |
| 1462 | { |
| 1463 | BT_ASSERT(element_fc); |
| 1464 | init_field_class((void *) fc, type, release_func); |
| 1465 | fc->element_fc = element_fc; |
| 1466 | bt_object_get_no_null_check(fc->element_fc); |
| 1467 | bt_field_class_freeze(element_fc); |
| 1468 | } |
| 1469 | |
| 1470 | static |
| 1471 | void finalize_array_field_class(struct bt_field_class_array *array_fc) |
| 1472 | { |
| 1473 | BT_ASSERT(array_fc); |
| 1474 | BT_LOGD_STR("Putting element field class."); |
| 1475 | BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc); |
| 1476 | } |
| 1477 | |
| 1478 | static |
| 1479 | void destroy_static_array_field_class(struct bt_object *obj) |
| 1480 | { |
| 1481 | BT_ASSERT(obj); |
| 1482 | BT_LIB_LOGD("Destroying static array field class object: %!+F", obj); |
| 1483 | finalize_array_field_class((void *) obj); |
| 1484 | g_free(obj); |
| 1485 | } |
| 1486 | |
| 1487 | struct bt_field_class * |
| 1488 | bt_field_class_array_static_create(bt_trace_class *trace_class, |
| 1489 | struct bt_field_class *element_fc, uint64_t length) |
| 1490 | { |
| 1491 | struct bt_field_class_array_static *array_fc = NULL; |
| 1492 | |
| 1493 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1494 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
| 1495 | BT_LOGD_STR("Creating default static array field class object."); |
| 1496 | array_fc = g_new0(struct bt_field_class_array_static, 1); |
| 1497 | if (!array_fc) { |
| 1498 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1499 | "Failed to allocate one static array field class."); |
| 1500 | goto error; |
| 1501 | } |
| 1502 | |
| 1503 | init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
| 1504 | destroy_static_array_field_class, element_fc); |
| 1505 | array_fc->length = length; |
| 1506 | BT_LIB_LOGD("Created static array field class object: %!+F", array_fc); |
| 1507 | goto end; |
| 1508 | |
| 1509 | error: |
| 1510 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
| 1511 | |
| 1512 | end: |
| 1513 | return (void *) array_fc; |
| 1514 | } |
| 1515 | |
| 1516 | const struct bt_field_class * |
| 1517 | bt_field_class_array_borrow_element_field_class_const( |
| 1518 | const struct bt_field_class *fc) |
| 1519 | { |
| 1520 | const struct bt_field_class_array *array_fc = (const void *) fc; |
| 1521 | |
| 1522 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1523 | BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class"); |
| 1524 | return array_fc->element_fc; |
| 1525 | } |
| 1526 | |
| 1527 | struct bt_field_class * |
| 1528 | bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc) |
| 1529 | { |
| 1530 | struct bt_field_class_array *array_fc = (void *) fc; |
| 1531 | |
| 1532 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1533 | BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class"); |
| 1534 | return array_fc->element_fc; |
| 1535 | } |
| 1536 | |
| 1537 | uint64_t bt_field_class_array_static_get_length(const struct bt_field_class *fc) |
| 1538 | { |
| 1539 | const struct bt_field_class_array_static *array_fc = (const void *) fc; |
| 1540 | |
| 1541 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1542 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
| 1543 | "Field class"); |
| 1544 | return (uint64_t) array_fc->length; |
| 1545 | } |
| 1546 | |
| 1547 | static |
| 1548 | void destroy_dynamic_array_field_class(struct bt_object *obj) |
| 1549 | { |
| 1550 | struct bt_field_class_array_dynamic *fc = (void *) obj; |
| 1551 | |
| 1552 | BT_ASSERT(fc); |
| 1553 | BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc); |
| 1554 | finalize_array_field_class((void *) fc); |
| 1555 | BT_LOGD_STR("Putting length field path."); |
| 1556 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path); |
| 1557 | BT_LOGD_STR("Putting length field class."); |
| 1558 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc); |
| 1559 | g_free(fc); |
| 1560 | } |
| 1561 | |
| 1562 | struct bt_field_class *bt_field_class_array_dynamic_create( |
| 1563 | struct bt_trace_class *trace_class, |
| 1564 | struct bt_field_class *element_fc, |
| 1565 | struct bt_field_class *length_fc) |
| 1566 | { |
| 1567 | struct bt_field_class_array_dynamic *array_fc = NULL; |
| 1568 | |
| 1569 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1570 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
| 1571 | BT_LOGD_STR("Creating default dynamic array field class object."); |
| 1572 | array_fc = g_new0(struct bt_field_class_array_dynamic, 1); |
| 1573 | if (!array_fc) { |
| 1574 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1575 | "Failed to allocate one dynamic array field class."); |
| 1576 | goto error; |
| 1577 | } |
| 1578 | |
| 1579 | init_array_field_class((void *) array_fc, |
| 1580 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
| 1581 | destroy_dynamic_array_field_class, element_fc); |
| 1582 | |
| 1583 | if (length_fc) { |
| 1584 | BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, |
| 1585 | "Length field class"); |
| 1586 | array_fc->length_fc = length_fc; |
| 1587 | bt_object_get_no_null_check(array_fc->length_fc); |
| 1588 | bt_field_class_freeze(length_fc); |
| 1589 | } |
| 1590 | |
| 1591 | BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc); |
| 1592 | goto end; |
| 1593 | |
| 1594 | error: |
| 1595 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
| 1596 | |
| 1597 | end: |
| 1598 | return (void *) array_fc; |
| 1599 | } |
| 1600 | |
| 1601 | const struct bt_field_path * |
| 1602 | bt_field_class_array_dynamic_borrow_length_field_path_const( |
| 1603 | const struct bt_field_class *fc) |
| 1604 | { |
| 1605 | const struct bt_field_class_array_dynamic *seq_fc = (const void *) fc; |
| 1606 | |
| 1607 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
| 1608 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
| 1609 | "Field class"); |
| 1610 | return seq_fc->length_field_path; |
| 1611 | } |
| 1612 | |
| 1613 | static |
| 1614 | void destroy_string_field_class(struct bt_object *obj) |
| 1615 | { |
| 1616 | BT_ASSERT(obj); |
| 1617 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
| 1618 | g_free(obj); |
| 1619 | } |
| 1620 | |
| 1621 | struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class) |
| 1622 | { |
| 1623 | struct bt_field_class_string *string_fc = NULL; |
| 1624 | |
| 1625 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1626 | BT_LOGD_STR("Creating default string field class object."); |
| 1627 | string_fc = g_new0(struct bt_field_class_string, 1); |
| 1628 | if (!string_fc) { |
| 1629 | BT_LIB_LOGE_APPEND_CAUSE( |
| 1630 | "Failed to allocate one string field class."); |
| 1631 | goto error; |
| 1632 | } |
| 1633 | |
| 1634 | init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING, |
| 1635 | destroy_string_field_class); |
| 1636 | BT_LIB_LOGD("Created string field class object: %!+F", string_fc); |
| 1637 | goto end; |
| 1638 | |
| 1639 | error: |
| 1640 | BT_OBJECT_PUT_REF_AND_RESET(string_fc); |
| 1641 | |
| 1642 | end: |
| 1643 | return (void *) string_fc; |
| 1644 | } |
| 1645 | |
| 1646 | BT_HIDDEN |
| 1647 | void _bt_field_class_freeze(const struct bt_field_class *c_fc) |
| 1648 | { |
| 1649 | struct bt_field_class *fc = (void *) c_fc; |
| 1650 | |
| 1651 | /* |
| 1652 | * Element/member/option field classes are frozen when added to |
| 1653 | * their owner. |
| 1654 | */ |
| 1655 | BT_ASSERT(fc); |
| 1656 | fc->frozen = true; |
| 1657 | |
| 1658 | switch (fc->type) { |
| 1659 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 1660 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 1661 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 1662 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 1663 | { |
| 1664 | struct bt_field_class_named_field_class_container *container_fc = |
| 1665 | (void *) fc; |
| 1666 | uint64_t i; |
| 1667 | |
| 1668 | for (i = 0; i < container_fc->named_fcs->len; i++) { |
| 1669 | bt_named_field_class_freeze( |
| 1670 | container_fc->named_fcs->pdata[i]); |
| 1671 | } |
| 1672 | |
| 1673 | break; |
| 1674 | } |
| 1675 | default: |
| 1676 | break; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | BT_HIDDEN |
| 1681 | void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc) |
| 1682 | { |
| 1683 | BT_ASSERT(named_fc); |
| 1684 | ((struct bt_named_field_class *) named_fc)->frozen = true; |
| 1685 | bt_field_class_freeze(named_fc->fc); |
| 1686 | } |
| 1687 | |
| 1688 | BT_HIDDEN |
| 1689 | void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc) |
| 1690 | { |
| 1691 | struct bt_field_class *fc = (void *) c_fc; |
| 1692 | |
| 1693 | BT_ASSERT(fc); |
| 1694 | BT_ASSERT_PRE(!fc->part_of_trace_class, |
| 1695 | "Field class is already part of a trace: %!+F", fc); |
| 1696 | fc->part_of_trace_class = true; |
| 1697 | |
| 1698 | switch (fc->type) { |
| 1699 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 1700 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 1701 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 1702 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 1703 | { |
| 1704 | struct bt_field_class_named_field_class_container *container_fc = |
| 1705 | (void *) fc; |
| 1706 | uint64_t i; |
| 1707 | |
| 1708 | for (i = 0; i < container_fc->named_fcs->len; i++) { |
| 1709 | struct bt_named_field_class *named_fc = |
| 1710 | container_fc->named_fcs->pdata[i]; |
| 1711 | |
| 1712 | bt_field_class_make_part_of_trace_class(named_fc->fc); |
| 1713 | } |
| 1714 | |
| 1715 | break; |
| 1716 | } |
| 1717 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 1718 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 1719 | { |
| 1720 | struct bt_field_class_array *array_fc = (void *) fc; |
| 1721 | |
| 1722 | bt_field_class_make_part_of_trace_class(array_fc->element_fc); |
| 1723 | break; |
| 1724 | } |
| 1725 | default: |
| 1726 | break; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | void bt_field_class_get_ref(const struct bt_field_class *field_class) |
| 1731 | { |
| 1732 | bt_object_get_ref(field_class); |
| 1733 | } |
| 1734 | |
| 1735 | void bt_field_class_put_ref(const struct bt_field_class *field_class) |
| 1736 | { |
| 1737 | bt_object_put_ref(field_class); |
| 1738 | } |