| 1 | /* |
| 2 | * fields.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Event Fields |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "FIELDS" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/assert-pre-internal.h> |
| 33 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 34 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 35 | #include <babeltrace/object-internal.h> |
| 36 | #include <babeltrace/ref.h> |
| 37 | #include <babeltrace/compiler-internal.h> |
| 38 | #include <babeltrace/compat/fcntl-internal.h> |
| 39 | #include <babeltrace/align-internal.h> |
| 40 | #include <babeltrace/assert-internal.h> |
| 41 | #include <inttypes.h> |
| 42 | |
| 43 | static |
| 44 | struct bt_field_common *bt_field_integer_copy(struct bt_field_common *src); |
| 45 | |
| 46 | static |
| 47 | struct bt_field_common *bt_field_enumeration_copy_recursive( |
| 48 | struct bt_field_common *src); |
| 49 | |
| 50 | static |
| 51 | struct bt_field_common *bt_field_floating_point_copy( |
| 52 | struct bt_field_common *src); |
| 53 | |
| 54 | static |
| 55 | struct bt_field_common *bt_field_structure_copy_recursive( |
| 56 | struct bt_field_common *src); |
| 57 | |
| 58 | static |
| 59 | struct bt_field_common *bt_field_variant_copy_recursive( |
| 60 | struct bt_field_common *src); |
| 61 | |
| 62 | static |
| 63 | struct bt_field_common *bt_field_array_copy_recursive( |
| 64 | struct bt_field_common *src); |
| 65 | |
| 66 | static |
| 67 | struct bt_field_common *bt_field_sequence_copy_recursive( |
| 68 | struct bt_field_common *src); |
| 69 | |
| 70 | static |
| 71 | struct bt_field_common *bt_field_string_copy(struct bt_field_common *src); |
| 72 | |
| 73 | static struct bt_field_common_methods bt_field_integer_methods = { |
| 74 | .freeze = bt_field_common_generic_freeze, |
| 75 | .validate = bt_field_common_generic_validate, |
| 76 | .copy = bt_field_integer_copy, |
| 77 | .is_set = bt_field_common_generic_is_set, |
| 78 | .reset = bt_field_common_generic_reset, |
| 79 | }; |
| 80 | |
| 81 | static struct bt_field_common_methods bt_field_floating_point_methods = { |
| 82 | .freeze = bt_field_common_generic_freeze, |
| 83 | .validate = bt_field_common_generic_validate, |
| 84 | .copy = bt_field_floating_point_copy, |
| 85 | .is_set = bt_field_common_generic_is_set, |
| 86 | .reset = bt_field_common_generic_reset, |
| 87 | }; |
| 88 | |
| 89 | static struct bt_field_common_methods bt_field_enumeration_methods = { |
| 90 | .freeze = bt_field_common_enumeration_freeze_recursive, |
| 91 | .validate = bt_field_common_enumeration_validate_recursive, |
| 92 | .copy = bt_field_enumeration_copy_recursive, |
| 93 | .is_set = bt_field_common_enumeration_is_set_recursive, |
| 94 | .reset = bt_field_common_enumeration_reset_recursive, |
| 95 | }; |
| 96 | |
| 97 | static struct bt_field_common_methods bt_field_string_methods = { |
| 98 | .freeze = bt_field_common_generic_freeze, |
| 99 | .validate = bt_field_common_generic_validate, |
| 100 | .copy = bt_field_string_copy, |
| 101 | .is_set = bt_field_common_generic_is_set, |
| 102 | .reset = bt_field_common_string_reset, |
| 103 | }; |
| 104 | |
| 105 | static struct bt_field_common_methods bt_field_structure_methods = { |
| 106 | .freeze = bt_field_common_structure_freeze_recursive, |
| 107 | .validate = bt_field_common_structure_validate_recursive, |
| 108 | .copy = bt_field_structure_copy_recursive, |
| 109 | .is_set = bt_field_common_structure_is_set_recursive, |
| 110 | .reset = bt_field_common_structure_reset_recursive, |
| 111 | }; |
| 112 | |
| 113 | static struct bt_field_common_methods bt_field_sequence_methods = { |
| 114 | .freeze = bt_field_common_sequence_freeze_recursive, |
| 115 | .validate = bt_field_common_sequence_validate_recursive, |
| 116 | .copy = bt_field_sequence_copy_recursive, |
| 117 | .is_set = bt_field_common_sequence_is_set_recursive, |
| 118 | .reset = bt_field_common_sequence_reset_recursive, |
| 119 | }; |
| 120 | |
| 121 | static struct bt_field_common_methods bt_field_array_methods = { |
| 122 | .freeze = bt_field_common_array_freeze_recursive, |
| 123 | .validate = bt_field_common_array_validate_recursive, |
| 124 | .copy = bt_field_array_copy_recursive, |
| 125 | .is_set = bt_field_common_array_is_set_recursive, |
| 126 | .reset = bt_field_common_array_reset_recursive, |
| 127 | }; |
| 128 | |
| 129 | static struct bt_field_common_methods bt_field_variant_methods = { |
| 130 | .freeze = bt_field_common_variant_freeze_recursive, |
| 131 | .validate = bt_field_common_variant_validate_recursive, |
| 132 | .copy = bt_field_variant_copy_recursive, |
| 133 | .is_set = bt_field_common_variant_is_set_recursive, |
| 134 | .reset = bt_field_common_variant_reset_recursive, |
| 135 | }; |
| 136 | |
| 137 | static |
| 138 | struct bt_field *bt_field_integer_create(struct bt_field_type *); |
| 139 | |
| 140 | static |
| 141 | struct bt_field *bt_field_enumeration_create(struct bt_field_type *); |
| 142 | |
| 143 | static |
| 144 | struct bt_field *bt_field_floating_point_create(struct bt_field_type *); |
| 145 | |
| 146 | static |
| 147 | struct bt_field *bt_field_structure_create(struct bt_field_type *); |
| 148 | |
| 149 | static |
| 150 | struct bt_field *bt_field_variant_create(struct bt_field_type *); |
| 151 | |
| 152 | static |
| 153 | struct bt_field *bt_field_array_create(struct bt_field_type *); |
| 154 | |
| 155 | static |
| 156 | struct bt_field *bt_field_sequence_create(struct bt_field_type *); |
| 157 | |
| 158 | static |
| 159 | struct bt_field *bt_field_string_create(struct bt_field_type *); |
| 160 | |
| 161 | static |
| 162 | struct bt_field *(* const field_create_funcs[])(struct bt_field_type *) = { |
| 163 | [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_create, |
| 164 | [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_create, |
| 165 | [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_create, |
| 166 | [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_create, |
| 167 | [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_create, |
| 168 | [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_create, |
| 169 | [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_create, |
| 170 | [BT_FIELD_TYPE_ID_STRING] = bt_field_string_create, |
| 171 | }; |
| 172 | |
| 173 | struct bt_field *bt_field_create(struct bt_field_type *type) |
| 174 | { |
| 175 | struct bt_field *field = NULL; |
| 176 | enum bt_field_type_id type_id; |
| 177 | |
| 178 | BT_ASSERT_PRE_NON_NULL(type, "Field type"); |
| 179 | BT_ASSERT(field_type_common_has_known_id((void *) type)); |
| 180 | BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0, |
| 181 | "Field type is invalid: %!+F", type); |
| 182 | type_id = bt_field_type_get_type_id(type); |
| 183 | field = field_create_funcs[type_id](type); |
| 184 | if (!field) { |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | bt_field_type_freeze(type); |
| 189 | |
| 190 | end: |
| 191 | return field; |
| 192 | } |
| 193 | |
| 194 | struct bt_field_type *bt_field_borrow_type(struct bt_field *field) |
| 195 | { |
| 196 | return (void *) bt_field_common_borrow_type((void *) field); |
| 197 | } |
| 198 | |
| 199 | enum bt_field_type_id bt_field_get_type_id(struct bt_field *field) |
| 200 | { |
| 201 | struct bt_field_common *field_common = (void *) field; |
| 202 | |
| 203 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
| 204 | return field_common->type->id; |
| 205 | } |
| 206 | |
| 207 | bt_bool bt_field_is_integer(struct bt_field *field) |
| 208 | { |
| 209 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_INTEGER; |
| 210 | } |
| 211 | |
| 212 | bt_bool bt_field_is_floating_point(struct bt_field *field) |
| 213 | { |
| 214 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_FLOAT; |
| 215 | } |
| 216 | |
| 217 | bt_bool bt_field_is_enumeration(struct bt_field *field) |
| 218 | { |
| 219 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ENUM; |
| 220 | } |
| 221 | |
| 222 | bt_bool bt_field_is_string(struct bt_field *field) |
| 223 | { |
| 224 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRING; |
| 225 | } |
| 226 | |
| 227 | bt_bool bt_field_is_structure(struct bt_field *field) |
| 228 | { |
| 229 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRUCT; |
| 230 | } |
| 231 | |
| 232 | bt_bool bt_field_is_array(struct bt_field *field) |
| 233 | { |
| 234 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ARRAY; |
| 235 | } |
| 236 | |
| 237 | bt_bool bt_field_is_sequence(struct bt_field *field) |
| 238 | { |
| 239 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SEQUENCE; |
| 240 | } |
| 241 | |
| 242 | bt_bool bt_field_is_variant(struct bt_field *field) |
| 243 | { |
| 244 | return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_VARIANT; |
| 245 | } |
| 246 | |
| 247 | BT_HIDDEN |
| 248 | int64_t bt_field_sequence_get_int_length(struct bt_field *field) |
| 249 | { |
| 250 | return bt_field_common_sequence_get_int_length((void *) field); |
| 251 | } |
| 252 | |
| 253 | struct bt_field *bt_field_sequence_borrow_length(struct bt_field *field) |
| 254 | { |
| 255 | return (void *) bt_field_common_sequence_borrow_length((void *) field); |
| 256 | } |
| 257 | |
| 258 | int bt_field_sequence_set_length(struct bt_field *field, |
| 259 | struct bt_field *length_field) |
| 260 | { |
| 261 | return bt_field_common_sequence_set_length((void *) field, |
| 262 | (void *) length_field); |
| 263 | } |
| 264 | |
| 265 | struct bt_field *bt_field_structure_borrow_field_by_index( |
| 266 | struct bt_field *field, uint64_t index) |
| 267 | { |
| 268 | return (void *) bt_field_common_structure_borrow_field_by_index( |
| 269 | (void *) field, index); |
| 270 | } |
| 271 | |
| 272 | struct bt_field *bt_field_structure_borrow_field_by_name( |
| 273 | struct bt_field *field, const char *name) |
| 274 | { |
| 275 | return (void *) bt_field_common_structure_borrow_field_by_name( |
| 276 | (void *) field, name); |
| 277 | } |
| 278 | |
| 279 | int bt_field_structure_set_field_by_name(struct bt_field_common *field, |
| 280 | const char *name, struct bt_field_common *value) |
| 281 | { |
| 282 | return bt_field_common_structure_set_field_by_name((void *) field, |
| 283 | name, (void *) value); |
| 284 | } |
| 285 | |
| 286 | struct bt_field *bt_field_array_borrow_field( |
| 287 | struct bt_field *field, uint64_t index) |
| 288 | { |
| 289 | return (void *) bt_field_common_array_borrow_field((void *) field, |
| 290 | index, (bt_field_common_create_func) bt_field_create); |
| 291 | } |
| 292 | |
| 293 | struct bt_field *bt_field_sequence_borrow_field( |
| 294 | struct bt_field *field, uint64_t index) |
| 295 | { |
| 296 | return (void *) bt_field_common_sequence_borrow_field((void *) field, |
| 297 | index, (bt_field_common_create_func) bt_field_create); |
| 298 | } |
| 299 | |
| 300 | struct bt_field *bt_field_variant_borrow_field(struct bt_field *field, |
| 301 | struct bt_field *tag_field) |
| 302 | { |
| 303 | return (void *) bt_field_common_variant_borrow_field((void *) field, |
| 304 | (void *) tag_field, |
| 305 | (bt_field_common_create_func) bt_field_create); |
| 306 | } |
| 307 | |
| 308 | struct bt_field *bt_field_variant_borrow_current_field( |
| 309 | struct bt_field *variant_field) |
| 310 | { |
| 311 | return (void *) bt_field_common_variant_borrow_current_field( |
| 312 | (void *) variant_field); |
| 313 | } |
| 314 | |
| 315 | struct bt_field_common *bt_field_variant_borrow_tag( |
| 316 | struct bt_field_common *variant_field) |
| 317 | { |
| 318 | return (void *) bt_field_common_variant_borrow_tag( |
| 319 | (void *) variant_field); |
| 320 | } |
| 321 | |
| 322 | struct bt_field *bt_field_enumeration_borrow_container(struct bt_field *field) |
| 323 | { |
| 324 | return (void *) bt_field_common_enumeration_borrow_container( |
| 325 | (void *) field, (bt_field_common_create_func) bt_field_create); |
| 326 | } |
| 327 | |
| 328 | struct bt_field_type_enumeration_mapping_iterator * |
| 329 | bt_field_enumeration_get_mappings(struct bt_field *field) |
| 330 | { |
| 331 | return bt_field_common_enumeration_get_mappings((void *) field, |
| 332 | (bt_field_common_create_func) bt_field_create); |
| 333 | } |
| 334 | |
| 335 | int bt_field_integer_signed_get_value(struct bt_field *field, int64_t *value) |
| 336 | { |
| 337 | return bt_field_common_integer_signed_get_value((void *) field, value); |
| 338 | } |
| 339 | |
| 340 | int bt_field_integer_signed_set_value(struct bt_field *field, |
| 341 | int64_t value) |
| 342 | { |
| 343 | return bt_field_common_integer_signed_set_value((void *) field, value); |
| 344 | } |
| 345 | |
| 346 | int bt_field_integer_unsigned_get_value(struct bt_field *field, |
| 347 | uint64_t *value) |
| 348 | { |
| 349 | return bt_field_common_integer_unsigned_get_value((void *) field, |
| 350 | value); |
| 351 | } |
| 352 | |
| 353 | int bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value) |
| 354 | { |
| 355 | return bt_field_common_integer_unsigned_set_value((void *) field, value); |
| 356 | } |
| 357 | |
| 358 | int bt_field_floating_point_get_value(struct bt_field *field, |
| 359 | double *value) |
| 360 | { |
| 361 | return bt_field_common_floating_point_get_value((void *) field, value); |
| 362 | } |
| 363 | |
| 364 | int bt_field_floating_point_set_value(struct bt_field *field, |
| 365 | double value) |
| 366 | { |
| 367 | return bt_field_common_floating_point_set_value((void *) field, value); |
| 368 | } |
| 369 | |
| 370 | const char *bt_field_string_get_value(struct bt_field *field) |
| 371 | { |
| 372 | return bt_field_common_string_get_value((void *) field); |
| 373 | } |
| 374 | |
| 375 | int bt_field_string_set_value(struct bt_field *field, const char *value) |
| 376 | { |
| 377 | return bt_field_common_string_set_value((void *) field, value); |
| 378 | } |
| 379 | |
| 380 | int bt_field_string_append(struct bt_field *field, const char *value) |
| 381 | { |
| 382 | return bt_field_common_string_append((void *) field, value); |
| 383 | } |
| 384 | |
| 385 | int bt_field_string_append_len(struct bt_field *field, |
| 386 | const char *value, unsigned int length) |
| 387 | { |
| 388 | return bt_field_common_string_append_len((void *) field, value, length); |
| 389 | } |
| 390 | |
| 391 | BT_HIDDEN |
| 392 | struct bt_field_common *bt_field_common_copy(struct bt_field_common *field) |
| 393 | { |
| 394 | struct bt_field_common *copy = NULL; |
| 395 | |
| 396 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
| 397 | BT_ASSERT(field_type_common_has_known_id(field->type)); |
| 398 | BT_ASSERT(field->methods->copy); |
| 399 | copy = field->methods->copy(field); |
| 400 | if (!copy) { |
| 401 | BT_LOGW("Cannot create field: ft-addr=%p", field->type); |
| 402 | goto end; |
| 403 | } |
| 404 | |
| 405 | bt_field_common_set(copy, field->payload_set); |
| 406 | |
| 407 | end: |
| 408 | return copy; |
| 409 | } |
| 410 | |
| 411 | struct bt_field *bt_field_copy(struct bt_field *field) |
| 412 | { |
| 413 | return (void *) bt_field_common_copy((void *) field); |
| 414 | } |
| 415 | |
| 416 | static void bt_field_common_finalize(struct bt_field_common *field) |
| 417 | { |
| 418 | BT_ASSERT(field); |
| 419 | BT_LOGD_STR("Putting field's type."); |
| 420 | bt_put(field->type); |
| 421 | } |
| 422 | |
| 423 | BT_HIDDEN |
| 424 | void bt_field_common_integer_destroy(struct bt_object *obj) |
| 425 | { |
| 426 | struct bt_field_common_integer *integer = (void *) obj; |
| 427 | |
| 428 | BT_ASSERT(obj); |
| 429 | bt_field_common_finalize(BT_TO_COMMON(integer)); |
| 430 | BT_LOGD("Destroying integer field object: addr=%p", obj); |
| 431 | g_free(obj); |
| 432 | } |
| 433 | |
| 434 | BT_HIDDEN |
| 435 | void bt_field_common_enumeration_destroy_recursive(struct bt_object *obj) |
| 436 | { |
| 437 | struct bt_field_common_enumeration *enumeration = (void *) obj; |
| 438 | |
| 439 | BT_ASSERT(enumeration); |
| 440 | bt_field_common_finalize(BT_TO_COMMON(enumeration)); |
| 441 | BT_LOGD("Destroying enumeration field object: addr=%p", obj); |
| 442 | BT_LOGD_STR("Putting payload field."); |
| 443 | bt_put(enumeration->payload); |
| 444 | g_free(enumeration); |
| 445 | } |
| 446 | |
| 447 | BT_HIDDEN |
| 448 | void bt_field_common_floating_point_destroy(struct bt_object *obj) |
| 449 | { |
| 450 | struct bt_field_common_floating_point *floating_point = (void *) obj; |
| 451 | |
| 452 | BT_ASSERT(obj); |
| 453 | bt_field_common_finalize(BT_TO_COMMON(floating_point)); |
| 454 | BT_LOGD("Destroying floating point number field object: addr=%p", obj); |
| 455 | g_free(obj); |
| 456 | } |
| 457 | |
| 458 | BT_HIDDEN |
| 459 | void bt_field_common_structure_destroy_recursive(struct bt_object *obj) |
| 460 | { |
| 461 | struct bt_field_common_structure *structure = (void *) obj; |
| 462 | |
| 463 | BT_ASSERT(obj); |
| 464 | bt_field_common_finalize(BT_TO_COMMON(structure)); |
| 465 | BT_LOGD("Destroying structure field object: addr=%p", obj); |
| 466 | g_ptr_array_free(structure->fields, TRUE); |
| 467 | g_free(structure); |
| 468 | } |
| 469 | |
| 470 | BT_HIDDEN |
| 471 | void bt_field_common_variant_destroy_recursive(struct bt_object *obj) |
| 472 | { |
| 473 | struct bt_field_common_variant *variant = (void *) obj; |
| 474 | |
| 475 | BT_ASSERT(obj); |
| 476 | bt_field_common_finalize(BT_TO_COMMON(variant)); |
| 477 | BT_LOGD("Destroying variant field object: addr=%p", obj); |
| 478 | BT_LOGD_STR("Putting tag field."); |
| 479 | bt_put(variant->tag); |
| 480 | BT_LOGD_STR("Putting payload field."); |
| 481 | bt_put(variant->payload); |
| 482 | g_free(variant); |
| 483 | } |
| 484 | |
| 485 | BT_HIDDEN |
| 486 | void bt_field_common_array_destroy_recursive(struct bt_object *obj) |
| 487 | { |
| 488 | struct bt_field_common_array *array = (void *) obj; |
| 489 | |
| 490 | BT_ASSERT(obj); |
| 491 | bt_field_common_finalize(BT_TO_COMMON(array)); |
| 492 | BT_LOGD("Destroying array field object: addr=%p", obj); |
| 493 | g_ptr_array_free(array->elements, TRUE); |
| 494 | g_free(array); |
| 495 | } |
| 496 | |
| 497 | BT_HIDDEN |
| 498 | void bt_field_common_sequence_destroy_recursive(struct bt_object *obj) |
| 499 | { |
| 500 | struct bt_field_common_sequence *sequence = (void *) obj; |
| 501 | |
| 502 | BT_ASSERT(obj); |
| 503 | bt_field_common_finalize(BT_TO_COMMON(sequence)); |
| 504 | BT_LOGD("Destroying sequence field object: addr=%p", obj); |
| 505 | |
| 506 | if (sequence->elements) { |
| 507 | g_ptr_array_free(sequence->elements, TRUE); |
| 508 | } |
| 509 | |
| 510 | BT_LOGD_STR("Putting length field."); |
| 511 | bt_put(sequence->length); |
| 512 | g_free(sequence); |
| 513 | } |
| 514 | |
| 515 | BT_HIDDEN |
| 516 | void bt_field_common_string_destroy(struct bt_object *obj) |
| 517 | { |
| 518 | struct bt_field_common_string *string = (void *) obj; |
| 519 | |
| 520 | BT_ASSERT(obj); |
| 521 | bt_field_common_finalize(BT_TO_COMMON(string)); |
| 522 | BT_LOGD("Destroying string field object: addr=%p", obj); |
| 523 | |
| 524 | if (string->payload) { |
| 525 | g_string_free(string->payload, TRUE); |
| 526 | } |
| 527 | |
| 528 | g_free(string); |
| 529 | } |
| 530 | |
| 531 | static |
| 532 | struct bt_field *bt_field_integer_create(struct bt_field_type *type) |
| 533 | { |
| 534 | struct bt_field_common_integer *integer = |
| 535 | g_new0(struct bt_field_common_integer, 1); |
| 536 | |
| 537 | BT_LOGD("Creating integer field object: ft-addr=%p", type); |
| 538 | |
| 539 | if (integer) { |
| 540 | bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type, |
| 541 | bt_field_common_integer_destroy, |
| 542 | &bt_field_integer_methods); |
| 543 | BT_LOGD("Created integer field object: addr=%p, ft-addr=%p", |
| 544 | integer, type); |
| 545 | } else { |
| 546 | BT_LOGE_STR("Failed to allocate one integer field."); |
| 547 | } |
| 548 | |
| 549 | return (void *) integer; |
| 550 | } |
| 551 | |
| 552 | static |
| 553 | struct bt_field *bt_field_enumeration_create(struct bt_field_type *type) |
| 554 | { |
| 555 | struct bt_field_common_enumeration *enumeration = g_new0( |
| 556 | struct bt_field_common_enumeration, 1); |
| 557 | |
| 558 | BT_LOGD("Creating enumeration field object: ft-addr=%p", type); |
| 559 | |
| 560 | if (enumeration) { |
| 561 | bt_field_common_initialize(BT_TO_COMMON(enumeration), |
| 562 | (void *) type, |
| 563 | bt_field_common_enumeration_destroy_recursive, |
| 564 | &bt_field_enumeration_methods); |
| 565 | BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p", |
| 566 | enumeration, type); |
| 567 | } else { |
| 568 | BT_LOGE_STR("Failed to allocate one enumeration field."); |
| 569 | } |
| 570 | |
| 571 | return (void *) enumeration; |
| 572 | } |
| 573 | |
| 574 | static |
| 575 | struct bt_field *bt_field_floating_point_create(struct bt_field_type *type) |
| 576 | { |
| 577 | struct bt_field_common_floating_point *floating_point; |
| 578 | |
| 579 | BT_LOGD("Creating floating point number field object: ft-addr=%p", type); |
| 580 | floating_point = g_new0(struct bt_field_common_floating_point, 1); |
| 581 | |
| 582 | if (floating_point) { |
| 583 | bt_field_common_initialize(BT_TO_COMMON(floating_point), |
| 584 | (void *) type, |
| 585 | bt_field_common_floating_point_destroy, |
| 586 | &bt_field_floating_point_methods); |
| 587 | BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p", |
| 588 | floating_point, type); |
| 589 | } else { |
| 590 | BT_LOGE_STR("Failed to allocate one floating point number field."); |
| 591 | } |
| 592 | |
| 593 | return (void *) floating_point; |
| 594 | } |
| 595 | |
| 596 | BT_HIDDEN |
| 597 | int bt_field_common_structure_initialize(struct bt_field_common *field, |
| 598 | struct bt_field_type_common *type, |
| 599 | bt_object_release_func release_func, |
| 600 | struct bt_field_common_methods *methods, |
| 601 | bt_field_common_create_func field_create_func) |
| 602 | { |
| 603 | int ret = 0; |
| 604 | struct bt_field_type_common_structure *structure_type = |
| 605 | BT_FROM_COMMON(type); |
| 606 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); |
| 607 | size_t i; |
| 608 | |
| 609 | BT_LOGD("Initializing common structure field object: ft-addr=%p", type); |
| 610 | bt_field_common_initialize(field, type, release_func, methods); |
| 611 | structure->fields = g_ptr_array_new_with_free_func( |
| 612 | (GDestroyNotify) bt_put); |
| 613 | g_ptr_array_set_size(structure->fields, structure_type->fields->len); |
| 614 | |
| 615 | /* Create all fields contained by the structure field. */ |
| 616 | for (i = 0; i < structure_type->fields->len; i++) { |
| 617 | struct bt_field_common *field; |
| 618 | struct structure_field_common *struct_field = |
| 619 | g_ptr_array_index(structure_type->fields, i); |
| 620 | |
| 621 | field = field_create_func(struct_field->type); |
| 622 | if (!field) { |
| 623 | BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu", |
| 624 | g_quark_to_string(struct_field->name), i); |
| 625 | ret = -1; |
| 626 | goto end; |
| 627 | } |
| 628 | |
| 629 | g_ptr_array_index(structure->fields, i) = field; |
| 630 | } |
| 631 | |
| 632 | BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p", |
| 633 | field, type); |
| 634 | |
| 635 | end: |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static |
| 640 | struct bt_field *bt_field_structure_create(struct bt_field_type *type) |
| 641 | { |
| 642 | struct bt_field_common_structure *structure = g_new0( |
| 643 | struct bt_field_common_structure, 1); |
| 644 | int iret; |
| 645 | |
| 646 | BT_LOGD("Creating structure field object: ft-addr=%p", type); |
| 647 | |
| 648 | if (!structure) { |
| 649 | BT_LOGE_STR("Failed to allocate one structure field."); |
| 650 | goto end; |
| 651 | } |
| 652 | |
| 653 | iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure), |
| 654 | (void *) type, bt_field_common_structure_destroy_recursive, |
| 655 | &bt_field_structure_methods, |
| 656 | (bt_field_common_create_func) bt_field_create); |
| 657 | if (iret) { |
| 658 | BT_PUT(structure); |
| 659 | goto end; |
| 660 | } |
| 661 | |
| 662 | BT_LOGD("Created structure field object: addr=%p, ft-addr=%p", |
| 663 | structure, type); |
| 664 | |
| 665 | end: |
| 666 | return (void *) structure; |
| 667 | } |
| 668 | |
| 669 | static |
| 670 | struct bt_field *bt_field_variant_create(struct bt_field_type *type) |
| 671 | { |
| 672 | struct bt_field_common_variant *variant = g_new0( |
| 673 | struct bt_field_common_variant, 1); |
| 674 | |
| 675 | BT_LOGD("Creating variant field object: ft-addr=%p", type); |
| 676 | |
| 677 | if (variant) { |
| 678 | bt_field_common_initialize(BT_TO_COMMON(variant), |
| 679 | (void *) type, |
| 680 | bt_field_common_variant_destroy_recursive, |
| 681 | &bt_field_variant_methods); |
| 682 | BT_LOGD("Created variant field object: addr=%p, ft-addr=%p", |
| 683 | variant, type); |
| 684 | } else { |
| 685 | BT_LOGE_STR("Failed to allocate one variant field."); |
| 686 | } |
| 687 | |
| 688 | return (void *) variant; |
| 689 | } |
| 690 | |
| 691 | BT_HIDDEN |
| 692 | int bt_field_common_array_initialize(struct bt_field_common *field, |
| 693 | struct bt_field_type_common *type, |
| 694 | bt_object_release_func release_func, |
| 695 | struct bt_field_common_methods *methods) |
| 696 | { |
| 697 | struct bt_field_type_common_array *array_type = BT_FROM_COMMON(type); |
| 698 | struct bt_field_common_array *array = BT_FROM_COMMON(field); |
| 699 | unsigned int array_length; |
| 700 | int ret = 0; |
| 701 | |
| 702 | BT_LOGD("Initializing common array field object: ft-addr=%p", type); |
| 703 | BT_ASSERT(type); |
| 704 | bt_field_common_initialize(field, type, release_func, methods); |
| 705 | array_length = array_type->length; |
| 706 | array->elements = g_ptr_array_sized_new(array_length); |
| 707 | if (!array->elements) { |
| 708 | ret = -1; |
| 709 | goto end; |
| 710 | } |
| 711 | |
| 712 | g_ptr_array_set_free_func(array->elements, (GDestroyNotify) bt_put); |
| 713 | g_ptr_array_set_size(array->elements, array_length); |
| 714 | BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p", |
| 715 | field, type); |
| 716 | |
| 717 | end: |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | static |
| 722 | struct bt_field *bt_field_array_create(struct bt_field_type *type) |
| 723 | { |
| 724 | struct bt_field_common_array *array = |
| 725 | g_new0(struct bt_field_common_array, 1); |
| 726 | int ret; |
| 727 | |
| 728 | BT_LOGD("Creating array field object: ft-addr=%p", type); |
| 729 | BT_ASSERT(type); |
| 730 | |
| 731 | if (!array) { |
| 732 | BT_LOGE_STR("Failed to allocate one array field."); |
| 733 | goto end; |
| 734 | } |
| 735 | |
| 736 | ret = bt_field_common_array_initialize(BT_TO_COMMON(array), |
| 737 | (void *) type, |
| 738 | bt_field_common_array_destroy_recursive, |
| 739 | &bt_field_array_methods); |
| 740 | if (ret) { |
| 741 | BT_PUT(array); |
| 742 | goto end; |
| 743 | } |
| 744 | |
| 745 | BT_LOGD("Created array field object: addr=%p, ft-addr=%p", |
| 746 | array, type); |
| 747 | |
| 748 | end: |
| 749 | return (void *) array; |
| 750 | } |
| 751 | |
| 752 | static |
| 753 | struct bt_field *bt_field_sequence_create(struct bt_field_type *type) |
| 754 | { |
| 755 | struct bt_field_common_sequence *sequence = g_new0( |
| 756 | struct bt_field_common_sequence, 1); |
| 757 | |
| 758 | BT_LOGD("Creating sequence field object: ft-addr=%p", type); |
| 759 | |
| 760 | if (sequence) { |
| 761 | bt_field_common_initialize(BT_TO_COMMON(sequence), |
| 762 | (void *) type, |
| 763 | bt_field_common_sequence_destroy_recursive, |
| 764 | &bt_field_sequence_methods); |
| 765 | BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p", |
| 766 | sequence, type); |
| 767 | } else { |
| 768 | BT_LOGE_STR("Failed to allocate one sequence field."); |
| 769 | } |
| 770 | |
| 771 | return (void *) sequence; |
| 772 | } |
| 773 | |
| 774 | static |
| 775 | struct bt_field *bt_field_string_create(struct bt_field_type *type) |
| 776 | { |
| 777 | struct bt_field_common_string *string = g_new0( |
| 778 | struct bt_field_common_string, 1); |
| 779 | |
| 780 | BT_LOGD("Creating string field object: ft-addr=%p", type); |
| 781 | |
| 782 | if (string) { |
| 783 | bt_field_common_initialize(BT_TO_COMMON(string), |
| 784 | (void *) type, |
| 785 | bt_field_common_string_destroy, |
| 786 | &bt_field_string_methods); |
| 787 | BT_LOGD("Created string field object: addr=%p, ft-addr=%p", |
| 788 | string, type); |
| 789 | } else { |
| 790 | BT_LOGE_STR("Failed to allocate one string field."); |
| 791 | } |
| 792 | |
| 793 | return (void *) string; |
| 794 | } |
| 795 | |
| 796 | BT_HIDDEN |
| 797 | int bt_field_common_generic_validate(struct bt_field_common *field) |
| 798 | { |
| 799 | return (field && field->payload_set) ? 0 : -1; |
| 800 | } |
| 801 | |
| 802 | BT_HIDDEN |
| 803 | int bt_field_common_enumeration_validate_recursive( |
| 804 | struct bt_field_common *field) |
| 805 | { |
| 806 | int ret; |
| 807 | struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field); |
| 808 | |
| 809 | BT_ASSERT(field); |
| 810 | |
| 811 | if (!enumeration->payload) { |
| 812 | BT_ASSERT_PRE_MSG("Invalid enumeration field: payload is not set: " |
| 813 | "%!+_f", field); |
| 814 | ret = -1; |
| 815 | goto end; |
| 816 | } |
| 817 | |
| 818 | ret = bt_field_common_validate_recursive(enumeration->payload); |
| 819 | |
| 820 | end: |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | BT_HIDDEN |
| 825 | int bt_field_common_structure_validate_recursive(struct bt_field_common *field) |
| 826 | { |
| 827 | int64_t i; |
| 828 | int ret = 0; |
| 829 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); |
| 830 | |
| 831 | BT_ASSERT(field); |
| 832 | |
| 833 | for (i = 0; i < structure->fields->len; i++) { |
| 834 | ret = bt_field_common_validate_recursive( |
| 835 | (void *) structure->fields->pdata[i]); |
| 836 | |
| 837 | if (ret) { |
| 838 | int this_ret; |
| 839 | const char *name; |
| 840 | |
| 841 | this_ret = bt_field_type_common_structure_borrow_field_by_index( |
| 842 | field->type, &name, NULL, i); |
| 843 | BT_ASSERT(this_ret == 0); |
| 844 | BT_ASSERT_PRE_MSG("Invalid structure field's field: " |
| 845 | "%![struct-field-]+_f, field-name=\"%s\", " |
| 846 | "index=%" PRId64 ", %![field-]+_f", |
| 847 | field, name, i, structure->fields->pdata[i]); |
| 848 | goto end; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | end: |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | BT_HIDDEN |
| 857 | int bt_field_common_variant_validate_recursive(struct bt_field_common *field) |
| 858 | { |
| 859 | int ret = 0; |
| 860 | struct bt_field_common_variant *variant = BT_FROM_COMMON(field); |
| 861 | |
| 862 | BT_ASSERT(field); |
| 863 | ret = bt_field_common_validate_recursive(variant->payload); |
| 864 | if (ret) { |
| 865 | BT_ASSERT_PRE_MSG("Invalid variant field's payload field: " |
| 866 | "%![variant-field-]+_f, %![payload-field-]+_f", |
| 867 | field, variant->payload); |
| 868 | } |
| 869 | |
| 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | BT_HIDDEN |
| 874 | int bt_field_common_array_validate_recursive(struct bt_field_common *field) |
| 875 | { |
| 876 | int64_t i; |
| 877 | int ret = 0; |
| 878 | struct bt_field_common_array *array = BT_FROM_COMMON(field); |
| 879 | |
| 880 | BT_ASSERT(field); |
| 881 | |
| 882 | for (i = 0; i < array->elements->len; i++) { |
| 883 | ret = bt_field_common_validate_recursive((void *) array->elements->pdata[i]); |
| 884 | if (ret) { |
| 885 | BT_ASSERT_PRE_MSG("Invalid array field's element field: " |
| 886 | "%![array-field-]+_f, " PRId64 ", " |
| 887 | "%![elem-field-]+_f", |
| 888 | field, i, array->elements->pdata[i]); |
| 889 | goto end; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | end: |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | BT_HIDDEN |
| 898 | int bt_field_common_sequence_validate_recursive(struct bt_field_common *field) |
| 899 | { |
| 900 | size_t i; |
| 901 | int ret = 0; |
| 902 | struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field); |
| 903 | |
| 904 | BT_ASSERT(field); |
| 905 | |
| 906 | for (i = 0; i < sequence->elements->len; i++) { |
| 907 | ret = bt_field_common_validate_recursive( |
| 908 | (void *) sequence->elements->pdata[i]); |
| 909 | if (ret) { |
| 910 | BT_ASSERT_PRE_MSG("Invalid sequence field's element field: " |
| 911 | "%![seq-field-]+_f, " PRId64 ", " |
| 912 | "%![elem-field-]+_f", |
| 913 | field, i, sequence->elements->pdata[i]); |
| 914 | goto end; |
| 915 | } |
| 916 | } |
| 917 | end: |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | BT_HIDDEN |
| 922 | void bt_field_common_generic_reset(struct bt_field_common *field) |
| 923 | { |
| 924 | BT_ASSERT(field); |
| 925 | field->payload_set = false; |
| 926 | } |
| 927 | |
| 928 | BT_HIDDEN |
| 929 | void bt_field_common_enumeration_reset_recursive(struct bt_field_common *field) |
| 930 | { |
| 931 | struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field); |
| 932 | |
| 933 | BT_ASSERT(field); |
| 934 | |
| 935 | if (!enumeration->payload) { |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | bt_field_common_reset_recursive(enumeration->payload); |
| 940 | } |
| 941 | |
| 942 | BT_HIDDEN |
| 943 | void bt_field_common_structure_reset_recursive(struct bt_field_common *field) |
| 944 | { |
| 945 | int64_t i; |
| 946 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); |
| 947 | |
| 948 | BT_ASSERT(field); |
| 949 | |
| 950 | for (i = 0; i < structure->fields->len; i++) { |
| 951 | struct bt_field_common *member = structure->fields->pdata[i]; |
| 952 | |
| 953 | if (!member) { |
| 954 | /* |
| 955 | * Structure members are lazily initialized; |
| 956 | * skip if this member has not been allocated |
| 957 | * yet. |
| 958 | */ |
| 959 | continue; |
| 960 | } |
| 961 | |
| 962 | bt_field_common_reset_recursive(member); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | BT_HIDDEN |
| 967 | void bt_field_common_variant_reset_recursive(struct bt_field_common *field) |
| 968 | { |
| 969 | struct bt_field_common_variant *variant = BT_FROM_COMMON(field); |
| 970 | |
| 971 | BT_ASSERT(field); |
| 972 | BT_PUT(variant->tag); |
| 973 | BT_PUT(variant->payload); |
| 974 | } |
| 975 | |
| 976 | BT_HIDDEN |
| 977 | void bt_field_common_array_reset_recursive(struct bt_field_common *field) |
| 978 | { |
| 979 | size_t i; |
| 980 | struct bt_field_common_array *array = BT_FROM_COMMON(field); |
| 981 | |
| 982 | BT_ASSERT(field); |
| 983 | |
| 984 | for (i = 0; i < array->elements->len; i++) { |
| 985 | struct bt_field_common *member = array->elements->pdata[i]; |
| 986 | |
| 987 | if (!member) { |
| 988 | /* |
| 989 | * Array elements are lazily initialized; skip |
| 990 | * if this member has not been allocated yet. |
| 991 | */ |
| 992 | continue; |
| 993 | } |
| 994 | |
| 995 | bt_field_common_reset_recursive(member); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | BT_HIDDEN |
| 1000 | void bt_field_common_sequence_reset_recursive(struct bt_field_common *field) |
| 1001 | { |
| 1002 | struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field); |
| 1003 | |
| 1004 | BT_ASSERT(field); |
| 1005 | |
| 1006 | if (sequence->elements) { |
| 1007 | g_ptr_array_free(sequence->elements, TRUE); |
| 1008 | sequence->elements = NULL; |
| 1009 | } |
| 1010 | |
| 1011 | BT_PUT(sequence->length); |
| 1012 | } |
| 1013 | |
| 1014 | BT_HIDDEN |
| 1015 | void bt_field_common_string_reset(struct bt_field_common *field) |
| 1016 | { |
| 1017 | struct bt_field_common_string *string = BT_FROM_COMMON(field); |
| 1018 | |
| 1019 | BT_ASSERT(field); |
| 1020 | bt_field_common_generic_reset(field); |
| 1021 | |
| 1022 | if (string->payload) { |
| 1023 | g_string_truncate(string->payload, 0); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | static |
| 1028 | struct bt_field_common *bt_field_integer_copy(struct bt_field_common *src) |
| 1029 | { |
| 1030 | struct bt_field_common_integer *integer_src = (void *) src; |
| 1031 | struct bt_field_common_integer *integer_dst; |
| 1032 | |
| 1033 | BT_LOGD("Copying integer field: src-field-addr=%p", src); |
| 1034 | integer_dst = (void *) bt_field_create((void *) src->type); |
| 1035 | if (!integer_dst) { |
| 1036 | goto end; |
| 1037 | } |
| 1038 | |
| 1039 | integer_dst->payload = integer_src->payload; |
| 1040 | BT_LOGD_STR("Copied integer field."); |
| 1041 | |
| 1042 | end: |
| 1043 | return BT_TO_COMMON(integer_dst); |
| 1044 | } |
| 1045 | |
| 1046 | static |
| 1047 | struct bt_field_common *bt_field_enumeration_copy_recursive( |
| 1048 | struct bt_field_common *src) |
| 1049 | { |
| 1050 | struct bt_field_common_enumeration *enum_src = BT_FROM_COMMON(src); |
| 1051 | struct bt_field_common_enumeration *enum_dst; |
| 1052 | |
| 1053 | BT_LOGD("Copying enumeration field: src-field-addr=%p", src); |
| 1054 | enum_dst = (void *) bt_field_create((void *) src->type); |
| 1055 | if (!enum_dst) { |
| 1056 | goto error; |
| 1057 | } |
| 1058 | |
| 1059 | if (enum_src->payload) { |
| 1060 | BT_LOGD_STR("Copying enumeration field's payload field."); |
| 1061 | enum_dst->payload = (void *) |
| 1062 | bt_field_copy((void *) enum_src->payload); |
| 1063 | if (!enum_dst->payload) { |
| 1064 | BT_LOGE_STR("Cannot copy enumeration field's payload field."); |
| 1065 | goto error; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | BT_LOGD_STR("Copied enumeration field."); |
| 1070 | goto end; |
| 1071 | |
| 1072 | error: |
| 1073 | BT_PUT(enum_dst); |
| 1074 | |
| 1075 | end: |
| 1076 | return BT_TO_COMMON(enum_dst); |
| 1077 | } |
| 1078 | |
| 1079 | static |
| 1080 | struct bt_field_common *bt_field_floating_point_copy( |
| 1081 | struct bt_field_common *src) |
| 1082 | { |
| 1083 | struct bt_field_common_floating_point *float_src = BT_FROM_COMMON(src); |
| 1084 | struct bt_field_common_floating_point *float_dst; |
| 1085 | |
| 1086 | BT_LOGD("Copying floating point number field: src-field-addr=%p", src); |
| 1087 | float_dst = (void *) bt_field_create((void *) src->type); |
| 1088 | if (!float_dst) { |
| 1089 | goto end; |
| 1090 | } |
| 1091 | |
| 1092 | float_dst->payload = float_src->payload; |
| 1093 | BT_LOGD_STR("Copied floating point number field."); |
| 1094 | |
| 1095 | end: |
| 1096 | return BT_TO_COMMON(float_dst); |
| 1097 | } |
| 1098 | |
| 1099 | static |
| 1100 | struct bt_field_common *bt_field_structure_copy_recursive( |
| 1101 | struct bt_field_common *src) |
| 1102 | { |
| 1103 | int64_t i; |
| 1104 | struct bt_field_common_structure *struct_src = BT_FROM_COMMON(src); |
| 1105 | struct bt_field_common_structure *struct_dst; |
| 1106 | |
| 1107 | BT_LOGD("Copying structure field: src-field-addr=%p", src); |
| 1108 | struct_dst = (void *) bt_field_create((void *) src->type); |
| 1109 | if (!struct_dst) { |
| 1110 | goto error; |
| 1111 | } |
| 1112 | |
| 1113 | g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len); |
| 1114 | |
| 1115 | for (i = 0; i < struct_src->fields->len; i++) { |
| 1116 | struct bt_field_common *field = |
| 1117 | g_ptr_array_index(struct_src->fields, i); |
| 1118 | struct bt_field_common *field_copy = NULL; |
| 1119 | |
| 1120 | if (field) { |
| 1121 | BT_LOGD("Copying structure field's field: src-field-addr=%p" |
| 1122 | "index=%" PRId64, field, i); |
| 1123 | field_copy = (void *) bt_field_copy((void *) field); |
| 1124 | if (!field_copy) { |
| 1125 | BT_LOGE("Cannot copy structure field's field: " |
| 1126 | "src-field-addr=%p, index=%" PRId64, |
| 1127 | field, i); |
| 1128 | goto error; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | BT_MOVE(g_ptr_array_index(struct_dst->fields, i), field_copy); |
| 1133 | } |
| 1134 | |
| 1135 | BT_LOGD_STR("Copied structure field."); |
| 1136 | goto end; |
| 1137 | |
| 1138 | error: |
| 1139 | BT_PUT(struct_dst); |
| 1140 | |
| 1141 | end: |
| 1142 | return BT_TO_COMMON(struct_dst); |
| 1143 | } |
| 1144 | |
| 1145 | static |
| 1146 | struct bt_field_common *bt_field_variant_copy_recursive( |
| 1147 | struct bt_field_common *src) |
| 1148 | { |
| 1149 | struct bt_field_common_variant *variant_src = BT_FROM_COMMON(src); |
| 1150 | struct bt_field_common_variant *variant_dst; |
| 1151 | |
| 1152 | BT_LOGD("Copying variant field: src-field-addr=%p", src); |
| 1153 | variant_dst = (void *) bt_field_create((void *) src->type); |
| 1154 | if (!variant_dst) { |
| 1155 | goto end; |
| 1156 | } |
| 1157 | |
| 1158 | if (variant_src->tag) { |
| 1159 | BT_LOGD_STR("Copying variant field's tag field."); |
| 1160 | variant_dst->tag = (void *) bt_field_copy( |
| 1161 | (void *) variant_src->tag); |
| 1162 | if (!variant_dst->tag) { |
| 1163 | BT_LOGE_STR("Cannot copy variant field's tag field."); |
| 1164 | goto error; |
| 1165 | } |
| 1166 | } |
| 1167 | if (variant_src->payload) { |
| 1168 | BT_LOGD_STR("Copying variant field's payload field."); |
| 1169 | variant_dst->payload = (void *) bt_field_copy( |
| 1170 | (void *) variant_src->payload); |
| 1171 | if (!variant_dst->payload) { |
| 1172 | BT_LOGE_STR("Cannot copy variant field's payload field."); |
| 1173 | goto error; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | BT_LOGD_STR("Copied variant field."); |
| 1178 | goto end; |
| 1179 | |
| 1180 | error: |
| 1181 | BT_PUT(variant_dst); |
| 1182 | |
| 1183 | end: |
| 1184 | return BT_TO_COMMON(variant_dst); |
| 1185 | } |
| 1186 | |
| 1187 | static |
| 1188 | struct bt_field_common *bt_field_array_copy_recursive( |
| 1189 | struct bt_field_common *src) |
| 1190 | { |
| 1191 | int64_t i; |
| 1192 | struct bt_field_common_array *array_src = BT_FROM_COMMON(src); |
| 1193 | struct bt_field_common_array *array_dst; |
| 1194 | |
| 1195 | BT_LOGD("Copying array field: src-field-addr=%p", src); |
| 1196 | array_dst = (void *) bt_field_create((void *) src->type); |
| 1197 | if (!array_dst) { |
| 1198 | goto error; |
| 1199 | } |
| 1200 | |
| 1201 | g_ptr_array_set_size(array_dst->elements, array_src->elements->len); |
| 1202 | for (i = 0; i < array_src->elements->len; i++) { |
| 1203 | struct bt_field_common *field = |
| 1204 | g_ptr_array_index(array_src->elements, i); |
| 1205 | struct bt_field_common *field_copy = NULL; |
| 1206 | |
| 1207 | if (field) { |
| 1208 | BT_LOGD("Copying array field's element field: field-addr=%p, " |
| 1209 | "index=%" PRId64, field, i); |
| 1210 | field_copy = (void *) bt_field_copy((void *) field); |
| 1211 | if (!field_copy) { |
| 1212 | BT_LOGE("Cannot copy array field's element field: " |
| 1213 | "src-field-addr=%p, index=%" PRId64, |
| 1214 | field, i); |
| 1215 | goto error; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | g_ptr_array_index(array_dst->elements, i) = field_copy; |
| 1220 | } |
| 1221 | |
| 1222 | BT_LOGD_STR("Copied array field."); |
| 1223 | goto end; |
| 1224 | |
| 1225 | error: |
| 1226 | BT_PUT(array_dst); |
| 1227 | |
| 1228 | end: |
| 1229 | return BT_TO_COMMON(array_dst); |
| 1230 | } |
| 1231 | |
| 1232 | static |
| 1233 | struct bt_field_common *bt_field_sequence_copy_recursive( |
| 1234 | struct bt_field_common *src) |
| 1235 | { |
| 1236 | int ret = 0; |
| 1237 | int64_t i; |
| 1238 | struct bt_field_common_sequence *sequence_src = BT_FROM_COMMON(src); |
| 1239 | struct bt_field_common_sequence *sequence_dst; |
| 1240 | struct bt_field_common *src_length; |
| 1241 | struct bt_field_common *dst_length; |
| 1242 | |
| 1243 | BT_LOGD("Copying sequence field: src-field-addr=%p", src); |
| 1244 | sequence_dst = (void *) bt_field_create((void *) src->type); |
| 1245 | if (!sequence_dst) { |
| 1246 | goto error; |
| 1247 | } |
| 1248 | |
| 1249 | src_length = bt_field_common_sequence_borrow_length(src); |
| 1250 | if (!src_length) { |
| 1251 | /* no length set yet: keep destination sequence empty */ |
| 1252 | goto end; |
| 1253 | } |
| 1254 | |
| 1255 | /* copy source length */ |
| 1256 | BT_LOGD_STR("Copying sequence field's length field."); |
| 1257 | dst_length = (void *) bt_field_copy((void *) src_length); |
| 1258 | if (!dst_length) { |
| 1259 | BT_LOGE_STR("Cannot copy sequence field's length field."); |
| 1260 | goto error; |
| 1261 | } |
| 1262 | |
| 1263 | /* this will initialize the destination sequence's internal array */ |
| 1264 | ret = bt_field_common_sequence_set_length( |
| 1265 | BT_TO_COMMON(sequence_dst), dst_length); |
| 1266 | bt_put(dst_length); |
| 1267 | if (ret) { |
| 1268 | BT_LOGE("Cannot set sequence field copy's length field: " |
| 1269 | "dst-length-field-addr=%p", dst_length); |
| 1270 | goto error; |
| 1271 | } |
| 1272 | |
| 1273 | BT_ASSERT(sequence_dst->elements->len == sequence_src->elements->len); |
| 1274 | |
| 1275 | for (i = 0; i < sequence_src->elements->len; i++) { |
| 1276 | struct bt_field_common *field = |
| 1277 | g_ptr_array_index(sequence_src->elements, i); |
| 1278 | struct bt_field_common *field_copy = NULL; |
| 1279 | |
| 1280 | if (field) { |
| 1281 | BT_LOGD("Copying sequence field's element field: field-addr=%p, " |
| 1282 | "index=%" PRId64, field, i); |
| 1283 | field_copy = (void *) bt_field_copy((void *) field); |
| 1284 | if (!field_copy) { |
| 1285 | BT_LOGE("Cannot copy sequence field's element field: " |
| 1286 | "src-field-addr=%p, index=%" PRId64, |
| 1287 | field, i); |
| 1288 | goto error; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | g_ptr_array_index(sequence_dst->elements, i) = field_copy; |
| 1293 | } |
| 1294 | |
| 1295 | BT_LOGD_STR("Copied sequence field."); |
| 1296 | goto end; |
| 1297 | |
| 1298 | error: |
| 1299 | BT_PUT(sequence_dst); |
| 1300 | |
| 1301 | end: |
| 1302 | return BT_TO_COMMON(sequence_dst); |
| 1303 | } |
| 1304 | |
| 1305 | static |
| 1306 | struct bt_field_common *bt_field_string_copy(struct bt_field_common *src) |
| 1307 | { |
| 1308 | struct bt_field_common_string *string_src = BT_FROM_COMMON(src); |
| 1309 | struct bt_field_common_string *string_dst; |
| 1310 | |
| 1311 | BT_LOGD("Copying string field: src-field-addr=%p", src); |
| 1312 | string_dst = (void *) bt_field_create((void *) src->type); |
| 1313 | if (!string_dst) { |
| 1314 | goto error; |
| 1315 | } |
| 1316 | |
| 1317 | if (string_src->payload) { |
| 1318 | string_dst->payload = g_string_new(string_src->payload->str); |
| 1319 | if (!string_dst->payload) { |
| 1320 | BT_LOGE_STR("Failed to allocate a GString."); |
| 1321 | goto error; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | BT_LOGD_STR("Copied string field."); |
| 1326 | goto end; |
| 1327 | |
| 1328 | error: |
| 1329 | BT_PUT(string_dst); |
| 1330 | |
| 1331 | end: |
| 1332 | return BT_TO_COMMON(string_dst); |
| 1333 | } |
| 1334 | |
| 1335 | BT_HIDDEN |
| 1336 | void bt_field_common_generic_freeze(struct bt_field_common *field) |
| 1337 | { |
| 1338 | field->frozen = true; |
| 1339 | } |
| 1340 | |
| 1341 | BT_HIDDEN |
| 1342 | void bt_field_common_enumeration_freeze_recursive(struct bt_field_common *field) |
| 1343 | { |
| 1344 | struct bt_field_common_enumeration *enum_field = BT_FROM_COMMON(field); |
| 1345 | |
| 1346 | BT_LOGD("Freezing enumeration field object: addr=%p", field); |
| 1347 | BT_LOGD("Freezing enumeration field object's contained payload field: payload-field-addr=%p", enum_field->payload); |
| 1348 | bt_field_common_freeze_recursive(enum_field->payload); |
| 1349 | bt_field_common_generic_freeze(field); |
| 1350 | } |
| 1351 | |
| 1352 | BT_HIDDEN |
| 1353 | void bt_field_common_structure_freeze_recursive(struct bt_field_common *field) |
| 1354 | { |
| 1355 | int64_t i; |
| 1356 | struct bt_field_common_structure *structure_field = |
| 1357 | BT_FROM_COMMON(field); |
| 1358 | |
| 1359 | BT_LOGD("Freezing structure field object: addr=%p", field); |
| 1360 | |
| 1361 | for (i = 0; i < structure_field->fields->len; i++) { |
| 1362 | struct bt_field_common *field = |
| 1363 | g_ptr_array_index(structure_field->fields, i); |
| 1364 | |
| 1365 | BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64, |
| 1366 | field, i); |
| 1367 | bt_field_common_freeze_recursive(field); |
| 1368 | } |
| 1369 | |
| 1370 | bt_field_common_generic_freeze(field); |
| 1371 | } |
| 1372 | |
| 1373 | BT_HIDDEN |
| 1374 | void bt_field_common_variant_freeze_recursive(struct bt_field_common *field) |
| 1375 | { |
| 1376 | struct bt_field_common_variant *variant_field = BT_FROM_COMMON(field); |
| 1377 | |
| 1378 | BT_LOGD("Freezing variant field object: addr=%p", field); |
| 1379 | BT_LOGD("Freezing variant field object's tag field: tag-field-addr=%p", variant_field->tag); |
| 1380 | bt_field_common_freeze_recursive(variant_field->tag); |
| 1381 | BT_LOGD("Freezing variant field object's payload field: payload-field-addr=%p", variant_field->payload); |
| 1382 | bt_field_common_freeze_recursive(variant_field->payload); |
| 1383 | bt_field_common_generic_freeze(field); |
| 1384 | } |
| 1385 | |
| 1386 | BT_HIDDEN |
| 1387 | void bt_field_common_array_freeze_recursive(struct bt_field_common *field) |
| 1388 | { |
| 1389 | int64_t i; |
| 1390 | struct bt_field_common_array *array_field = BT_FROM_COMMON(field); |
| 1391 | |
| 1392 | BT_LOGD("Freezing array field object: addr=%p", field); |
| 1393 | |
| 1394 | for (i = 0; i < array_field->elements->len; i++) { |
| 1395 | struct bt_field_common *elem_field = |
| 1396 | g_ptr_array_index(array_field->elements, i); |
| 1397 | |
| 1398 | BT_LOGD("Freezing array field object's element field: " |
| 1399 | "element-field-addr=%p, index=%" PRId64, |
| 1400 | elem_field, i); |
| 1401 | bt_field_common_freeze_recursive(elem_field); |
| 1402 | } |
| 1403 | |
| 1404 | bt_field_common_generic_freeze(field); |
| 1405 | } |
| 1406 | |
| 1407 | BT_HIDDEN |
| 1408 | void bt_field_common_sequence_freeze_recursive(struct bt_field_common *field) |
| 1409 | { |
| 1410 | int64_t i; |
| 1411 | struct bt_field_common_sequence *sequence_field = |
| 1412 | BT_FROM_COMMON(field); |
| 1413 | |
| 1414 | BT_LOGD("Freezing sequence field object: addr=%p", field); |
| 1415 | BT_LOGD("Freezing sequence field object's length field: length-field-addr=%p", |
| 1416 | sequence_field->length); |
| 1417 | bt_field_common_freeze_recursive(sequence_field->length); |
| 1418 | |
| 1419 | for (i = 0; i < sequence_field->elements->len; i++) { |
| 1420 | struct bt_field_common *elem_field = |
| 1421 | g_ptr_array_index(sequence_field->elements, i); |
| 1422 | |
| 1423 | BT_LOGD("Freezing sequence field object's element field: " |
| 1424 | "element-field-addr=%p, index=%" PRId64, |
| 1425 | elem_field, i); |
| 1426 | bt_field_common_freeze_recursive(elem_field); |
| 1427 | } |
| 1428 | |
| 1429 | bt_field_common_generic_freeze(field); |
| 1430 | } |
| 1431 | |
| 1432 | BT_HIDDEN |
| 1433 | void _bt_field_common_freeze_recursive(struct bt_field_common *field) |
| 1434 | { |
| 1435 | if (!field) { |
| 1436 | goto end; |
| 1437 | } |
| 1438 | |
| 1439 | if (field->frozen) { |
| 1440 | goto end; |
| 1441 | } |
| 1442 | |
| 1443 | BT_LOGD("Freezing field object: addr=%p", field); |
| 1444 | BT_ASSERT(field_type_common_has_known_id(field->type)); |
| 1445 | BT_ASSERT(field->methods->freeze); |
| 1446 | field->methods->freeze(field); |
| 1447 | |
| 1448 | end: |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | BT_HIDDEN |
| 1453 | bt_bool bt_field_common_generic_is_set(struct bt_field_common *field) |
| 1454 | { |
| 1455 | return field && field->payload_set; |
| 1456 | } |
| 1457 | |
| 1458 | BT_HIDDEN |
| 1459 | bt_bool bt_field_common_enumeration_is_set_recursive( |
| 1460 | struct bt_field_common *field) |
| 1461 | { |
| 1462 | bt_bool is_set = BT_FALSE; |
| 1463 | struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field); |
| 1464 | |
| 1465 | BT_ASSERT(field); |
| 1466 | |
| 1467 | if (!enumeration->payload) { |
| 1468 | goto end; |
| 1469 | } |
| 1470 | |
| 1471 | is_set = bt_field_common_is_set_recursive(enumeration->payload); |
| 1472 | |
| 1473 | end: |
| 1474 | return is_set; |
| 1475 | } |
| 1476 | |
| 1477 | BT_HIDDEN |
| 1478 | bt_bool bt_field_common_structure_is_set_recursive( |
| 1479 | struct bt_field_common *field) |
| 1480 | { |
| 1481 | bt_bool is_set = BT_FALSE; |
| 1482 | size_t i; |
| 1483 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); |
| 1484 | |
| 1485 | BT_ASSERT(field); |
| 1486 | |
| 1487 | for (i = 0; i < structure->fields->len; i++) { |
| 1488 | is_set = bt_field_common_is_set_recursive( |
| 1489 | structure->fields->pdata[i]); |
| 1490 | if (!is_set) { |
| 1491 | goto end; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | end: |
| 1496 | return is_set; |
| 1497 | } |
| 1498 | |
| 1499 | BT_HIDDEN |
| 1500 | bt_bool bt_field_common_variant_is_set_recursive(struct bt_field_common *field) |
| 1501 | { |
| 1502 | struct bt_field_common_variant *variant = BT_FROM_COMMON(field); |
| 1503 | |
| 1504 | BT_ASSERT(field); |
| 1505 | return bt_field_common_is_set_recursive(variant->payload); |
| 1506 | } |
| 1507 | |
| 1508 | BT_HIDDEN |
| 1509 | bt_bool bt_field_common_array_is_set_recursive(struct bt_field_common *field) |
| 1510 | { |
| 1511 | size_t i; |
| 1512 | bt_bool is_set = BT_FALSE; |
| 1513 | struct bt_field_common_array *array = BT_FROM_COMMON(field); |
| 1514 | |
| 1515 | BT_ASSERT(field); |
| 1516 | |
| 1517 | for (i = 0; i < array->elements->len; i++) { |
| 1518 | is_set = bt_field_common_is_set_recursive(array->elements->pdata[i]); |
| 1519 | if (!is_set) { |
| 1520 | goto end; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | end: |
| 1525 | return is_set; |
| 1526 | } |
| 1527 | |
| 1528 | BT_HIDDEN |
| 1529 | bt_bool bt_field_common_sequence_is_set_recursive(struct bt_field_common *field) |
| 1530 | { |
| 1531 | size_t i; |
| 1532 | bt_bool is_set = BT_FALSE; |
| 1533 | struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field); |
| 1534 | |
| 1535 | BT_ASSERT(field); |
| 1536 | |
| 1537 | if (!sequence->elements) { |
| 1538 | goto end; |
| 1539 | } |
| 1540 | |
| 1541 | for (i = 0; i < sequence->elements->len; i++) { |
| 1542 | is_set = bt_field_common_is_set_recursive( |
| 1543 | sequence->elements->pdata[i]); |
| 1544 | if (!is_set) { |
| 1545 | goto end; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | end: |
| 1550 | return is_set; |
| 1551 | } |