| 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" |
| 25 | #include "lib/logging.h" |
| 26 | |
| 27 | #include "lib/assert-pre.h" |
| 28 | #include <babeltrace2/trace-ir/field.h> |
| 29 | #include <babeltrace2/trace-ir/field-const.h> |
| 30 | #include "lib/object.h" |
| 31 | #include "compat/compiler.h" |
| 32 | #include "compat/fcntl.h" |
| 33 | #include "common/align.h" |
| 34 | #include "common/assert.h" |
| 35 | #include <inttypes.h> |
| 36 | |
| 37 | #include "field.h" |
| 38 | #include "field-class.h" |
| 39 | #include "lib/func-status.h" |
| 40 | |
| 41 | static |
| 42 | void reset_single_field(struct bt_field *field); |
| 43 | |
| 44 | static |
| 45 | void reset_array_field(struct bt_field *field); |
| 46 | |
| 47 | static |
| 48 | void reset_structure_field(struct bt_field *field); |
| 49 | |
| 50 | static |
| 51 | void reset_option_field(struct bt_field *field); |
| 52 | |
| 53 | static |
| 54 | void reset_variant_field(struct bt_field *field); |
| 55 | |
| 56 | static |
| 57 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen); |
| 58 | |
| 59 | static |
| 60 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen); |
| 61 | |
| 62 | static |
| 63 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen); |
| 64 | |
| 65 | static |
| 66 | void set_option_field_is_frozen(struct bt_field *field, bool is_frozen); |
| 67 | |
| 68 | static |
| 69 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen); |
| 70 | |
| 71 | static |
| 72 | bool single_field_is_set(const struct bt_field *field); |
| 73 | |
| 74 | static |
| 75 | bool array_field_is_set(const struct bt_field *field); |
| 76 | |
| 77 | static |
| 78 | bool structure_field_is_set(const struct bt_field *field); |
| 79 | |
| 80 | static |
| 81 | bool option_field_is_set(const struct bt_field *field); |
| 82 | |
| 83 | static |
| 84 | bool variant_field_is_set(const struct bt_field *field); |
| 85 | |
| 86 | static |
| 87 | struct bt_field_methods bool_field_methods = { |
| 88 | .set_is_frozen = set_single_field_is_frozen, |
| 89 | .is_set = single_field_is_set, |
| 90 | .reset = reset_single_field, |
| 91 | }; |
| 92 | |
| 93 | static |
| 94 | struct bt_field_methods bit_array_field_methods = { |
| 95 | .set_is_frozen = set_single_field_is_frozen, |
| 96 | .is_set = single_field_is_set, |
| 97 | .reset = reset_single_field, |
| 98 | }; |
| 99 | |
| 100 | static |
| 101 | struct bt_field_methods integer_field_methods = { |
| 102 | .set_is_frozen = set_single_field_is_frozen, |
| 103 | .is_set = single_field_is_set, |
| 104 | .reset = reset_single_field, |
| 105 | }; |
| 106 | |
| 107 | static |
| 108 | struct bt_field_methods real_field_methods = { |
| 109 | .set_is_frozen = set_single_field_is_frozen, |
| 110 | .is_set = single_field_is_set, |
| 111 | .reset = reset_single_field, |
| 112 | }; |
| 113 | |
| 114 | static |
| 115 | struct bt_field_methods string_field_methods = { |
| 116 | .set_is_frozen = set_single_field_is_frozen, |
| 117 | .is_set = single_field_is_set, |
| 118 | .reset = reset_single_field, |
| 119 | }; |
| 120 | |
| 121 | static |
| 122 | struct bt_field_methods structure_field_methods = { |
| 123 | .set_is_frozen = set_structure_field_is_frozen, |
| 124 | .is_set = structure_field_is_set, |
| 125 | .reset = reset_structure_field, |
| 126 | }; |
| 127 | |
| 128 | static |
| 129 | struct bt_field_methods array_field_methods = { |
| 130 | .set_is_frozen = set_array_field_is_frozen, |
| 131 | .is_set = array_field_is_set, |
| 132 | .reset = reset_array_field, |
| 133 | }; |
| 134 | |
| 135 | static |
| 136 | struct bt_field_methods option_field_methods = { |
| 137 | .set_is_frozen = set_option_field_is_frozen, |
| 138 | .is_set = option_field_is_set, |
| 139 | .reset = reset_option_field, |
| 140 | }; |
| 141 | |
| 142 | static |
| 143 | struct bt_field_methods variant_field_methods = { |
| 144 | .set_is_frozen = set_variant_field_is_frozen, |
| 145 | .is_set = variant_field_is_set, |
| 146 | .reset = reset_variant_field, |
| 147 | }; |
| 148 | |
| 149 | static |
| 150 | struct bt_field *create_bool_field(struct bt_field_class *); |
| 151 | |
| 152 | static |
| 153 | struct bt_field *create_bit_array_field(struct bt_field_class *); |
| 154 | |
| 155 | static |
| 156 | struct bt_field *create_integer_field(struct bt_field_class *); |
| 157 | |
| 158 | static |
| 159 | struct bt_field *create_real_field(struct bt_field_class *); |
| 160 | |
| 161 | static |
| 162 | struct bt_field *create_string_field(struct bt_field_class *); |
| 163 | |
| 164 | static |
| 165 | struct bt_field *create_structure_field(struct bt_field_class *); |
| 166 | |
| 167 | static |
| 168 | struct bt_field *create_static_array_field(struct bt_field_class *); |
| 169 | |
| 170 | static |
| 171 | struct bt_field *create_dynamic_array_field(struct bt_field_class *); |
| 172 | |
| 173 | static |
| 174 | struct bt_field *create_option_field(struct bt_field_class *); |
| 175 | |
| 176 | static |
| 177 | struct bt_field *create_variant_field(struct bt_field_class *); |
| 178 | |
| 179 | static |
| 180 | struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = { |
| 181 | [BT_FIELD_CLASS_TYPE_BOOL] = create_bool_field, |
| 182 | [BT_FIELD_CLASS_TYPE_BIT_ARRAY] = create_bit_array_field, |
| 183 | [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = create_integer_field, |
| 184 | [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = create_integer_field, |
| 185 | [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = create_integer_field, |
| 186 | [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = create_integer_field, |
| 187 | [BT_FIELD_CLASS_TYPE_REAL] = create_real_field, |
| 188 | [BT_FIELD_CLASS_TYPE_STRING] = create_string_field, |
| 189 | [BT_FIELD_CLASS_TYPE_STRUCTURE] = create_structure_field, |
| 190 | [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = create_static_array_field, |
| 191 | [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = create_dynamic_array_field, |
| 192 | [BT_FIELD_CLASS_TYPE_OPTION] = create_option_field, |
| 193 | [BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR] = create_variant_field, |
| 194 | [BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR] = create_variant_field, |
| 195 | [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR] = create_variant_field, |
| 196 | }; |
| 197 | |
| 198 | static |
| 199 | void destroy_bool_field(struct bt_field *field); |
| 200 | |
| 201 | static |
| 202 | void destroy_bit_array_field(struct bt_field *field); |
| 203 | |
| 204 | static |
| 205 | void destroy_integer_field(struct bt_field *field); |
| 206 | |
| 207 | static |
| 208 | void destroy_real_field(struct bt_field *field); |
| 209 | |
| 210 | static |
| 211 | void destroy_string_field(struct bt_field *field); |
| 212 | |
| 213 | static |
| 214 | void destroy_structure_field(struct bt_field *field); |
| 215 | |
| 216 | static |
| 217 | void destroy_array_field(struct bt_field *field); |
| 218 | |
| 219 | static |
| 220 | void destroy_option_field(struct bt_field *field); |
| 221 | |
| 222 | static |
| 223 | void destroy_variant_field(struct bt_field *field); |
| 224 | |
| 225 | static |
| 226 | void (* const field_destroy_funcs[])(struct bt_field *) = { |
| 227 | [BT_FIELD_CLASS_TYPE_BOOL] = destroy_bool_field, |
| 228 | [BT_FIELD_CLASS_TYPE_BIT_ARRAY] = destroy_bit_array_field, |
| 229 | [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = destroy_integer_field, |
| 230 | [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = destroy_integer_field, |
| 231 | [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = destroy_integer_field, |
| 232 | [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = destroy_integer_field, |
| 233 | [BT_FIELD_CLASS_TYPE_REAL] = destroy_real_field, |
| 234 | [BT_FIELD_CLASS_TYPE_STRING] = destroy_string_field, |
| 235 | [BT_FIELD_CLASS_TYPE_STRUCTURE] = destroy_structure_field, |
| 236 | [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = destroy_array_field, |
| 237 | [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = destroy_array_field, |
| 238 | [BT_FIELD_CLASS_TYPE_OPTION] = destroy_option_field, |
| 239 | [BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR] = destroy_variant_field, |
| 240 | [BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR] = destroy_variant_field, |
| 241 | [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR] = destroy_variant_field, |
| 242 | }; |
| 243 | |
| 244 | struct bt_field_class *bt_field_borrow_class(const struct bt_field *field) |
| 245 | { |
| 246 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 247 | return field->class; |
| 248 | } |
| 249 | |
| 250 | const struct bt_field_class *bt_field_borrow_class_const( |
| 251 | const struct bt_field *field) |
| 252 | { |
| 253 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 254 | return field->class; |
| 255 | } |
| 256 | |
| 257 | enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field) |
| 258 | { |
| 259 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 260 | return field->class->type; |
| 261 | } |
| 262 | |
| 263 | BT_HIDDEN |
| 264 | struct bt_field *bt_field_create(struct bt_field_class *fc) |
| 265 | { |
| 266 | struct bt_field *field = NULL; |
| 267 | |
| 268 | BT_ASSERT(fc); |
| 269 | field = field_create_funcs[fc->type](fc); |
| 270 | if (!field) { |
| 271 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: " |
| 272 | "%![fc-]+F", fc); |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | end: |
| 277 | return field; |
| 278 | } |
| 279 | |
| 280 | static inline |
| 281 | void init_field(struct bt_field *field, struct bt_field_class *fc, |
| 282 | struct bt_field_methods *methods) |
| 283 | { |
| 284 | BT_ASSERT(field); |
| 285 | BT_ASSERT(fc); |
| 286 | bt_object_init_unique(&field->base); |
| 287 | field->methods = methods; |
| 288 | field->class = fc; |
| 289 | bt_object_get_no_null_check(fc); |
| 290 | } |
| 291 | |
| 292 | static |
| 293 | struct bt_field *create_bool_field(struct bt_field_class *fc) |
| 294 | { |
| 295 | struct bt_field_bool *bool_field; |
| 296 | |
| 297 | BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc); |
| 298 | bool_field = g_new0(struct bt_field_bool, 1); |
| 299 | if (!bool_field) { |
| 300 | BT_LIB_LOGE_APPEND_CAUSE( |
| 301 | "Failed to allocate one boolean field."); |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | init_field((void *) bool_field, fc, &bool_field_methods); |
| 306 | BT_LIB_LOGD("Created boolean field object: %!+f", bool_field); |
| 307 | |
| 308 | end: |
| 309 | return (void *) bool_field; |
| 310 | } |
| 311 | |
| 312 | static |
| 313 | struct bt_field *create_bit_array_field(struct bt_field_class *fc) |
| 314 | { |
| 315 | struct bt_field_bit_array *ba_field; |
| 316 | |
| 317 | BT_LIB_LOGD("Creating bit array field object: %![fc-]+F", fc); |
| 318 | ba_field = g_new0(struct bt_field_bit_array, 1); |
| 319 | if (!ba_field) { |
| 320 | BT_LIB_LOGE_APPEND_CAUSE( |
| 321 | "Failed to allocate one bit array field."); |
| 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | init_field((void *) ba_field, fc, &bit_array_field_methods); |
| 326 | BT_LIB_LOGD("Created bit array field object: %!+f", ba_field); |
| 327 | |
| 328 | end: |
| 329 | return (void *) ba_field; |
| 330 | } |
| 331 | |
| 332 | static |
| 333 | struct bt_field *create_integer_field(struct bt_field_class *fc) |
| 334 | { |
| 335 | struct bt_field_integer *int_field; |
| 336 | |
| 337 | BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc); |
| 338 | int_field = g_new0(struct bt_field_integer, 1); |
| 339 | if (!int_field) { |
| 340 | BT_LIB_LOGE_APPEND_CAUSE( |
| 341 | "Failed to allocate one integer field."); |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | init_field((void *) int_field, fc, &integer_field_methods); |
| 346 | BT_LIB_LOGD("Created integer field object: %!+f", int_field); |
| 347 | |
| 348 | end: |
| 349 | return (void *) int_field; |
| 350 | } |
| 351 | |
| 352 | static |
| 353 | struct bt_field *create_real_field(struct bt_field_class *fc) |
| 354 | { |
| 355 | struct bt_field_real *real_field; |
| 356 | |
| 357 | BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc); |
| 358 | real_field = g_new0(struct bt_field_real, 1); |
| 359 | if (!real_field) { |
| 360 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field."); |
| 361 | goto end; |
| 362 | } |
| 363 | |
| 364 | init_field((void *) real_field, fc, &real_field_methods); |
| 365 | BT_LIB_LOGD("Created real field object: %!+f", real_field); |
| 366 | |
| 367 | end: |
| 368 | return (void *) real_field; |
| 369 | } |
| 370 | |
| 371 | static |
| 372 | struct bt_field *create_string_field(struct bt_field_class *fc) |
| 373 | { |
| 374 | struct bt_field_string *string_field; |
| 375 | |
| 376 | BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc); |
| 377 | string_field = g_new0(struct bt_field_string, 1); |
| 378 | if (!string_field) { |
| 379 | BT_LIB_LOGE_APPEND_CAUSE( |
| 380 | "Failed to allocate one string field."); |
| 381 | goto end; |
| 382 | } |
| 383 | |
| 384 | init_field((void *) string_field, fc, &string_field_methods); |
| 385 | string_field->buf = g_array_sized_new(FALSE, FALSE, |
| 386 | sizeof(char), 1); |
| 387 | if (!string_field->buf) { |
| 388 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
| 389 | BT_OBJECT_PUT_REF_AND_RESET(string_field); |
| 390 | goto end; |
| 391 | } |
| 392 | |
| 393 | g_array_index(string_field->buf, char, 0) = '\0'; |
| 394 | BT_LIB_LOGD("Created string field object: %!+f", string_field); |
| 395 | |
| 396 | end: |
| 397 | return (void *) string_field; |
| 398 | } |
| 399 | |
| 400 | static inline |
| 401 | int create_fields_from_named_field_classes( |
| 402 | struct bt_field_class_named_field_class_container *fc, |
| 403 | GPtrArray **fields) |
| 404 | { |
| 405 | int ret = 0; |
| 406 | uint64_t i; |
| 407 | |
| 408 | *fields = g_ptr_array_new_with_free_func( |
| 409 | (GDestroyNotify) bt_field_destroy); |
| 410 | if (!*fields) { |
| 411 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
| 412 | ret = -1; |
| 413 | goto end; |
| 414 | } |
| 415 | |
| 416 | g_ptr_array_set_size(*fields, fc->named_fcs->len); |
| 417 | |
| 418 | for (i = 0; i < fc->named_fcs->len; i++) { |
| 419 | struct bt_field *field; |
| 420 | struct bt_named_field_class *named_fc = fc->named_fcs->pdata[i]; |
| 421 | |
| 422 | field = bt_field_create(named_fc->fc); |
| 423 | if (!field) { |
| 424 | BT_LIB_LOGE_APPEND_CAUSE( |
| 425 | "Failed to create structure member or variant option field: " |
| 426 | "name=\"%s\", %![fc-]+F", |
| 427 | named_fc->name->str, named_fc->fc); |
| 428 | ret = -1; |
| 429 | goto end; |
| 430 | } |
| 431 | |
| 432 | g_ptr_array_index(*fields, i) = field; |
| 433 | } |
| 434 | |
| 435 | end: |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static |
| 440 | struct bt_field *create_structure_field(struct bt_field_class *fc) |
| 441 | { |
| 442 | struct bt_field_structure *struct_field; |
| 443 | |
| 444 | BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc); |
| 445 | struct_field = g_new0(struct bt_field_structure, 1); |
| 446 | if (!struct_field) { |
| 447 | BT_LIB_LOGE_APPEND_CAUSE( |
| 448 | "Failed to allocate one structure field."); |
| 449 | goto end; |
| 450 | } |
| 451 | |
| 452 | init_field((void *) struct_field, fc, &structure_field_methods); |
| 453 | |
| 454 | if (create_fields_from_named_field_classes((void *) fc, |
| 455 | &struct_field->fields)) { |
| 456 | BT_LIB_LOGE_APPEND_CAUSE( |
| 457 | "Cannot create structure member fields: %![fc-]+F", fc); |
| 458 | BT_OBJECT_PUT_REF_AND_RESET(struct_field); |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | BT_LIB_LOGD("Created structure field object: %!+f", struct_field); |
| 463 | |
| 464 | end: |
| 465 | return (void *) struct_field; |
| 466 | } |
| 467 | |
| 468 | static |
| 469 | struct bt_field *create_option_field(struct bt_field_class *fc) |
| 470 | { |
| 471 | struct bt_field_option *opt_field; |
| 472 | struct bt_field_class_option *opt_fc = (void *) fc; |
| 473 | |
| 474 | BT_LIB_LOGD("Creating option field object: %![fc-]+F", fc); |
| 475 | opt_field = g_new0(struct bt_field_option, 1); |
| 476 | if (!opt_field) { |
| 477 | BT_LIB_LOGE_APPEND_CAUSE( |
| 478 | "Failed to allocate one option field."); |
| 479 | goto end; |
| 480 | } |
| 481 | |
| 482 | init_field((void *) opt_field, fc, &option_field_methods); |
| 483 | opt_field->content_field = bt_field_create(opt_fc->content_fc); |
| 484 | if (!opt_field->content_field) { |
| 485 | BT_LIB_LOGE_APPEND_CAUSE( |
| 486 | "Failed to create option field's content field: " |
| 487 | "%![opt-fc-]+F, %![content-fc-]+F", |
| 488 | opt_fc, opt_fc->content_fc); |
| 489 | BT_OBJECT_PUT_REF_AND_RESET(opt_field); |
| 490 | goto end; |
| 491 | } |
| 492 | |
| 493 | BT_LIB_LOGD("Created option field object: %!+f", opt_field); |
| 494 | |
| 495 | end: |
| 496 | return (void *) opt_field; |
| 497 | } |
| 498 | |
| 499 | static |
| 500 | struct bt_field *create_variant_field(struct bt_field_class *fc) |
| 501 | { |
| 502 | struct bt_field_variant *var_field; |
| 503 | |
| 504 | BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc); |
| 505 | var_field = g_new0(struct bt_field_variant, 1); |
| 506 | if (!var_field) { |
| 507 | BT_LIB_LOGE_APPEND_CAUSE( |
| 508 | "Failed to allocate one variant field."); |
| 509 | goto end; |
| 510 | } |
| 511 | |
| 512 | init_field((void *) var_field, fc, &variant_field_methods); |
| 513 | |
| 514 | if (create_fields_from_named_field_classes((void *) fc, |
| 515 | &var_field->fields)) { |
| 516 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: " |
| 517 | "%![fc-]+F", fc); |
| 518 | BT_OBJECT_PUT_REF_AND_RESET(var_field); |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | BT_LIB_LOGD("Created variant field object: %!+f", var_field); |
| 523 | |
| 524 | end: |
| 525 | return (void *) var_field; |
| 526 | } |
| 527 | |
| 528 | static inline |
| 529 | int init_array_field_fields(struct bt_field_array *array_field) |
| 530 | { |
| 531 | int ret = 0; |
| 532 | uint64_t i; |
| 533 | struct bt_field_class_array *array_fc; |
| 534 | |
| 535 | BT_ASSERT(array_field); |
| 536 | array_fc = (void *) array_field->common.class; |
| 537 | array_field->fields = g_ptr_array_sized_new(array_field->length); |
| 538 | if (!array_field->fields) { |
| 539 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
| 540 | ret = -1; |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | g_ptr_array_set_free_func(array_field->fields, |
| 545 | (GDestroyNotify) bt_field_destroy); |
| 546 | g_ptr_array_set_size(array_field->fields, array_field->length); |
| 547 | |
| 548 | for (i = 0; i < array_field->length; i++) { |
| 549 | array_field->fields->pdata[i] = bt_field_create( |
| 550 | array_fc->element_fc); |
| 551 | if (!array_field->fields->pdata[i]) { |
| 552 | BT_LIB_LOGE_APPEND_CAUSE( |
| 553 | "Cannot create array field's element field: " |
| 554 | "index=%" PRIu64 ", %![fc-]+F", i, array_fc); |
| 555 | ret = -1; |
| 556 | goto end; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | end: |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | static |
| 565 | struct bt_field *create_static_array_field(struct bt_field_class *fc) |
| 566 | { |
| 567 | struct bt_field_class_array_static *array_fc = (void *) fc; |
| 568 | struct bt_field_array *array_field; |
| 569 | |
| 570 | BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc); |
| 571 | array_field = g_new0(struct bt_field_array, 1); |
| 572 | if (!array_field) { |
| 573 | BT_LIB_LOGE_APPEND_CAUSE( |
| 574 | "Failed to allocate one static array field."); |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | init_field((void *) array_field, fc, &array_field_methods); |
| 579 | array_field->length = array_fc->length; |
| 580 | |
| 581 | if (init_array_field_fields(array_field)) { |
| 582 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: " |
| 583 | "%![fc-]+F", fc); |
| 584 | BT_OBJECT_PUT_REF_AND_RESET(array_field); |
| 585 | goto end; |
| 586 | } |
| 587 | |
| 588 | BT_LIB_LOGD("Created static array field object: %!+f", array_field); |
| 589 | |
| 590 | end: |
| 591 | return (void *) array_field; |
| 592 | } |
| 593 | |
| 594 | static |
| 595 | struct bt_field *create_dynamic_array_field(struct bt_field_class *fc) |
| 596 | { |
| 597 | struct bt_field_array *array_field; |
| 598 | |
| 599 | BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc); |
| 600 | array_field = g_new0(struct bt_field_array, 1); |
| 601 | if (!array_field) { |
| 602 | BT_LIB_LOGE_APPEND_CAUSE( |
| 603 | "Failed to allocate one dynamic array field."); |
| 604 | goto end; |
| 605 | } |
| 606 | |
| 607 | init_field((void *) array_field, fc, &array_field_methods); |
| 608 | |
| 609 | if (init_array_field_fields(array_field)) { |
| 610 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: " |
| 611 | "%![fc-]+F", fc); |
| 612 | BT_OBJECT_PUT_REF_AND_RESET(array_field); |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field); |
| 617 | |
| 618 | end: |
| 619 | return (void *) array_field; |
| 620 | } |
| 621 | |
| 622 | bt_bool bt_field_bool_get_value(const struct bt_field *field) |
| 623 | { |
| 624 | const struct bt_field_bool *bool_field = (const void *) field; |
| 625 | |
| 626 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 627 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 628 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL, |
| 629 | "Field"); |
| 630 | return (bt_bool) bool_field->value; |
| 631 | } |
| 632 | |
| 633 | void bt_field_bool_set_value(struct bt_field *field, bt_bool value) |
| 634 | { |
| 635 | struct bt_field_bool *bool_field = (void *) field; |
| 636 | |
| 637 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 638 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL, |
| 639 | "Field"); |
| 640 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 641 | bool_field->value = (bool) value; |
| 642 | bt_field_set_single(field, true); |
| 643 | } |
| 644 | |
| 645 | uint64_t bt_field_bit_array_get_value_as_integer(const struct bt_field *field) |
| 646 | { |
| 647 | const struct bt_field_bit_array *ba_field = (const void *) field; |
| 648 | |
| 649 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 650 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 651 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 652 | BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field"); |
| 653 | return ba_field->value_as_int; |
| 654 | } |
| 655 | |
| 656 | void bt_field_bit_array_set_value_as_integer(struct bt_field *field, |
| 657 | uint64_t value) |
| 658 | { |
| 659 | struct bt_field_bit_array *ba_field = (void *) field; |
| 660 | struct bt_field_class_bit_array *ba_fc; |
| 661 | |
| 662 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 663 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 664 | BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field"); |
| 665 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 666 | ba_fc = (void *) field->class; |
| 667 | ba_field->value_as_int = value; |
| 668 | |
| 669 | if (ba_fc->length < 64) { |
| 670 | /* Apply mask */ |
| 671 | ba_field->value_as_int &= ((UINT64_C(1) << ba_fc->length) - 1); |
| 672 | } |
| 673 | |
| 674 | bt_field_set_single(field, true); |
| 675 | } |
| 676 | |
| 677 | int64_t bt_field_integer_signed_get_value(const struct bt_field *field) |
| 678 | { |
| 679 | const struct bt_field_integer *int_field = (const void *) field; |
| 680 | |
| 681 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 682 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 683 | BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field"); |
| 684 | return int_field->value.i; |
| 685 | } |
| 686 | |
| 687 | void bt_field_integer_signed_set_value(struct bt_field *field, int64_t value) |
| 688 | { |
| 689 | struct bt_field_integer *int_field = (void *) field; |
| 690 | |
| 691 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 692 | BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field"); |
| 693 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 694 | BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed( |
| 695 | ((struct bt_field_class_integer *) field->class)->range, value), |
| 696 | "Value is out of bounds: value=%" PRId64 ", %![field-]+f, " |
| 697 | "%![fc-]+F", value, field, field->class); |
| 698 | int_field->value.i = value; |
| 699 | bt_field_set_single(field, true); |
| 700 | } |
| 701 | |
| 702 | uint64_t bt_field_integer_unsigned_get_value(const struct bt_field *field) |
| 703 | { |
| 704 | const struct bt_field_integer *int_field = (const void *) field; |
| 705 | |
| 706 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 707 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 708 | BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field"); |
| 709 | return int_field->value.u; |
| 710 | } |
| 711 | |
| 712 | void bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value) |
| 713 | { |
| 714 | struct bt_field_integer *int_field = (void *) field; |
| 715 | |
| 716 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 717 | BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field"); |
| 718 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 719 | BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned( |
| 720 | ((struct bt_field_class_integer *) field->class)->range, value), |
| 721 | "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, " |
| 722 | "%![fc-]+F", value, field, field->class); |
| 723 | int_field->value.u = value; |
| 724 | bt_field_set_single(field, true); |
| 725 | } |
| 726 | |
| 727 | double bt_field_real_get_value(const struct bt_field *field) |
| 728 | { |
| 729 | const struct bt_field_real *real_field = (const void *) field; |
| 730 | |
| 731 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 732 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 733 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field"); |
| 734 | return real_field->value; |
| 735 | } |
| 736 | |
| 737 | void bt_field_real_set_value(struct bt_field *field, double value) |
| 738 | { |
| 739 | struct bt_field_real *real_field = (void *) field; |
| 740 | |
| 741 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 742 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field"); |
| 743 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 744 | BT_ASSERT_PRE_DEV( |
| 745 | !((struct bt_field_class_real *) field->class)->is_single_precision || |
| 746 | (double) (float) value == value, |
| 747 | "Invalid value for a single-precision real number: value=%f, " |
| 748 | "%![fc-]+F", value, field->class); |
| 749 | real_field->value = value; |
| 750 | bt_field_set_single(field, true); |
| 751 | } |
| 752 | |
| 753 | enum bt_field_enumeration_get_mapping_labels_status |
| 754 | bt_field_enumeration_unsigned_get_mapping_labels( |
| 755 | const struct bt_field *field, |
| 756 | bt_field_class_enumeration_mapping_label_array *label_array, |
| 757 | uint64_t *count) |
| 758 | { |
| 759 | const struct bt_field_integer *int_field = (const void *) field; |
| 760 | |
| 761 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 762 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); |
| 763 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)"); |
| 764 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 765 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 766 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field"); |
| 767 | return (int) |
| 768 | bt_field_class_enumeration_unsigned_get_mapping_labels_for_value( |
| 769 | field->class, int_field->value.u, label_array, count); |
| 770 | } |
| 771 | |
| 772 | enum bt_field_enumeration_get_mapping_labels_status |
| 773 | bt_field_enumeration_signed_get_mapping_labels( |
| 774 | const struct bt_field *field, |
| 775 | bt_field_class_enumeration_mapping_label_array *label_array, |
| 776 | uint64_t *count) |
| 777 | { |
| 778 | const struct bt_field_integer *int_field = (const void *) field; |
| 779 | |
| 780 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 781 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); |
| 782 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)"); |
| 783 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 784 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 785 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field"); |
| 786 | return (int) |
| 787 | bt_field_class_enumeration_signed_get_mapping_labels_for_value( |
| 788 | field->class, int_field->value.i, label_array, count); |
| 789 | } |
| 790 | |
| 791 | const char *bt_field_string_get_value(const struct bt_field *field) |
| 792 | { |
| 793 | const struct bt_field_string *string_field = (const void *) field; |
| 794 | |
| 795 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 796 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 797 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
| 798 | "Field"); |
| 799 | return (const char *) string_field->buf->data; |
| 800 | } |
| 801 | |
| 802 | uint64_t bt_field_string_get_length(const struct bt_field *field) |
| 803 | { |
| 804 | const struct bt_field_string *string_field = (const void *) field; |
| 805 | |
| 806 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 807 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); |
| 808 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
| 809 | "Field"); |
| 810 | return string_field->length; |
| 811 | } |
| 812 | |
| 813 | static inline |
| 814 | void clear_string_field(struct bt_field *field) |
| 815 | { |
| 816 | struct bt_field_string *string_field = (void *) field; |
| 817 | |
| 818 | BT_ASSERT(field); |
| 819 | string_field->length = 0; |
| 820 | bt_field_set_single(field, true); |
| 821 | } |
| 822 | |
| 823 | enum bt_field_string_set_value_status bt_field_string_set_value( |
| 824 | struct bt_field *field, const char *value) |
| 825 | { |
| 826 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 827 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); |
| 828 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 829 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
| 830 | "Field"); |
| 831 | clear_string_field(field); |
| 832 | return (int) bt_field_string_append_with_length(field, value, |
| 833 | (uint64_t) strlen(value)); |
| 834 | } |
| 835 | |
| 836 | enum bt_field_string_append_status bt_field_string_append( |
| 837 | struct bt_field *field, const char *value) |
| 838 | { |
| 839 | return bt_field_string_append_with_length(field, |
| 840 | value, (uint64_t) strlen(value)); |
| 841 | } |
| 842 | |
| 843 | enum bt_field_string_append_status bt_field_string_append_with_length( |
| 844 | struct bt_field *field, const char *value, uint64_t length) |
| 845 | { |
| 846 | struct bt_field_string *string_field = (void *) field; |
| 847 | char *data; |
| 848 | uint64_t new_length; |
| 849 | |
| 850 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 851 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); |
| 852 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 853 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 854 | BT_FIELD_CLASS_TYPE_STRING, "Field"); |
| 855 | |
| 856 | /* Make sure no null bytes are appended */ |
| 857 | BT_ASSERT_PRE_DEV(!memchr(value, '\0', length), |
| 858 | "String value to append contains a null character: " |
| 859 | "partial-value=\"%.32s\", length=%" PRIu64, value, length); |
| 860 | |
| 861 | new_length = length + string_field->length; |
| 862 | |
| 863 | if (G_UNLIKELY(new_length + 1 > string_field->buf->len)) { |
| 864 | g_array_set_size(string_field->buf, new_length + 1); |
| 865 | } |
| 866 | |
| 867 | data = string_field->buf->data; |
| 868 | memcpy(data + string_field->length, value, length); |
| 869 | ((char *) string_field->buf->data)[new_length] = '\0'; |
| 870 | string_field->length = new_length; |
| 871 | bt_field_set_single(field, true); |
| 872 | return BT_FUNC_STATUS_OK; |
| 873 | } |
| 874 | |
| 875 | void bt_field_string_clear(struct bt_field *field) |
| 876 | { |
| 877 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 878 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 879 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 880 | BT_FIELD_CLASS_TYPE_STRING, "Field"); |
| 881 | clear_string_field(field); |
| 882 | } |
| 883 | |
| 884 | uint64_t bt_field_array_get_length(const struct bt_field *field) |
| 885 | { |
| 886 | const struct bt_field_array *array_field = (const void *) field; |
| 887 | |
| 888 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 889 | BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field"); |
| 890 | return array_field->length; |
| 891 | } |
| 892 | |
| 893 | enum bt_field_array_dynamic_set_length_status bt_field_array_dynamic_set_length( |
| 894 | struct bt_field *field, uint64_t length) |
| 895 | { |
| 896 | int ret = BT_FUNC_STATUS_OK; |
| 897 | struct bt_field_array *array_field = (void *) field; |
| 898 | |
| 899 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 900 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 901 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, "Field"); |
| 902 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 903 | |
| 904 | if (G_UNLIKELY(length > array_field->fields->len)) { |
| 905 | /* Make more room */ |
| 906 | struct bt_field_class_array *array_fc; |
| 907 | uint64_t cur_len = array_field->fields->len; |
| 908 | uint64_t i; |
| 909 | |
| 910 | g_ptr_array_set_size(array_field->fields, length); |
| 911 | array_fc = (void *) field->class; |
| 912 | |
| 913 | for (i = cur_len; i < array_field->fields->len; i++) { |
| 914 | struct bt_field *elem_field = bt_field_create( |
| 915 | array_fc->element_fc); |
| 916 | |
| 917 | if (!elem_field) { |
| 918 | BT_LIB_LOGE_APPEND_CAUSE( |
| 919 | "Cannot create element field for " |
| 920 | "dynamic array field: " |
| 921 | "index=%" PRIu64 ", " |
| 922 | "%![array-field-]+f", i, field); |
| 923 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
| 924 | goto end; |
| 925 | } |
| 926 | |
| 927 | BT_ASSERT(!array_field->fields->pdata[i]); |
| 928 | array_field->fields->pdata[i] = elem_field; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | array_field->length = length; |
| 933 | |
| 934 | end: |
| 935 | return ret; |
| 936 | } |
| 937 | |
| 938 | static inline |
| 939 | struct bt_field *borrow_array_field_element_field_by_index( |
| 940 | struct bt_field *field, uint64_t index) |
| 941 | { |
| 942 | struct bt_field_array *array_field = (void *) field; |
| 943 | |
| 944 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 945 | BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field"); |
| 946 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, array_field->length); |
| 947 | return array_field->fields->pdata[index]; |
| 948 | } |
| 949 | |
| 950 | struct bt_field *bt_field_array_borrow_element_field_by_index( |
| 951 | struct bt_field *field, uint64_t index) |
| 952 | { |
| 953 | return borrow_array_field_element_field_by_index(field, index); |
| 954 | } |
| 955 | |
| 956 | const struct bt_field * |
| 957 | bt_field_array_borrow_element_field_by_index_const( |
| 958 | const struct bt_field *field, uint64_t index) |
| 959 | { |
| 960 | return borrow_array_field_element_field_by_index((void *) field, index); |
| 961 | } |
| 962 | |
| 963 | static inline |
| 964 | struct bt_field *borrow_structure_field_member_field_by_index( |
| 965 | struct bt_field *field, uint64_t index) |
| 966 | { |
| 967 | struct bt_field_structure *struct_field = (void *) field; |
| 968 | |
| 969 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 970 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 971 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); |
| 972 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, struct_field->fields->len); |
| 973 | return struct_field->fields->pdata[index]; |
| 974 | } |
| 975 | |
| 976 | struct bt_field *bt_field_structure_borrow_member_field_by_index( |
| 977 | struct bt_field *field, uint64_t index) |
| 978 | { |
| 979 | return borrow_structure_field_member_field_by_index(field, |
| 980 | index); |
| 981 | } |
| 982 | |
| 983 | const struct bt_field * |
| 984 | bt_field_structure_borrow_member_field_by_index_const( |
| 985 | const struct bt_field *field, uint64_t index) |
| 986 | { |
| 987 | return borrow_structure_field_member_field_by_index( |
| 988 | (void *) field, index); |
| 989 | } |
| 990 | |
| 991 | static inline |
| 992 | struct bt_field *borrow_structure_field_member_field_by_name( |
| 993 | struct bt_field *field, const char *name) |
| 994 | { |
| 995 | struct bt_field *ret_field = NULL; |
| 996 | struct bt_field_class_structure *struct_fc; |
| 997 | struct bt_field_structure *struct_field = (void *) field; |
| 998 | gpointer orig_key; |
| 999 | gpointer index; |
| 1000 | |
| 1001 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1002 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Field name"); |
| 1003 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 1004 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); |
| 1005 | struct_fc = (void *) field->class; |
| 1006 | |
| 1007 | if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name, |
| 1008 | &orig_key, &index)) { |
| 1009 | goto end; |
| 1010 | } |
| 1011 | |
| 1012 | ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)]; |
| 1013 | BT_ASSERT(ret_field); |
| 1014 | |
| 1015 | end: |
| 1016 | return ret_field; |
| 1017 | } |
| 1018 | |
| 1019 | struct bt_field *bt_field_structure_borrow_member_field_by_name( |
| 1020 | struct bt_field *field, const char *name) |
| 1021 | { |
| 1022 | return borrow_structure_field_member_field_by_name(field, name); |
| 1023 | } |
| 1024 | |
| 1025 | const struct bt_field *bt_field_structure_borrow_member_field_by_name_const( |
| 1026 | const struct bt_field *field, const char *name) |
| 1027 | { |
| 1028 | return borrow_structure_field_member_field_by_name( |
| 1029 | (void *) field, name); |
| 1030 | } |
| 1031 | |
| 1032 | void bt_field_option_set_has_field(struct bt_field *field, bt_bool has_field) |
| 1033 | { |
| 1034 | struct bt_field_option *opt_field = (void *) field; |
| 1035 | |
| 1036 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1037 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 1038 | BT_FIELD_CLASS_TYPE_OPTION, "Field"); |
| 1039 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 1040 | |
| 1041 | if (has_field) { |
| 1042 | opt_field->selected_field = opt_field->content_field; |
| 1043 | } else { |
| 1044 | opt_field->selected_field = NULL; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | struct bt_field *bt_field_option_borrow_field(struct bt_field *field) |
| 1049 | { |
| 1050 | struct bt_field_option *opt_field = (void *) field; |
| 1051 | |
| 1052 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1053 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 1054 | BT_FIELD_CLASS_TYPE_OPTION, "Field"); |
| 1055 | return opt_field->selected_field; |
| 1056 | } |
| 1057 | |
| 1058 | const struct bt_field *bt_field_option_borrow_field_const( |
| 1059 | const struct bt_field *field) |
| 1060 | { |
| 1061 | return (const void *) bt_field_option_borrow_field((void *) field); |
| 1062 | } |
| 1063 | |
| 1064 | static inline |
| 1065 | struct bt_field *borrow_variant_field_selected_option_field( |
| 1066 | struct bt_field *field) |
| 1067 | { |
| 1068 | struct bt_field_variant *var_field = (void *) field; |
| 1069 | |
| 1070 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1071 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
| 1072 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
| 1073 | "Variant field has no selected field: %!+f", field); |
| 1074 | return var_field->selected_field; |
| 1075 | } |
| 1076 | |
| 1077 | struct bt_field *bt_field_variant_borrow_selected_option_field( |
| 1078 | struct bt_field *field) |
| 1079 | { |
| 1080 | return borrow_variant_field_selected_option_field(field); |
| 1081 | } |
| 1082 | |
| 1083 | const struct bt_field *bt_field_variant_borrow_selected_option_field_const( |
| 1084 | const struct bt_field *field) |
| 1085 | { |
| 1086 | return borrow_variant_field_selected_option_field((void *) field); |
| 1087 | } |
| 1088 | |
| 1089 | static |
| 1090 | const struct bt_field_class_variant_option * |
| 1091 | borrow_variant_field_selected_class_option(const struct bt_field *field) |
| 1092 | { |
| 1093 | const struct bt_field_class_named_field_class_container *container_fc; |
| 1094 | const struct bt_field_variant *var_field = (const void *) field; |
| 1095 | |
| 1096 | BT_ASSERT(field); |
| 1097 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
| 1098 | "Variant field has no selected field: %!+f", field); |
| 1099 | container_fc = (const void *) field->class; |
| 1100 | return container_fc->named_fcs->pdata[var_field->selected_index]; |
| 1101 | } |
| 1102 | |
| 1103 | const struct bt_field_class_variant_option * |
| 1104 | bt_field_variant_borrow_selected_class_option_const( |
| 1105 | const struct bt_field *field) |
| 1106 | { |
| 1107 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1108 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
| 1109 | return borrow_variant_field_selected_class_option(field); |
| 1110 | } |
| 1111 | |
| 1112 | const struct bt_field_class_variant_with_selector_unsigned_option * |
| 1113 | bt_field_variant_with_unsigned_selector_borrow_selected_class_option_const( |
| 1114 | const struct bt_field *field) |
| 1115 | { |
| 1116 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1117 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 1118 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR, "Field"); |
| 1119 | return (const void *) borrow_variant_field_selected_class_option(field); |
| 1120 | } |
| 1121 | |
| 1122 | const struct bt_field_class_variant_with_selector_signed_option * |
| 1123 | bt_field_variant_with_signed_selector_borrow_selected_class_option_const( |
| 1124 | const struct bt_field *field) |
| 1125 | { |
| 1126 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1127 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
| 1128 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, "Field"); |
| 1129 | return (const void *) borrow_variant_field_selected_class_option(field); |
| 1130 | } |
| 1131 | |
| 1132 | enum bt_field_variant_select_option_field_by_index_status |
| 1133 | bt_field_variant_select_option_field_by_index( |
| 1134 | struct bt_field *field, uint64_t index) |
| 1135 | { |
| 1136 | struct bt_field_variant *var_field = (void *) field; |
| 1137 | |
| 1138 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1139 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
| 1140 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
| 1141 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, var_field->fields->len); |
| 1142 | var_field->selected_field = var_field->fields->pdata[index]; |
| 1143 | var_field->selected_index = index; |
| 1144 | return BT_FUNC_STATUS_OK; |
| 1145 | } |
| 1146 | |
| 1147 | uint64_t bt_field_variant_get_selected_option_field_index( |
| 1148 | const struct bt_field *field) |
| 1149 | { |
| 1150 | const struct bt_field_variant *var_field = (const void *) field; |
| 1151 | |
| 1152 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
| 1153 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
| 1154 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
| 1155 | "Variant field has no selected field: %!+f", field); |
| 1156 | return var_field->selected_index; |
| 1157 | } |
| 1158 | |
| 1159 | static inline |
| 1160 | void bt_field_finalize(struct bt_field *field) |
| 1161 | { |
| 1162 | BT_ASSERT(field); |
| 1163 | BT_LOGD_STR("Putting field's class."); |
| 1164 | BT_OBJECT_PUT_REF_AND_RESET(field->class); |
| 1165 | } |
| 1166 | |
| 1167 | static |
| 1168 | void destroy_bool_field(struct bt_field *field) |
| 1169 | { |
| 1170 | BT_ASSERT(field); |
| 1171 | BT_LIB_LOGD("Destroying boolean field object: %!+f", field); |
| 1172 | bt_field_finalize(field); |
| 1173 | g_free(field); |
| 1174 | } |
| 1175 | |
| 1176 | static |
| 1177 | void destroy_bit_array_field(struct bt_field *field) |
| 1178 | { |
| 1179 | BT_ASSERT(field); |
| 1180 | BT_LIB_LOGD("Destroying bit array field object: %!+f", field); |
| 1181 | bt_field_finalize(field); |
| 1182 | g_free(field); |
| 1183 | } |
| 1184 | |
| 1185 | static |
| 1186 | void destroy_integer_field(struct bt_field *field) |
| 1187 | { |
| 1188 | BT_ASSERT(field); |
| 1189 | BT_LIB_LOGD("Destroying integer field object: %!+f", field); |
| 1190 | bt_field_finalize(field); |
| 1191 | g_free(field); |
| 1192 | } |
| 1193 | |
| 1194 | static |
| 1195 | void destroy_real_field(struct bt_field *field) |
| 1196 | { |
| 1197 | BT_ASSERT(field); |
| 1198 | BT_LIB_LOGD("Destroying real field object: %!+f", field); |
| 1199 | bt_field_finalize(field); |
| 1200 | g_free(field); |
| 1201 | } |
| 1202 | |
| 1203 | static |
| 1204 | void destroy_structure_field(struct bt_field *field) |
| 1205 | { |
| 1206 | struct bt_field_structure *struct_field = (void *) field; |
| 1207 | |
| 1208 | BT_ASSERT(field); |
| 1209 | BT_LIB_LOGD("Destroying structure field object: %!+f", field); |
| 1210 | bt_field_finalize(field); |
| 1211 | |
| 1212 | if (struct_field->fields) { |
| 1213 | g_ptr_array_free(struct_field->fields, TRUE); |
| 1214 | struct_field->fields = NULL; |
| 1215 | } |
| 1216 | |
| 1217 | g_free(field); |
| 1218 | } |
| 1219 | |
| 1220 | static |
| 1221 | void destroy_option_field(struct bt_field *field) |
| 1222 | { |
| 1223 | struct bt_field_option *opt_field = (void *) field; |
| 1224 | |
| 1225 | BT_ASSERT(field); |
| 1226 | BT_LIB_LOGD("Destroying option field object: %!+f", field); |
| 1227 | bt_field_finalize(field); |
| 1228 | |
| 1229 | if (opt_field->content_field) { |
| 1230 | bt_field_destroy(opt_field->content_field); |
| 1231 | } |
| 1232 | |
| 1233 | g_free(field); |
| 1234 | } |
| 1235 | |
| 1236 | static |
| 1237 | void destroy_variant_field(struct bt_field *field) |
| 1238 | { |
| 1239 | struct bt_field_variant *var_field = (void *) field; |
| 1240 | |
| 1241 | BT_ASSERT(field); |
| 1242 | BT_LIB_LOGD("Destroying variant field object: %!+f", field); |
| 1243 | bt_field_finalize(field); |
| 1244 | |
| 1245 | if (var_field->fields) { |
| 1246 | g_ptr_array_free(var_field->fields, TRUE); |
| 1247 | var_field->fields = NULL; |
| 1248 | } |
| 1249 | |
| 1250 | g_free(field); |
| 1251 | } |
| 1252 | |
| 1253 | static |
| 1254 | void destroy_array_field(struct bt_field *field) |
| 1255 | { |
| 1256 | struct bt_field_array *array_field = (void *) field; |
| 1257 | |
| 1258 | BT_ASSERT(field); |
| 1259 | BT_LIB_LOGD("Destroying array field object: %!+f", field); |
| 1260 | bt_field_finalize(field); |
| 1261 | |
| 1262 | if (array_field->fields) { |
| 1263 | g_ptr_array_free(array_field->fields, TRUE); |
| 1264 | array_field->fields = NULL; |
| 1265 | } |
| 1266 | |
| 1267 | g_free(field); |
| 1268 | } |
| 1269 | |
| 1270 | static |
| 1271 | void destroy_string_field(struct bt_field *field) |
| 1272 | { |
| 1273 | struct bt_field_string *string_field = (void *) field; |
| 1274 | |
| 1275 | BT_ASSERT(field); |
| 1276 | BT_LIB_LOGD("Destroying string field object: %!+f", field); |
| 1277 | bt_field_finalize(field); |
| 1278 | |
| 1279 | if (string_field->buf) { |
| 1280 | g_array_free(string_field->buf, TRUE); |
| 1281 | string_field->buf = NULL; |
| 1282 | } |
| 1283 | |
| 1284 | g_free(field); |
| 1285 | } |
| 1286 | |
| 1287 | BT_HIDDEN |
| 1288 | void bt_field_destroy(struct bt_field *field) |
| 1289 | { |
| 1290 | BT_ASSERT(field); |
| 1291 | field_destroy_funcs[field->class->type](field); |
| 1292 | } |
| 1293 | |
| 1294 | static |
| 1295 | void reset_single_field(struct bt_field *field) |
| 1296 | { |
| 1297 | BT_ASSERT(field); |
| 1298 | field->is_set = false; |
| 1299 | } |
| 1300 | |
| 1301 | static |
| 1302 | void reset_structure_field(struct bt_field *field) |
| 1303 | { |
| 1304 | uint64_t i; |
| 1305 | struct bt_field_structure *struct_field = (void *) field; |
| 1306 | |
| 1307 | BT_ASSERT(field); |
| 1308 | |
| 1309 | for (i = 0; i < struct_field->fields->len; i++) { |
| 1310 | bt_field_reset(struct_field->fields->pdata[i]); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | static |
| 1315 | void reset_option_field(struct bt_field *field) |
| 1316 | { |
| 1317 | struct bt_field_option *opt_field = (void *) field; |
| 1318 | |
| 1319 | BT_ASSERT(opt_field); |
| 1320 | bt_field_reset(opt_field->content_field); |
| 1321 | opt_field->selected_field = NULL; |
| 1322 | } |
| 1323 | |
| 1324 | static |
| 1325 | void reset_variant_field(struct bt_field *field) |
| 1326 | { |
| 1327 | uint64_t i; |
| 1328 | struct bt_field_variant *var_field = (void *) field; |
| 1329 | |
| 1330 | BT_ASSERT(field); |
| 1331 | |
| 1332 | for (i = 0; i < var_field->fields->len; i++) { |
| 1333 | bt_field_reset(var_field->fields->pdata[i]); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | static |
| 1338 | void reset_array_field(struct bt_field *field) |
| 1339 | { |
| 1340 | uint64_t i; |
| 1341 | struct bt_field_array *array_field = (void *) field; |
| 1342 | |
| 1343 | BT_ASSERT(field); |
| 1344 | |
| 1345 | for (i = 0; i < array_field->fields->len; i++) { |
| 1346 | bt_field_reset(array_field->fields->pdata[i]); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static |
| 1351 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen) |
| 1352 | { |
| 1353 | field->frozen = is_frozen; |
| 1354 | } |
| 1355 | |
| 1356 | static |
| 1357 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen) |
| 1358 | { |
| 1359 | uint64_t i; |
| 1360 | struct bt_field_structure *struct_field = (void *) field; |
| 1361 | |
| 1362 | BT_LIB_LOGD("Setting structure field's frozen state: " |
| 1363 | "%![field-]+f, is-frozen=%d", field, is_frozen); |
| 1364 | |
| 1365 | for (i = 0; i < struct_field->fields->len; i++) { |
| 1366 | struct bt_field *member_field = struct_field->fields->pdata[i]; |
| 1367 | |
| 1368 | BT_LIB_LOGD("Setting structure field's member field's " |
| 1369 | "frozen state: %![field-]+f, index=%" PRIu64, |
| 1370 | member_field, i); |
| 1371 | _bt_field_set_is_frozen(member_field, is_frozen); |
| 1372 | } |
| 1373 | |
| 1374 | set_single_field_is_frozen(field, is_frozen); |
| 1375 | } |
| 1376 | |
| 1377 | static |
| 1378 | void set_option_field_is_frozen(struct bt_field *field, bool is_frozen) |
| 1379 | { |
| 1380 | struct bt_field_option *opt_field = (void *) field; |
| 1381 | |
| 1382 | BT_LIB_LOGD("Setting option field's frozen state: " |
| 1383 | "%![field-]+f, is-frozen=%d", field, is_frozen); |
| 1384 | _bt_field_set_is_frozen(opt_field->content_field, is_frozen); |
| 1385 | set_single_field_is_frozen(field, is_frozen); |
| 1386 | } |
| 1387 | |
| 1388 | static |
| 1389 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen) |
| 1390 | { |
| 1391 | uint64_t i; |
| 1392 | struct bt_field_variant *var_field = (void *) field; |
| 1393 | |
| 1394 | BT_LIB_LOGD("Setting variant field's frozen state: " |
| 1395 | "%![field-]+f, is-frozen=%d", field, is_frozen); |
| 1396 | |
| 1397 | for (i = 0; i < var_field->fields->len; i++) { |
| 1398 | struct bt_field *option_field = var_field->fields->pdata[i]; |
| 1399 | |
| 1400 | BT_LIB_LOGD("Setting variant field's option field's " |
| 1401 | "frozen state: %![field-]+f, index=%" PRIu64, |
| 1402 | option_field, i); |
| 1403 | _bt_field_set_is_frozen(option_field, is_frozen); |
| 1404 | } |
| 1405 | |
| 1406 | set_single_field_is_frozen(field, is_frozen); |
| 1407 | } |
| 1408 | |
| 1409 | static |
| 1410 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen) |
| 1411 | { |
| 1412 | uint64_t i; |
| 1413 | struct bt_field_array *array_field = (void *) field; |
| 1414 | |
| 1415 | BT_LIB_LOGD("Setting array field's frozen state: " |
| 1416 | "%![field-]+f, is-frozen=%d", field, is_frozen); |
| 1417 | |
| 1418 | for (i = 0; i < array_field->fields->len; i++) { |
| 1419 | struct bt_field *elem_field = array_field->fields->pdata[i]; |
| 1420 | |
| 1421 | BT_LIB_LOGD("Setting array field's element field's " |
| 1422 | "frozen state: %![field-]+f, index=%" PRIu64, |
| 1423 | elem_field, i); |
| 1424 | _bt_field_set_is_frozen(elem_field, is_frozen); |
| 1425 | } |
| 1426 | |
| 1427 | set_single_field_is_frozen(field, is_frozen); |
| 1428 | } |
| 1429 | |
| 1430 | BT_HIDDEN |
| 1431 | void _bt_field_set_is_frozen(const struct bt_field *field, |
| 1432 | bool is_frozen) |
| 1433 | { |
| 1434 | BT_ASSERT(field); |
| 1435 | BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d", |
| 1436 | field, is_frozen); |
| 1437 | BT_ASSERT(field->methods->set_is_frozen); |
| 1438 | field->methods->set_is_frozen((void *) field, is_frozen); |
| 1439 | } |
| 1440 | |
| 1441 | static |
| 1442 | bool single_field_is_set(const struct bt_field *field) |
| 1443 | { |
| 1444 | BT_ASSERT(field); |
| 1445 | return field->is_set; |
| 1446 | } |
| 1447 | |
| 1448 | static |
| 1449 | bool structure_field_is_set(const struct bt_field *field) |
| 1450 | { |
| 1451 | bool is_set = true; |
| 1452 | uint64_t i; |
| 1453 | const struct bt_field_structure *struct_field = (const void *) field; |
| 1454 | |
| 1455 | BT_ASSERT(field); |
| 1456 | |
| 1457 | for (i = 0; i < struct_field->fields->len; i++) { |
| 1458 | is_set = bt_field_is_set(struct_field->fields->pdata[i]); |
| 1459 | if (!is_set) { |
| 1460 | goto end; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | end: |
| 1465 | return is_set; |
| 1466 | } |
| 1467 | |
| 1468 | static |
| 1469 | bool option_field_is_set(const struct bt_field *field) |
| 1470 | { |
| 1471 | const struct bt_field_option *opt_field = (const void *) field; |
| 1472 | bool is_set = false; |
| 1473 | |
| 1474 | BT_ASSERT(field); |
| 1475 | |
| 1476 | if (opt_field->selected_field) { |
| 1477 | is_set = bt_field_is_set(opt_field->selected_field); |
| 1478 | } |
| 1479 | |
| 1480 | return is_set; |
| 1481 | } |
| 1482 | |
| 1483 | static |
| 1484 | bool variant_field_is_set(const struct bt_field *field) |
| 1485 | { |
| 1486 | const struct bt_field_variant *var_field = (const void *) field; |
| 1487 | bool is_set = false; |
| 1488 | |
| 1489 | BT_ASSERT(field); |
| 1490 | |
| 1491 | if (var_field->selected_field) { |
| 1492 | is_set = bt_field_is_set(var_field->selected_field); |
| 1493 | } |
| 1494 | |
| 1495 | return is_set; |
| 1496 | } |
| 1497 | |
| 1498 | static |
| 1499 | bool array_field_is_set(const struct bt_field *field) |
| 1500 | { |
| 1501 | bool is_set = true; |
| 1502 | uint64_t i; |
| 1503 | const struct bt_field_array *array_field = (const void *) field; |
| 1504 | |
| 1505 | BT_ASSERT(field); |
| 1506 | |
| 1507 | for (i = 0; i < array_field->length; i++) { |
| 1508 | is_set = bt_field_is_set(array_field->fields->pdata[i]); |
| 1509 | if (!is_set) { |
| 1510 | goto end; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | end: |
| 1515 | return is_set; |
| 1516 | } |