| 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 "FIELD-CLASSES" |
| 25 | #include "lib/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 | |
| 48 | enum bt_field_class_type bt_field_class_get_type( |
| 49 | const struct bt_field_class *fc) |
| 50 | { |
| 51 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 52 | return fc->type; |
| 53 | } |
| 54 | |
| 55 | static |
| 56 | void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, |
| 57 | bt_object_release_func release_func) |
| 58 | { |
| 59 | BT_ASSERT(fc); |
| 60 | BT_ASSERT(bt_field_class_has_known_type(fc)); |
| 61 | BT_ASSERT(release_func); |
| 62 | bt_object_init_shared(&fc->base, release_func); |
| 63 | fc->type = type; |
| 64 | } |
| 65 | |
| 66 | static |
| 67 | void init_integer_field_class(struct bt_field_class_integer *fc, |
| 68 | enum bt_field_class_type type, |
| 69 | bt_object_release_func release_func) |
| 70 | { |
| 71 | init_field_class((void *) fc, type, release_func); |
| 72 | fc->range = 64; |
| 73 | fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
| 74 | } |
| 75 | |
| 76 | static |
| 77 | void destroy_integer_field_class(struct bt_object *obj) |
| 78 | { |
| 79 | BT_ASSERT(obj); |
| 80 | BT_LIB_LOGD("Destroying integer field class object: %!+F", obj); |
| 81 | g_free(obj); |
| 82 | } |
| 83 | |
| 84 | static inline |
| 85 | struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class, |
| 86 | enum bt_field_class_type type) |
| 87 | { |
| 88 | struct bt_field_class_integer *int_fc = NULL; |
| 89 | |
| 90 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 91 | BT_LOGD("Creating default integer field class object: type=%s", |
| 92 | bt_common_field_class_type_string(type)); |
| 93 | int_fc = g_new0(struct bt_field_class_integer, 1); |
| 94 | if (!int_fc) { |
| 95 | BT_LOGE_STR("Failed to allocate one integer field class."); |
| 96 | goto error; |
| 97 | } |
| 98 | |
| 99 | init_integer_field_class(int_fc, type, destroy_integer_field_class); |
| 100 | BT_LIB_LOGD("Created integer field class object: %!+F", int_fc); |
| 101 | goto end; |
| 102 | |
| 103 | error: |
| 104 | BT_OBJECT_PUT_REF_AND_RESET(int_fc); |
| 105 | |
| 106 | end: |
| 107 | return (void *) int_fc; |
| 108 | } |
| 109 | |
| 110 | struct bt_field_class *bt_field_class_unsigned_integer_create( |
| 111 | bt_trace_class *trace_class) |
| 112 | { |
| 113 | return create_integer_field_class(trace_class, |
| 114 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER); |
| 115 | } |
| 116 | |
| 117 | struct bt_field_class *bt_field_class_signed_integer_create( |
| 118 | bt_trace_class *trace_class) |
| 119 | { |
| 120 | return create_integer_field_class(trace_class, |
| 121 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); |
| 122 | } |
| 123 | |
| 124 | uint64_t bt_field_class_integer_get_field_value_range( |
| 125 | const struct bt_field_class *fc) |
| 126 | { |
| 127 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
| 128 | |
| 129 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 130 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); |
| 131 | return int_fc->range; |
| 132 | } |
| 133 | |
| 134 | BT_ASSERT_PRE_FUNC |
| 135 | static |
| 136 | bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc, |
| 137 | uint64_t size) |
| 138 | { |
| 139 | // TODO |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | void bt_field_class_integer_set_field_value_range( |
| 144 | struct bt_field_class *fc, uint64_t size) |
| 145 | { |
| 146 | struct bt_field_class_integer *int_fc = (void *) fc; |
| 147 | |
| 148 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 149 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); |
| 150 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); |
| 151 | BT_ASSERT_PRE(size <= 64, |
| 152 | "Unsupported size for integer field class's field value range " |
| 153 | "(maximum is 64): size=%" PRIu64, size); |
| 154 | BT_ASSERT_PRE( |
| 155 | int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || |
| 156 | int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || |
| 157 | size_is_valid_for_enumeration_field_class(fc, size), |
| 158 | "Invalid field value range for enumeration field class: " |
| 159 | "at least one of the current mapping ranges contains values " |
| 160 | "which are outside this range: %!+F, size=%" PRIu64, fc, size); |
| 161 | int_fc->range = size; |
| 162 | BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc); |
| 163 | } |
| 164 | |
| 165 | enum bt_field_class_integer_preferred_display_base |
| 166 | bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc) |
| 167 | { |
| 168 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
| 169 | |
| 170 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 171 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); |
| 172 | return int_fc->base; |
| 173 | } |
| 174 | |
| 175 | void bt_field_class_integer_set_preferred_display_base( |
| 176 | struct bt_field_class *fc, |
| 177 | enum bt_field_class_integer_preferred_display_base base) |
| 178 | { |
| 179 | struct bt_field_class_integer *int_fc = (void *) fc; |
| 180 | |
| 181 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 182 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); |
| 183 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); |
| 184 | int_fc->base = base; |
| 185 | BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc); |
| 186 | } |
| 187 | |
| 188 | static |
| 189 | void finalize_enumeration_field_class_mapping( |
| 190 | struct bt_field_class_enumeration_mapping *mapping) |
| 191 | { |
| 192 | BT_ASSERT(mapping); |
| 193 | |
| 194 | if (mapping->label) { |
| 195 | g_string_free(mapping->label, TRUE); |
| 196 | } |
| 197 | |
| 198 | if (mapping->ranges) { |
| 199 | g_array_free(mapping->ranges, TRUE); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static |
| 204 | void destroy_enumeration_field_class(struct bt_object *obj) |
| 205 | { |
| 206 | struct bt_field_class_enumeration *fc = (void *) obj; |
| 207 | |
| 208 | BT_ASSERT(fc); |
| 209 | BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc); |
| 210 | |
| 211 | if (fc->mappings) { |
| 212 | uint64_t i; |
| 213 | |
| 214 | for (i = 0; i < fc->mappings->len; i++) { |
| 215 | finalize_enumeration_field_class_mapping( |
| 216 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i)); |
| 217 | } |
| 218 | |
| 219 | g_array_free(fc->mappings, TRUE); |
| 220 | fc->mappings = NULL; |
| 221 | } |
| 222 | |
| 223 | if (fc->label_buf) { |
| 224 | g_ptr_array_free(fc->label_buf, TRUE); |
| 225 | fc->label_buf = NULL; |
| 226 | } |
| 227 | |
| 228 | g_free(fc); |
| 229 | } |
| 230 | |
| 231 | static |
| 232 | struct bt_field_class *create_enumeration_field_class( |
| 233 | bt_trace_class *trace_class, enum bt_field_class_type type) |
| 234 | { |
| 235 | struct bt_field_class_enumeration *enum_fc = NULL; |
| 236 | |
| 237 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 238 | BT_LOGD("Creating default enumeration field class object: type=%s", |
| 239 | bt_common_field_class_type_string(type)); |
| 240 | enum_fc = g_new0(struct bt_field_class_enumeration, 1); |
| 241 | if (!enum_fc) { |
| 242 | BT_LOGE_STR("Failed to allocate one enumeration field class."); |
| 243 | goto error; |
| 244 | } |
| 245 | |
| 246 | init_integer_field_class((void *) enum_fc, type, |
| 247 | destroy_enumeration_field_class); |
| 248 | enum_fc->mappings = g_array_new(FALSE, TRUE, |
| 249 | sizeof(struct bt_field_class_enumeration_mapping)); |
| 250 | if (!enum_fc->mappings) { |
| 251 | BT_LOGE_STR("Failed to allocate a GArray."); |
| 252 | goto error; |
| 253 | } |
| 254 | |
| 255 | enum_fc->label_buf = g_ptr_array_new(); |
| 256 | if (!enum_fc->label_buf) { |
| 257 | BT_LOGE_STR("Failed to allocate a GArray."); |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc); |
| 262 | goto end; |
| 263 | |
| 264 | error: |
| 265 | BT_OBJECT_PUT_REF_AND_RESET(enum_fc); |
| 266 | |
| 267 | end: |
| 268 | return (void *) enum_fc; |
| 269 | } |
| 270 | |
| 271 | struct bt_field_class *bt_field_class_unsigned_enumeration_create( |
| 272 | bt_trace_class *trace_class) |
| 273 | { |
| 274 | return create_enumeration_field_class(trace_class, |
| 275 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
| 276 | } |
| 277 | |
| 278 | struct bt_field_class *bt_field_class_signed_enumeration_create( |
| 279 | bt_trace_class *trace_class) |
| 280 | { |
| 281 | return create_enumeration_field_class(trace_class, |
| 282 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
| 283 | } |
| 284 | |
| 285 | uint64_t bt_field_class_enumeration_get_mapping_count( |
| 286 | const struct bt_field_class *fc) |
| 287 | { |
| 288 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 289 | |
| 290 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 291 | BT_ASSERT_PRE_FC_IS_ENUM(fc, "Field class"); |
| 292 | return (uint64_t) enum_fc->mappings->len; |
| 293 | } |
| 294 | |
| 295 | const struct bt_field_class_unsigned_enumeration_mapping * |
| 296 | bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const( |
| 297 | const struct bt_field_class *fc, uint64_t index) |
| 298 | { |
| 299 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 300 | |
| 301 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 302 | BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len); |
| 303 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
| 304 | "Field class"); |
| 305 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
| 306 | } |
| 307 | |
| 308 | const struct bt_field_class_signed_enumeration_mapping * |
| 309 | bt_field_class_signed_enumeration_borrow_mapping_by_index_const( |
| 310 | const struct bt_field_class *fc, uint64_t index) |
| 311 | { |
| 312 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 313 | |
| 314 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 315 | BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len); |
| 316 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 317 | "Field class"); |
| 318 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
| 319 | } |
| 320 | |
| 321 | const char *bt_field_class_enumeration_mapping_get_label( |
| 322 | const struct bt_field_class_enumeration_mapping *mapping) |
| 323 | { |
| 324 | BT_ASSERT_PRE_NON_NULL(mapping, "Enumeration field class mapping"); |
| 325 | return mapping->label->str; |
| 326 | } |
| 327 | |
| 328 | uint64_t bt_field_class_enumeration_mapping_get_range_count( |
| 329 | const struct bt_field_class_enumeration_mapping *mapping) |
| 330 | { |
| 331 | BT_ASSERT_PRE_NON_NULL(mapping, "Enumeration field class mapping"); |
| 332 | return (uint64_t) mapping->ranges->len; |
| 333 | } |
| 334 | |
| 335 | static inline |
| 336 | void get_enumeration_field_class_mapping_range_at_index( |
| 337 | const struct bt_field_class_enumeration_mapping *mapping, |
| 338 | uint64_t index, uint64_t *lower, uint64_t *upper) |
| 339 | { |
| 340 | const struct bt_field_class_enumeration_mapping_range *range; |
| 341 | |
| 342 | BT_ASSERT_PRE_NON_NULL(mapping, "Ranges"); |
| 343 | BT_ASSERT_PRE_NON_NULL(lower, "Range's lower (output)"); |
| 344 | BT_ASSERT_PRE_NON_NULL(upper, "Range's upper (output)"); |
| 345 | BT_ASSERT_PRE_VALID_INDEX(index, mapping->ranges->len); |
| 346 | range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, index); |
| 347 | *lower = range->lower.u; |
| 348 | *upper = range->upper.u; |
| 349 | } |
| 350 | |
| 351 | void bt_field_class_unsigned_enumeration_mapping_get_range_by_index( |
| 352 | const struct bt_field_class_unsigned_enumeration_mapping *ranges, |
| 353 | uint64_t index, uint64_t *lower, uint64_t *upper) |
| 354 | { |
| 355 | get_enumeration_field_class_mapping_range_at_index( |
| 356 | (const void *) ranges, index, lower, upper); |
| 357 | } |
| 358 | |
| 359 | void bt_field_class_signed_enumeration_mapping_get_range_by_index( |
| 360 | const struct bt_field_class_signed_enumeration_mapping *ranges, |
| 361 | uint64_t index, int64_t *lower, int64_t *upper) |
| 362 | { |
| 363 | get_enumeration_field_class_mapping_range_at_index( |
| 364 | (const void *) ranges, index, |
| 365 | (uint64_t *) lower, (uint64_t *) upper); |
| 366 | } |
| 367 | |
| 368 | enum bt_field_class_status |
| 369 | bt_field_class_unsigned_enumeration_get_mapping_labels_by_value( |
| 370 | const struct bt_field_class *fc, uint64_t value, |
| 371 | bt_field_class_enumeration_mapping_label_array *label_array, |
| 372 | uint64_t *count) |
| 373 | { |
| 374 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 375 | uint64_t i; |
| 376 | |
| 377 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 378 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); |
| 379 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); |
| 380 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
| 381 | "Field class"); |
| 382 | g_ptr_array_set_size(enum_fc->label_buf, 0); |
| 383 | |
| 384 | for (i = 0; i < enum_fc->mappings->len; i++) { |
| 385 | uint64_t j; |
| 386 | const struct bt_field_class_enumeration_mapping *mapping = |
| 387 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
| 388 | |
| 389 | for (j = 0; j < mapping->ranges->len; j++) { |
| 390 | const struct bt_field_class_enumeration_mapping_range *range = |
| 391 | BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX( |
| 392 | mapping, j); |
| 393 | |
| 394 | if (value >= range->lower.u && |
| 395 | value <= range->upper.u) { |
| 396 | g_ptr_array_add(enum_fc->label_buf, |
| 397 | mapping->label->str); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | *label_array = (void *) enum_fc->label_buf->pdata; |
| 404 | *count = (uint64_t) enum_fc->label_buf->len; |
| 405 | return BT_FIELD_CLASS_STATUS_OK; |
| 406 | } |
| 407 | |
| 408 | enum bt_field_class_status |
| 409 | bt_field_class_signed_enumeration_get_mapping_labels_by_value( |
| 410 | const struct bt_field_class *fc, int64_t value, |
| 411 | bt_field_class_enumeration_mapping_label_array *label_array, |
| 412 | uint64_t *count) |
| 413 | { |
| 414 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
| 415 | uint64_t i; |
| 416 | |
| 417 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 418 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); |
| 419 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); |
| 420 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 421 | "Field class"); |
| 422 | g_ptr_array_set_size(enum_fc->label_buf, 0); |
| 423 | |
| 424 | for (i = 0; i < enum_fc->mappings->len; i++) { |
| 425 | uint64_t j; |
| 426 | const struct bt_field_class_enumeration_mapping *mapping = |
| 427 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
| 428 | |
| 429 | for (j = 0; j < mapping->ranges->len; j++) { |
| 430 | const struct bt_field_class_enumeration_mapping_range *range = |
| 431 | BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX( |
| 432 | mapping, j); |
| 433 | |
| 434 | if (value >= range->lower.i && |
| 435 | value <= range->upper.i) { |
| 436 | g_ptr_array_add(enum_fc->label_buf, |
| 437 | mapping->label->str); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | *label_array = (void *) enum_fc->label_buf->pdata; |
| 444 | *count = (uint64_t) enum_fc->label_buf->len; |
| 445 | return BT_FIELD_CLASS_STATUS_OK; |
| 446 | } |
| 447 | |
| 448 | static inline |
| 449 | enum bt_field_class_status add_mapping_to_enumeration_field_class( |
| 450 | struct bt_field_class *fc, |
| 451 | const char *label, uint64_t lower, uint64_t upper) |
| 452 | { |
| 453 | int ret = BT_FIELD_CLASS_STATUS_OK; |
| 454 | uint64_t i; |
| 455 | struct bt_field_class_enumeration *enum_fc = (void *) fc; |
| 456 | struct bt_field_class_enumeration_mapping *mapping = NULL; |
| 457 | struct bt_field_class_enumeration_mapping_range *range; |
| 458 | |
| 459 | BT_ASSERT(fc); |
| 460 | BT_ASSERT_PRE_NON_NULL(label, "Label"); |
| 461 | |
| 462 | /* Find existing mapping identified by this label */ |
| 463 | for (i = 0; i < enum_fc->mappings->len; i++) { |
| 464 | struct bt_field_class_enumeration_mapping *mapping_candidate = |
| 465 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
| 466 | |
| 467 | if (strcmp(mapping_candidate->label->str, label) == 0) { |
| 468 | mapping = mapping_candidate; |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (!mapping) { |
| 474 | /* Create new mapping for this label */ |
| 475 | g_array_set_size(enum_fc->mappings, enum_fc->mappings->len + 1); |
| 476 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, |
| 477 | enum_fc->mappings->len - 1); |
| 478 | mapping->ranges = g_array_new(FALSE, TRUE, |
| 479 | sizeof(struct bt_field_class_enumeration_mapping_range)); |
| 480 | if (!mapping->ranges) { |
| 481 | finalize_enumeration_field_class_mapping(mapping); |
| 482 | g_array_set_size(enum_fc->mappings, |
| 483 | enum_fc->mappings->len - 1); |
| 484 | ret = BT_FIELD_CLASS_STATUS_NOMEM; |
| 485 | goto end; |
| 486 | } |
| 487 | |
| 488 | mapping->label = g_string_new(label); |
| 489 | if (!mapping->label) { |
| 490 | finalize_enumeration_field_class_mapping(mapping); |
| 491 | g_array_set_size(enum_fc->mappings, |
| 492 | enum_fc->mappings->len - 1); |
| 493 | ret = BT_FIELD_CLASS_STATUS_NOMEM; |
| 494 | goto end; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* Add range */ |
| 499 | BT_ASSERT(mapping); |
| 500 | g_array_set_size(mapping->ranges, mapping->ranges->len + 1); |
| 501 | range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, |
| 502 | mapping->ranges->len - 1); |
| 503 | range->lower.u = lower; |
| 504 | range->upper.u = upper; |
| 505 | BT_LIB_LOGD("Added mapping to enumeration field class: " |
| 506 | "%![fc-]+F, label=\"%s\", lower-unsigned=%" PRIu64 ", " |
| 507 | "upper-unsigned=%" PRIu64, fc, label, lower, upper); |
| 508 | |
| 509 | end: |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | enum bt_field_class_status bt_field_class_unsigned_enumeration_map_range( |
| 514 | struct bt_field_class *fc, const char *label, |
| 515 | uint64_t range_lower, uint64_t range_upper) |
| 516 | { |
| 517 | struct bt_field_class_enumeration *enum_fc = (void *) fc; |
| 518 | |
| 519 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 520 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
| 521 | "Field class"); |
| 522 | BT_ASSERT_PRE(range_lower <= range_upper, |
| 523 | "Range's upper bound is less than lower bound: " |
| 524 | "upper=%" PRIu64 ", lower=%" PRIu64, |
| 525 | range_lower, range_upper); |
| 526 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range, |
| 527 | range_lower), |
| 528 | "Range's lower bound is outside the enumeration field class's value range: " |
| 529 | "%![fc-]+F, lower=%" PRIu64, fc, range_lower); |
| 530 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range, |
| 531 | range_upper), |
| 532 | "Range's upper bound is outside the enumeration field class's value range: " |
| 533 | "%![fc-]+F, upper=%" PRIu64, fc, range_upper); |
| 534 | return add_mapping_to_enumeration_field_class(fc, label, range_lower, |
| 535 | range_upper); |
| 536 | } |
| 537 | |
| 538 | enum bt_field_class_status bt_field_class_signed_enumeration_map_range( |
| 539 | struct bt_field_class *fc, const char *label, |
| 540 | int64_t range_lower, int64_t range_upper) |
| 541 | { |
| 542 | struct bt_field_class_enumeration *enum_fc = (void *) fc; |
| 543 | |
| 544 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 545 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
| 546 | "Field class"); |
| 547 | BT_ASSERT_PRE(range_lower <= range_upper, |
| 548 | "Range's upper bound is less than lower bound: " |
| 549 | "upper=%" PRId64 ", lower=%" PRId64, |
| 550 | range_lower, range_upper); |
| 551 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range, |
| 552 | range_lower), |
| 553 | "Range's lower bound is outside the enumeration field class's value range: " |
| 554 | "%![fc-]+F, lower=%" PRId64, fc, range_lower); |
| 555 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range, |
| 556 | range_upper), |
| 557 | "Range's upper bound is outside the enumeration field class's value range: " |
| 558 | "%![fc-]+F, upper=%" PRId64, fc, range_upper); |
| 559 | return add_mapping_to_enumeration_field_class(fc, label, range_lower, |
| 560 | range_upper); |
| 561 | } |
| 562 | |
| 563 | static |
| 564 | void destroy_real_field_class(struct bt_object *obj) |
| 565 | { |
| 566 | BT_ASSERT(obj); |
| 567 | BT_LIB_LOGD("Destroying real field class object: %!+F", obj); |
| 568 | g_free(obj); |
| 569 | } |
| 570 | |
| 571 | struct bt_field_class *bt_field_class_real_create(bt_trace_class *trace_class) |
| 572 | { |
| 573 | struct bt_field_class_real *real_fc = NULL; |
| 574 | |
| 575 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 576 | BT_LOGD_STR("Creating default real field class object."); |
| 577 | real_fc = g_new0(struct bt_field_class_real, 1); |
| 578 | if (!real_fc) { |
| 579 | BT_LOGE_STR("Failed to allocate one real field class."); |
| 580 | goto error; |
| 581 | } |
| 582 | |
| 583 | init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL, |
| 584 | destroy_real_field_class); |
| 585 | BT_LIB_LOGD("Created real field class object: %!+F", real_fc); |
| 586 | goto end; |
| 587 | |
| 588 | error: |
| 589 | BT_OBJECT_PUT_REF_AND_RESET(real_fc); |
| 590 | |
| 591 | end: |
| 592 | return (void *) real_fc; |
| 593 | } |
| 594 | |
| 595 | bt_bool bt_field_class_real_is_single_precision(const struct bt_field_class *fc) |
| 596 | { |
| 597 | const struct bt_field_class_real *real_fc = (const void *) fc; |
| 598 | |
| 599 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 600 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
| 601 | return real_fc->is_single_precision; |
| 602 | } |
| 603 | |
| 604 | void bt_field_class_real_set_is_single_precision(struct bt_field_class *fc, |
| 605 | bt_bool is_single_precision) |
| 606 | { |
| 607 | struct bt_field_class_real *real_fc = (void *) fc; |
| 608 | |
| 609 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 610 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
| 611 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); |
| 612 | real_fc->is_single_precision = (bool) is_single_precision; |
| 613 | BT_LIB_LOGD("Set real field class's \"is single precision\" property: " |
| 614 | "%!+F", fc); |
| 615 | } |
| 616 | |
| 617 | static |
| 618 | int init_named_field_classes_container( |
| 619 | struct bt_field_class_named_field_class_container *fc, |
| 620 | enum bt_field_class_type type, |
| 621 | bt_object_release_func release_func) |
| 622 | { |
| 623 | int ret = 0; |
| 624 | |
| 625 | init_field_class((void *) fc, type, release_func); |
| 626 | fc->named_fcs = g_array_new(FALSE, TRUE, |
| 627 | sizeof(struct bt_named_field_class)); |
| 628 | if (!fc->named_fcs) { |
| 629 | BT_LOGE_STR("Failed to allocate a GArray."); |
| 630 | ret = -1; |
| 631 | goto end; |
| 632 | } |
| 633 | |
| 634 | fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal); |
| 635 | if (!fc->name_to_index) { |
| 636 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 637 | ret = -1; |
| 638 | goto end; |
| 639 | } |
| 640 | |
| 641 | end: |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | static |
| 646 | void finalize_named_field_class(struct bt_named_field_class *named_fc) |
| 647 | { |
| 648 | BT_ASSERT(named_fc); |
| 649 | BT_LIB_LOGD("Finalizing named field class: " |
| 650 | "addr=%p, name=\"%s\", %![fc-]+F", |
| 651 | named_fc, named_fc->name ? named_fc->name->str : NULL, |
| 652 | named_fc->fc); |
| 653 | |
| 654 | if (named_fc->name) { |
| 655 | g_string_free(named_fc->name, TRUE); |
| 656 | } |
| 657 | |
| 658 | BT_LOGD_STR("Putting named field class's field class."); |
| 659 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc); |
| 660 | } |
| 661 | |
| 662 | static |
| 663 | void finalize_named_field_classes_container( |
| 664 | struct bt_field_class_named_field_class_container *fc) |
| 665 | { |
| 666 | uint64_t i; |
| 667 | |
| 668 | BT_ASSERT(fc); |
| 669 | |
| 670 | if (fc->named_fcs) { |
| 671 | for (i = 0; i < fc->named_fcs->len; i++) { |
| 672 | finalize_named_field_class( |
| 673 | &g_array_index(fc->named_fcs, |
| 674 | struct bt_named_field_class, i)); |
| 675 | } |
| 676 | |
| 677 | g_array_free(fc->named_fcs, TRUE); |
| 678 | } |
| 679 | |
| 680 | if (fc->name_to_index) { |
| 681 | g_hash_table_destroy(fc->name_to_index); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | static |
| 686 | void destroy_structure_field_class(struct bt_object *obj) |
| 687 | { |
| 688 | BT_ASSERT(obj); |
| 689 | BT_LIB_LOGD("Destroying structure field class object: %!+F", obj); |
| 690 | finalize_named_field_classes_container((void *) obj); |
| 691 | g_free(obj); |
| 692 | } |
| 693 | |
| 694 | struct bt_field_class *bt_field_class_structure_create( |
| 695 | bt_trace_class *trace_class) |
| 696 | { |
| 697 | int ret; |
| 698 | struct bt_field_class_structure *struct_fc = NULL; |
| 699 | |
| 700 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 701 | BT_LOGD_STR("Creating default structure field class object."); |
| 702 | struct_fc = g_new0(struct bt_field_class_structure, 1); |
| 703 | if (!struct_fc) { |
| 704 | BT_LOGE_STR("Failed to allocate one structure field class."); |
| 705 | goto error; |
| 706 | } |
| 707 | |
| 708 | ret = init_named_field_classes_container((void *) struct_fc, |
| 709 | BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class); |
| 710 | if (ret) { |
| 711 | goto error; |
| 712 | } |
| 713 | |
| 714 | BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc); |
| 715 | goto end; |
| 716 | |
| 717 | error: |
| 718 | BT_OBJECT_PUT_REF_AND_RESET(struct_fc); |
| 719 | |
| 720 | end: |
| 721 | return (void *) struct_fc; |
| 722 | } |
| 723 | |
| 724 | static |
| 725 | enum bt_field_class_status append_named_field_class_to_container_field_class( |
| 726 | struct bt_field_class_named_field_class_container *container_fc, |
| 727 | const char *name, struct bt_field_class *fc) |
| 728 | { |
| 729 | int ret = BT_FIELD_CLASS_STATUS_OK; |
| 730 | struct bt_named_field_class *named_fc; |
| 731 | GString *name_str; |
| 732 | |
| 733 | BT_ASSERT(container_fc); |
| 734 | BT_ASSERT_PRE_FC_HOT(container_fc, "Field class"); |
| 735 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 736 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 737 | BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index, |
| 738 | name), |
| 739 | "Duplicate member/option name in structure/variant field class: " |
| 740 | "%![container-fc-]+F, name=\"%s\"", container_fc, name); |
| 741 | name_str = g_string_new(name); |
| 742 | if (!name_str) { |
| 743 | BT_LOGE_STR("Failed to allocate a GString."); |
| 744 | ret = BT_FIELD_CLASS_STATUS_NOMEM; |
| 745 | goto end; |
| 746 | } |
| 747 | |
| 748 | g_array_set_size(container_fc->named_fcs, |
| 749 | container_fc->named_fcs->len + 1); |
| 750 | named_fc = &g_array_index(container_fc->named_fcs, |
| 751 | struct bt_named_field_class, container_fc->named_fcs->len - 1); |
| 752 | named_fc->name = name_str; |
| 753 | named_fc->fc = fc; |
| 754 | bt_object_get_no_null_check(fc); |
| 755 | g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, |
| 756 | GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); |
| 757 | |
| 758 | /* |
| 759 | * Freeze the field class, but not the named field class (the |
| 760 | * user can still modify it, if possible, until the container |
| 761 | * itself is frozen). |
| 762 | */ |
| 763 | bt_field_class_freeze(fc); |
| 764 | |
| 765 | end: |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | enum bt_field_class_status bt_field_class_structure_append_member( |
| 770 | struct bt_field_class *fc, const char *name, |
| 771 | struct bt_field_class *member_fc) |
| 772 | { |
| 773 | |
| 774 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 775 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 776 | "Field class"); |
| 777 | return append_named_field_class_to_container_field_class((void *) fc, |
| 778 | name, member_fc); |
| 779 | } |
| 780 | |
| 781 | uint64_t bt_field_class_structure_get_member_count( |
| 782 | const struct bt_field_class *fc) |
| 783 | { |
| 784 | struct bt_field_class_structure *struct_fc = (void *) fc; |
| 785 | |
| 786 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 787 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 788 | "Field class"); |
| 789 | return (uint64_t) struct_fc->common.named_fcs->len; |
| 790 | } |
| 791 | |
| 792 | static |
| 793 | struct bt_named_field_class * |
| 794 | borrow_named_field_class_from_container_field_class_at_index( |
| 795 | struct bt_field_class_named_field_class_container *fc, |
| 796 | uint64_t index) |
| 797 | { |
| 798 | BT_ASSERT(fc); |
| 799 | BT_ASSERT_PRE_VALID_INDEX(index, fc->named_fcs->len); |
| 800 | return BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, index); |
| 801 | } |
| 802 | |
| 803 | const struct bt_field_class_structure_member * |
| 804 | bt_field_class_structure_borrow_member_by_index_const( |
| 805 | const struct bt_field_class *fc, uint64_t index) |
| 806 | { |
| 807 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 808 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 809 | "Field class"); |
| 810 | return (const void *) |
| 811 | borrow_named_field_class_from_container_field_class_at_index( |
| 812 | (void *) fc, index); |
| 813 | } |
| 814 | |
| 815 | struct bt_field_class_structure_member * |
| 816 | bt_field_class_structure_borrow_member_by_index( |
| 817 | struct bt_field_class *fc, uint64_t index) |
| 818 | { |
| 819 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 820 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 821 | "Field class"); |
| 822 | return (void *) |
| 823 | borrow_named_field_class_from_container_field_class_at_index( |
| 824 | (void *) fc, index); |
| 825 | } |
| 826 | |
| 827 | static |
| 828 | struct bt_named_field_class * |
| 829 | borrow_named_field_class_from_container_field_class_by_name( |
| 830 | struct bt_field_class_named_field_class_container *fc, |
| 831 | const char *name) |
| 832 | { |
| 833 | struct bt_named_field_class *named_fc = NULL; |
| 834 | gpointer orig_key; |
| 835 | gpointer value; |
| 836 | |
| 837 | BT_ASSERT(fc); |
| 838 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 839 | if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key, |
| 840 | &value)) { |
| 841 | goto end; |
| 842 | } |
| 843 | |
| 844 | named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, |
| 845 | GPOINTER_TO_UINT(value)); |
| 846 | |
| 847 | end: |
| 848 | return named_fc; |
| 849 | } |
| 850 | |
| 851 | const struct bt_field_class_structure_member * |
| 852 | bt_field_class_structure_borrow_member_by_name_const( |
| 853 | const struct bt_field_class *fc, const char *name) |
| 854 | { |
| 855 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 856 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 857 | "Field class"); |
| 858 | return (const void *) |
| 859 | borrow_named_field_class_from_container_field_class_by_name( |
| 860 | (void *) fc, name); |
| 861 | } |
| 862 | |
| 863 | struct bt_field_class_structure_member * |
| 864 | bt_field_class_structure_borrow_member_by_name( |
| 865 | struct bt_field_class *fc, const char *name) |
| 866 | { |
| 867 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 868 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
| 869 | "Field class"); |
| 870 | return (void *) |
| 871 | borrow_named_field_class_from_container_field_class_by_name( |
| 872 | (void *) fc, name); |
| 873 | } |
| 874 | |
| 875 | const char *bt_field_class_structure_member_get_name( |
| 876 | const struct bt_field_class_structure_member *member) |
| 877 | { |
| 878 | const struct bt_named_field_class *named_fc = (const void *) member; |
| 879 | |
| 880 | BT_ASSERT_PRE_NON_NULL(member, "Structure field class member"); |
| 881 | return named_fc->name->str; |
| 882 | } |
| 883 | |
| 884 | const struct bt_field_class * |
| 885 | bt_field_class_structure_member_borrow_field_class_const( |
| 886 | const struct bt_field_class_structure_member *member) |
| 887 | { |
| 888 | const struct bt_named_field_class *named_fc = (const void *) member; |
| 889 | |
| 890 | BT_ASSERT_PRE_NON_NULL(member, "Structure field class member"); |
| 891 | return named_fc->fc; |
| 892 | } |
| 893 | |
| 894 | struct bt_field_class * |
| 895 | bt_field_class_structure_member_borrow_field_class( |
| 896 | struct bt_field_class_structure_member *member) |
| 897 | { |
| 898 | struct bt_named_field_class *named_fc = (void *) member; |
| 899 | |
| 900 | BT_ASSERT_PRE_NON_NULL(member, "Structure field class member"); |
| 901 | return named_fc->fc; |
| 902 | } |
| 903 | |
| 904 | static |
| 905 | void destroy_variant_field_class(struct bt_object *obj) |
| 906 | { |
| 907 | struct bt_field_class_variant *fc = (void *) obj; |
| 908 | |
| 909 | BT_ASSERT(fc); |
| 910 | BT_LIB_LOGD("Destroying variant field class object: %!+F", fc); |
| 911 | finalize_named_field_classes_container((void *) fc); |
| 912 | BT_LOGD_STR("Putting selector field path."); |
| 913 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path); |
| 914 | BT_LOGD_STR("Putting selector field class."); |
| 915 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc); |
| 916 | g_free(fc); |
| 917 | } |
| 918 | |
| 919 | struct bt_field_class *bt_field_class_variant_create( |
| 920 | bt_trace_class *trace_class) |
| 921 | { |
| 922 | int ret; |
| 923 | struct bt_field_class_variant *var_fc = NULL; |
| 924 | |
| 925 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 926 | BT_LOGD_STR("Creating default variant field class object."); |
| 927 | var_fc = g_new0(struct bt_field_class_variant, 1); |
| 928 | if (!var_fc) { |
| 929 | BT_LOGE_STR("Failed to allocate one variant field class."); |
| 930 | goto error; |
| 931 | } |
| 932 | |
| 933 | ret = init_named_field_classes_container((void *) var_fc, |
| 934 | BT_FIELD_CLASS_TYPE_VARIANT, destroy_variant_field_class); |
| 935 | if (ret) { |
| 936 | goto error; |
| 937 | } |
| 938 | |
| 939 | BT_LIB_LOGD("Created variant field class object: %!+F", var_fc); |
| 940 | goto end; |
| 941 | |
| 942 | error: |
| 943 | BT_OBJECT_PUT_REF_AND_RESET(var_fc); |
| 944 | |
| 945 | end: |
| 946 | return (void *) var_fc; |
| 947 | } |
| 948 | |
| 949 | enum bt_field_class_status bt_field_class_variant_set_selector_field_class( |
| 950 | struct bt_field_class *fc, |
| 951 | struct bt_field_class *selector_fc) |
| 952 | { |
| 953 | struct bt_field_class_variant *var_fc = (void *) fc; |
| 954 | |
| 955 | BT_ASSERT_PRE_NON_NULL(fc, "Variant field class"); |
| 956 | BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class"); |
| 957 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 958 | BT_ASSERT_PRE_FC_IS_ENUM(selector_fc, "Selector field class"); |
| 959 | BT_ASSERT_PRE_FC_HOT(fc, "Variant field class"); |
| 960 | var_fc->selector_fc = selector_fc; |
| 961 | bt_object_get_no_null_check(selector_fc); |
| 962 | bt_field_class_freeze(selector_fc); |
| 963 | return BT_FIELD_CLASS_STATUS_OK; |
| 964 | } |
| 965 | |
| 966 | enum bt_field_class_status bt_field_class_variant_append_option( |
| 967 | struct bt_field_class *fc, |
| 968 | const char *name, struct bt_field_class *option_fc) |
| 969 | { |
| 970 | |
| 971 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 972 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 973 | return append_named_field_class_to_container_field_class((void *) fc, |
| 974 | name, option_fc); |
| 975 | } |
| 976 | |
| 977 | const struct bt_field_class_variant_option * |
| 978 | bt_field_class_variant_borrow_option_by_name_const( |
| 979 | const struct bt_field_class *fc, const char *name) |
| 980 | { |
| 981 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 982 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 983 | return (const void *) |
| 984 | borrow_named_field_class_from_container_field_class_by_name( |
| 985 | (void *) fc, name); |
| 986 | } |
| 987 | |
| 988 | struct bt_field_class_variant_option * |
| 989 | bt_field_class_variant_borrow_option_by_name( |
| 990 | struct bt_field_class *fc, const char *name) |
| 991 | { |
| 992 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 993 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 994 | return (void *) |
| 995 | borrow_named_field_class_from_container_field_class_by_name( |
| 996 | (void *) fc, name); |
| 997 | } |
| 998 | |
| 999 | uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc) |
| 1000 | { |
| 1001 | const struct bt_field_class_variant *var_fc = (const void *) fc; |
| 1002 | |
| 1003 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1004 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 1005 | return (uint64_t) var_fc->common.named_fcs->len; |
| 1006 | } |
| 1007 | |
| 1008 | const struct bt_field_class_variant_option * |
| 1009 | bt_field_class_variant_borrow_option_by_index_const( |
| 1010 | const struct bt_field_class *fc, uint64_t index) |
| 1011 | { |
| 1012 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1013 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 1014 | return (const void *) |
| 1015 | borrow_named_field_class_from_container_field_class_at_index( |
| 1016 | (void *) fc, index); |
| 1017 | } |
| 1018 | |
| 1019 | struct bt_field_class_variant_option * |
| 1020 | bt_field_class_variant_borrow_option_by_index( |
| 1021 | struct bt_field_class *fc, uint64_t index) |
| 1022 | { |
| 1023 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1024 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
| 1025 | return (void *) |
| 1026 | borrow_named_field_class_from_container_field_class_at_index( |
| 1027 | (void *) fc, index); |
| 1028 | } |
| 1029 | |
| 1030 | const char *bt_field_class_variant_option_get_name( |
| 1031 | const struct bt_field_class_variant_option *option) |
| 1032 | { |
| 1033 | const struct bt_named_field_class *named_fc = (const void *) option; |
| 1034 | |
| 1035 | BT_ASSERT_PRE_NON_NULL(option, "Variant field class option"); |
| 1036 | return named_fc->name->str; |
| 1037 | } |
| 1038 | |
| 1039 | const struct bt_field_class * |
| 1040 | bt_field_class_variant_option_borrow_field_class_const( |
| 1041 | const struct bt_field_class_variant_option *option) |
| 1042 | { |
| 1043 | const struct bt_named_field_class *named_fc = (const void *) option; |
| 1044 | |
| 1045 | BT_ASSERT_PRE_NON_NULL(option, "Variant field class option"); |
| 1046 | return named_fc->fc; |
| 1047 | } |
| 1048 | |
| 1049 | struct bt_field_class * |
| 1050 | bt_field_class_variant_option_borrow_field_class( |
| 1051 | struct bt_field_class_variant_option *option) |
| 1052 | { |
| 1053 | struct bt_named_field_class *named_fc = (void *) option; |
| 1054 | |
| 1055 | BT_ASSERT_PRE_NON_NULL(option, "Variant field class option"); |
| 1056 | return named_fc->fc; |
| 1057 | } |
| 1058 | |
| 1059 | const struct bt_field_path * |
| 1060 | bt_field_class_variant_borrow_selector_field_path_const( |
| 1061 | const struct bt_field_class *fc) |
| 1062 | { |
| 1063 | const struct bt_field_class_variant *var_fc = (const void *) fc; |
| 1064 | |
| 1065 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1066 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, |
| 1067 | "Field class"); |
| 1068 | return var_fc->selector_field_path; |
| 1069 | } |
| 1070 | |
| 1071 | static |
| 1072 | void init_array_field_class(struct bt_field_class_array *fc, |
| 1073 | enum bt_field_class_type type, bt_object_release_func release_func, |
| 1074 | struct bt_field_class *element_fc) |
| 1075 | { |
| 1076 | BT_ASSERT(element_fc); |
| 1077 | init_field_class((void *) fc, type, release_func); |
| 1078 | fc->element_fc = element_fc; |
| 1079 | bt_object_get_no_null_check(element_fc); |
| 1080 | bt_field_class_freeze(element_fc); |
| 1081 | } |
| 1082 | |
| 1083 | static |
| 1084 | void finalize_array_field_class(struct bt_field_class_array *array_fc) |
| 1085 | { |
| 1086 | BT_ASSERT(array_fc); |
| 1087 | BT_LOGD_STR("Putting element field class."); |
| 1088 | BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc); |
| 1089 | } |
| 1090 | |
| 1091 | static |
| 1092 | void destroy_static_array_field_class(struct bt_object *obj) |
| 1093 | { |
| 1094 | BT_ASSERT(obj); |
| 1095 | BT_LIB_LOGD("Destroying static array field class object: %!+F", obj); |
| 1096 | finalize_array_field_class((void *) obj); |
| 1097 | g_free(obj); |
| 1098 | } |
| 1099 | |
| 1100 | struct bt_field_class * |
| 1101 | bt_field_class_static_array_create(bt_trace_class *trace_class, |
| 1102 | struct bt_field_class *element_fc, uint64_t length) |
| 1103 | { |
| 1104 | struct bt_field_class_static_array *array_fc = NULL; |
| 1105 | |
| 1106 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1107 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
| 1108 | BT_LOGD_STR("Creating default static array field class object."); |
| 1109 | array_fc = g_new0(struct bt_field_class_static_array, 1); |
| 1110 | if (!array_fc) { |
| 1111 | BT_LOGE_STR("Failed to allocate one static array field class."); |
| 1112 | goto error; |
| 1113 | } |
| 1114 | |
| 1115 | init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
| 1116 | destroy_static_array_field_class, element_fc); |
| 1117 | array_fc->length = length; |
| 1118 | BT_LIB_LOGD("Created static array field class object: %!+F", array_fc); |
| 1119 | goto end; |
| 1120 | |
| 1121 | error: |
| 1122 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
| 1123 | |
| 1124 | end: |
| 1125 | return (void *) array_fc; |
| 1126 | } |
| 1127 | |
| 1128 | const struct bt_field_class * |
| 1129 | bt_field_class_array_borrow_element_field_class_const( |
| 1130 | const struct bt_field_class *fc) |
| 1131 | { |
| 1132 | const struct bt_field_class_array *array_fc = (const void *) fc; |
| 1133 | |
| 1134 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1135 | BT_ASSERT_PRE_FC_IS_ARRAY(fc, "Field class"); |
| 1136 | return array_fc->element_fc; |
| 1137 | } |
| 1138 | |
| 1139 | struct bt_field_class * |
| 1140 | bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc) |
| 1141 | { |
| 1142 | struct bt_field_class_array *array_fc = (void *) fc; |
| 1143 | |
| 1144 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1145 | BT_ASSERT_PRE_FC_IS_ARRAY(fc, "Field class"); |
| 1146 | return array_fc->element_fc; |
| 1147 | } |
| 1148 | |
| 1149 | uint64_t bt_field_class_static_array_get_length(const struct bt_field_class *fc) |
| 1150 | { |
| 1151 | const struct bt_field_class_static_array *array_fc = (const void *) fc; |
| 1152 | |
| 1153 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1154 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
| 1155 | "Field class"); |
| 1156 | return (uint64_t) array_fc->length; |
| 1157 | } |
| 1158 | |
| 1159 | static |
| 1160 | void destroy_dynamic_array_field_class(struct bt_object *obj) |
| 1161 | { |
| 1162 | struct bt_field_class_dynamic_array *fc = (void *) obj; |
| 1163 | |
| 1164 | BT_ASSERT(fc); |
| 1165 | BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc); |
| 1166 | finalize_array_field_class((void *) fc); |
| 1167 | BT_LOGD_STR("Putting length field path."); |
| 1168 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path); |
| 1169 | BT_LOGD_STR("Putting length field class."); |
| 1170 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc); |
| 1171 | g_free(fc); |
| 1172 | } |
| 1173 | |
| 1174 | struct bt_field_class *bt_field_class_dynamic_array_create( |
| 1175 | bt_trace_class *trace_class, |
| 1176 | struct bt_field_class *element_fc) |
| 1177 | { |
| 1178 | struct bt_field_class_dynamic_array *array_fc = NULL; |
| 1179 | |
| 1180 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1181 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
| 1182 | BT_LOGD_STR("Creating default dynamic array field class object."); |
| 1183 | array_fc = g_new0(struct bt_field_class_dynamic_array, 1); |
| 1184 | if (!array_fc) { |
| 1185 | BT_LOGE_STR("Failed to allocate one dynamic array field class."); |
| 1186 | goto error; |
| 1187 | } |
| 1188 | |
| 1189 | init_array_field_class((void *) array_fc, |
| 1190 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
| 1191 | destroy_dynamic_array_field_class, element_fc); |
| 1192 | BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc); |
| 1193 | goto end; |
| 1194 | |
| 1195 | error: |
| 1196 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
| 1197 | |
| 1198 | end: |
| 1199 | return (void *) array_fc; |
| 1200 | } |
| 1201 | |
| 1202 | enum bt_field_class_status bt_field_class_dynamic_array_set_length_field_class( |
| 1203 | struct bt_field_class *fc, |
| 1204 | struct bt_field_class *length_fc) |
| 1205 | { |
| 1206 | struct bt_field_class_dynamic_array *array_fc = (void *) fc; |
| 1207 | |
| 1208 | BT_ASSERT_PRE_NON_NULL(fc, "Dynamic array field class"); |
| 1209 | BT_ASSERT_PRE_NON_NULL(length_fc, "Length field class"); |
| 1210 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
| 1211 | "Field class"); |
| 1212 | BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, "Length field class"); |
| 1213 | BT_ASSERT_PRE_FC_HOT(fc, "Dynamic array field class"); |
| 1214 | array_fc->length_fc = length_fc; |
| 1215 | bt_object_get_no_null_check(length_fc); |
| 1216 | bt_field_class_freeze(length_fc); |
| 1217 | return BT_FIELD_CLASS_STATUS_OK; |
| 1218 | } |
| 1219 | |
| 1220 | const struct bt_field_path * |
| 1221 | bt_field_class_dynamic_array_borrow_length_field_path_const( |
| 1222 | const struct bt_field_class *fc) |
| 1223 | { |
| 1224 | const struct bt_field_class_dynamic_array *seq_fc = (const void *) fc; |
| 1225 | |
| 1226 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
| 1227 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
| 1228 | "Field class"); |
| 1229 | return seq_fc->length_field_path; |
| 1230 | } |
| 1231 | |
| 1232 | static |
| 1233 | void destroy_string_field_class(struct bt_object *obj) |
| 1234 | { |
| 1235 | BT_ASSERT(obj); |
| 1236 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
| 1237 | g_free(obj); |
| 1238 | } |
| 1239 | |
| 1240 | struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class) |
| 1241 | { |
| 1242 | struct bt_field_class_string *string_fc = NULL; |
| 1243 | |
| 1244 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
| 1245 | BT_LOGD_STR("Creating default string field class object."); |
| 1246 | string_fc = g_new0(struct bt_field_class_string, 1); |
| 1247 | if (!string_fc) { |
| 1248 | BT_LOGE_STR("Failed to allocate one string field class."); |
| 1249 | goto error; |
| 1250 | } |
| 1251 | |
| 1252 | init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING, |
| 1253 | destroy_string_field_class); |
| 1254 | BT_LIB_LOGD("Created string field class object: %!+F", string_fc); |
| 1255 | goto end; |
| 1256 | |
| 1257 | error: |
| 1258 | BT_OBJECT_PUT_REF_AND_RESET(string_fc); |
| 1259 | |
| 1260 | end: |
| 1261 | return (void *) string_fc; |
| 1262 | } |
| 1263 | |
| 1264 | BT_HIDDEN |
| 1265 | void _bt_field_class_freeze(const struct bt_field_class *c_fc) |
| 1266 | { |
| 1267 | struct bt_field_class *fc = (void *) c_fc; |
| 1268 | |
| 1269 | /* |
| 1270 | * Element/member/option field classes are frozen when added to |
| 1271 | * their owner. |
| 1272 | */ |
| 1273 | BT_ASSERT(fc); |
| 1274 | fc->frozen = true; |
| 1275 | |
| 1276 | switch (fc->type) { |
| 1277 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 1278 | case BT_FIELD_CLASS_TYPE_VARIANT: |
| 1279 | { |
| 1280 | struct bt_field_class_named_field_class_container *container_fc = |
| 1281 | (void *) fc; |
| 1282 | uint64_t i; |
| 1283 | |
| 1284 | for (i = 0; i < container_fc->named_fcs->len; i++) { |
| 1285 | struct bt_named_field_class *named_fc = |
| 1286 | BT_FIELD_CLASS_NAMED_FC_AT_INDEX( |
| 1287 | container_fc, i); |
| 1288 | |
| 1289 | bt_named_field_class_freeze(named_fc); |
| 1290 | } |
| 1291 | |
| 1292 | break; |
| 1293 | } |
| 1294 | default: |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | BT_HIDDEN |
| 1300 | void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc) |
| 1301 | { |
| 1302 | BT_ASSERT(named_fc); |
| 1303 | ((struct bt_named_field_class *) named_fc)->frozen = true; |
| 1304 | bt_field_class_freeze(named_fc->fc); |
| 1305 | } |
| 1306 | |
| 1307 | BT_HIDDEN |
| 1308 | void _bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc) |
| 1309 | { |
| 1310 | struct bt_field_class *fc = (void *) c_fc; |
| 1311 | |
| 1312 | BT_ASSERT(fc); |
| 1313 | BT_ASSERT_PRE(!fc->part_of_trace_class, |
| 1314 | "Field class is already part of a trace: %!+F", fc); |
| 1315 | fc->part_of_trace_class = true; |
| 1316 | |
| 1317 | switch (fc->type) { |
| 1318 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 1319 | case BT_FIELD_CLASS_TYPE_VARIANT: |
| 1320 | { |
| 1321 | struct bt_field_class_named_field_class_container *container_fc = |
| 1322 | (void *) fc; |
| 1323 | uint64_t i; |
| 1324 | |
| 1325 | for (i = 0; i < container_fc->named_fcs->len; i++) { |
| 1326 | struct bt_named_field_class *named_fc = |
| 1327 | BT_FIELD_CLASS_NAMED_FC_AT_INDEX( |
| 1328 | container_fc, i); |
| 1329 | |
| 1330 | bt_field_class_make_part_of_trace_class(named_fc->fc); |
| 1331 | } |
| 1332 | |
| 1333 | break; |
| 1334 | } |
| 1335 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 1336 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 1337 | { |
| 1338 | struct bt_field_class_array *array_fc = (void *) fc; |
| 1339 | |
| 1340 | bt_field_class_make_part_of_trace_class(array_fc->element_fc); |
| 1341 | break; |
| 1342 | } |
| 1343 | default: |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | void bt_field_class_get_ref(const struct bt_field_class *field_class) |
| 1349 | { |
| 1350 | bt_object_get_ref(field_class); |
| 1351 | } |
| 1352 | |
| 1353 | void bt_field_class_put_ref(const struct bt_field_class *field_class) |
| 1354 | { |
| 1355 | bt_object_put_ref(field_class); |
| 1356 | } |