| 1 | /* |
| 2 | * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "LIB/VALUE" |
| 24 | #include "lib/lib-logging.h" |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <string.h> |
| 29 | #include <inttypes.h> |
| 30 | #include "compat/compiler.h" |
| 31 | #include "common/common.h" |
| 32 | #include <babeltrace2/value-const.h> |
| 33 | #include <babeltrace2/value.h> |
| 34 | #include "compat/glib.h" |
| 35 | #include <babeltrace2/types.h> |
| 36 | #include "lib/assert-pre.h" |
| 37 | #include "lib/value.h" |
| 38 | #include "common/assert.h" |
| 39 | |
| 40 | #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base)) |
| 41 | #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base)) |
| 42 | #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base)) |
| 43 | #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base)) |
| 44 | #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base)) |
| 45 | #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base)) |
| 46 | |
| 47 | #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \ |
| 48 | BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \ |
| 49 | "Value has the wrong type ID: expected-type=%s, " \ |
| 50 | "%![value-]+v", bt_common_value_type_string(_type), \ |
| 51 | (_value)) |
| 52 | |
| 53 | #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \ |
| 54 | BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \ |
| 55 | ": %!+v", (_value)) |
| 56 | |
| 57 | #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \ |
| 58 | BT_ASSERT_PRE((_index) < (_count), \ |
| 59 | "Index is out of bound: " \ |
| 60 | "index=%" PRIu64 ", count=%u", (_index), (_count)); |
| 61 | |
| 62 | static |
| 63 | void bt_value_null_instance_release_func(struct bt_object *obj) |
| 64 | { |
| 65 | BT_LOGW("Releasing the null value singleton: addr=%p", obj); |
| 66 | } |
| 67 | |
| 68 | static |
| 69 | struct bt_value bt_value_null_instance = { |
| 70 | .base = { |
| 71 | .is_shared = true, |
| 72 | .ref_count = 1, |
| 73 | .release_func = bt_value_null_instance_release_func, |
| 74 | .spec_release_func = NULL, |
| 75 | .parent_is_owner_listener_func = NULL, |
| 76 | .parent = NULL, |
| 77 | }, |
| 78 | .type = BT_VALUE_TYPE_NULL, |
| 79 | .frozen = BT_TRUE, |
| 80 | }; |
| 81 | |
| 82 | struct bt_value *const bt_value_null = &bt_value_null_instance; |
| 83 | |
| 84 | static |
| 85 | void bt_value_destroy(struct bt_object *obj); |
| 86 | |
| 87 | static |
| 88 | void bt_value_string_destroy(struct bt_value *object) |
| 89 | { |
| 90 | g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE); |
| 91 | BT_VALUE_TO_STRING(object)->gstr = NULL; |
| 92 | } |
| 93 | |
| 94 | static |
| 95 | void bt_value_array_destroy(struct bt_value *object) |
| 96 | { |
| 97 | /* |
| 98 | * Pointer array's registered value destructor will take care |
| 99 | * of putting each contained object. |
| 100 | */ |
| 101 | g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE); |
| 102 | BT_VALUE_TO_ARRAY(object)->garray = NULL; |
| 103 | } |
| 104 | |
| 105 | static |
| 106 | void bt_value_map_destroy(struct bt_value *object) |
| 107 | { |
| 108 | /* |
| 109 | * Hash table's registered value destructor will take care of |
| 110 | * putting each contained object. Keys are GQuarks and cannot |
| 111 | * be destroyed anyway. |
| 112 | */ |
| 113 | g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght); |
| 114 | BT_VALUE_TO_MAP(object)->ght = NULL; |
| 115 | } |
| 116 | |
| 117 | static |
| 118 | void (* const destroy_funcs[])(struct bt_value *) = { |
| 119 | [BT_VALUE_TYPE_NULL] = NULL, |
| 120 | [BT_VALUE_TYPE_BOOL] = NULL, |
| 121 | [BT_VALUE_TYPE_UNSIGNED_INTEGER] = NULL, |
| 122 | [BT_VALUE_TYPE_SIGNED_INTEGER] = NULL, |
| 123 | [BT_VALUE_TYPE_REAL] = NULL, |
| 124 | [BT_VALUE_TYPE_STRING] = bt_value_string_destroy, |
| 125 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy, |
| 126 | [BT_VALUE_TYPE_MAP] = bt_value_map_destroy, |
| 127 | }; |
| 128 | |
| 129 | static |
| 130 | struct bt_value *bt_value_null_copy(const struct bt_value *null_obj) |
| 131 | { |
| 132 | return (void *) bt_value_null; |
| 133 | } |
| 134 | |
| 135 | static |
| 136 | struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj) |
| 137 | { |
| 138 | return bt_value_bool_create_init( |
| 139 | BT_VALUE_TO_BOOL(bool_obj)->value); |
| 140 | } |
| 141 | |
| 142 | static inline |
| 143 | struct bt_value *bt_value_integer_create_init(enum bt_value_type type, |
| 144 | uint64_t uval); |
| 145 | |
| 146 | static |
| 147 | struct bt_value *bt_value_integer_copy( |
| 148 | const struct bt_value *integer_obj) |
| 149 | { |
| 150 | return bt_value_integer_create_init(integer_obj->type, |
| 151 | BT_VALUE_TO_INTEGER(integer_obj)->value.u); |
| 152 | } |
| 153 | |
| 154 | static |
| 155 | struct bt_value *bt_value_real_copy(const struct bt_value *real_obj) |
| 156 | { |
| 157 | return bt_value_real_create_init( |
| 158 | BT_VALUE_TO_REAL(real_obj)->value); |
| 159 | } |
| 160 | |
| 161 | static |
| 162 | struct bt_value *bt_value_string_copy(const struct bt_value *string_obj) |
| 163 | { |
| 164 | return bt_value_string_create_init( |
| 165 | BT_VALUE_TO_STRING(string_obj)->gstr->str); |
| 166 | } |
| 167 | |
| 168 | static |
| 169 | struct bt_value *bt_value_array_copy(const struct bt_value *array_obj) |
| 170 | { |
| 171 | int i; |
| 172 | int ret; |
| 173 | struct bt_value *copy_obj; |
| 174 | struct bt_value_array *typed_array_obj; |
| 175 | |
| 176 | BT_LOGD("Copying array value: addr=%p", array_obj); |
| 177 | typed_array_obj = BT_VALUE_TO_ARRAY(array_obj); |
| 178 | copy_obj = bt_value_array_create(); |
| 179 | if (!copy_obj) { |
| 180 | BT_LOGE_STR("Cannot create empty array value."); |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | for (i = 0; i < typed_array_obj->garray->len; ++i) { |
| 185 | struct bt_value *element_obj_copy = NULL; |
| 186 | const struct bt_value *element_obj = |
| 187 | bt_value_array_borrow_element_by_index_const( |
| 188 | array_obj, i); |
| 189 | |
| 190 | BT_ASSERT(element_obj); |
| 191 | BT_LOGD("Copying array value's element: element-addr=%p, " |
| 192 | "index=%d", element_obj, i); |
| 193 | ret = bt_value_copy(element_obj, &element_obj_copy); |
| 194 | if (ret) { |
| 195 | BT_LOGE("Cannot copy array value's element: " |
| 196 | "array-addr=%p, index=%d", |
| 197 | array_obj, i); |
| 198 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | BT_ASSERT(element_obj_copy); |
| 203 | ret = bt_value_array_append_element(copy_obj, |
| 204 | (void *) element_obj_copy); |
| 205 | BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy); |
| 206 | if (ret) { |
| 207 | BT_LOGE("Cannot append to array value: addr=%p", |
| 208 | array_obj); |
| 209 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
| 210 | goto end; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p", |
| 215 | array_obj, copy_obj); |
| 216 | |
| 217 | end: |
| 218 | return copy_obj; |
| 219 | } |
| 220 | |
| 221 | static |
| 222 | struct bt_value *bt_value_map_copy(const struct bt_value *map_obj) |
| 223 | { |
| 224 | int ret; |
| 225 | GHashTableIter iter; |
| 226 | gpointer key, element_obj; |
| 227 | struct bt_value *copy_obj; |
| 228 | struct bt_value *element_obj_copy = NULL; |
| 229 | struct bt_value_map *typed_map_obj; |
| 230 | |
| 231 | BT_LOGD("Copying map value: addr=%p", map_obj); |
| 232 | typed_map_obj = BT_VALUE_TO_MAP(map_obj); |
| 233 | copy_obj = bt_value_map_create(); |
| 234 | if (!copy_obj) { |
| 235 | goto end; |
| 236 | } |
| 237 | |
| 238 | g_hash_table_iter_init(&iter, typed_map_obj->ght); |
| 239 | |
| 240 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { |
| 241 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
| 242 | |
| 243 | BT_ASSERT(key_str); |
| 244 | BT_LOGD("Copying map value's element: element-addr=%p, " |
| 245 | "key=\"%s\"", element_obj, key_str); |
| 246 | ret = bt_value_copy(element_obj, &element_obj_copy); |
| 247 | if (ret) { |
| 248 | BT_LOGE("Cannot copy map value's element: " |
| 249 | "map-addr=%p, key=\"%s\"", |
| 250 | map_obj, key_str); |
| 251 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
| 252 | goto end; |
| 253 | } |
| 254 | |
| 255 | BT_ASSERT(element_obj_copy); |
| 256 | ret = bt_value_map_insert_entry(copy_obj, key_str, |
| 257 | (void *) element_obj_copy); |
| 258 | BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy); |
| 259 | if (ret) { |
| 260 | BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"", |
| 261 | map_obj, key_str); |
| 262 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
| 263 | goto end; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | BT_LOGD("Copied map value: addr=%p", map_obj); |
| 268 | |
| 269 | end: |
| 270 | return copy_obj; |
| 271 | } |
| 272 | |
| 273 | static |
| 274 | struct bt_value *(* const copy_funcs[])(const struct bt_value *) = { |
| 275 | [BT_VALUE_TYPE_NULL] = bt_value_null_copy, |
| 276 | [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy, |
| 277 | [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_copy, |
| 278 | [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_copy, |
| 279 | [BT_VALUE_TYPE_REAL] = bt_value_real_copy, |
| 280 | [BT_VALUE_TYPE_STRING] = bt_value_string_copy, |
| 281 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy, |
| 282 | [BT_VALUE_TYPE_MAP] = bt_value_map_copy, |
| 283 | }; |
| 284 | |
| 285 | static |
| 286 | bt_bool bt_value_null_compare(const struct bt_value *object_a, |
| 287 | const struct bt_value *object_b) |
| 288 | { |
| 289 | /* |
| 290 | * Always BT_TRUE since bt_value_compare() already checks if both |
| 291 | * object_a and object_b have the same type, and in the case of |
| 292 | * null value objects, they're always the same if it is so. |
| 293 | */ |
| 294 | return BT_TRUE; |
| 295 | } |
| 296 | |
| 297 | static |
| 298 | bt_bool bt_value_bool_compare(const struct bt_value *object_a, |
| 299 | const struct bt_value *object_b) |
| 300 | { |
| 301 | if (BT_VALUE_TO_BOOL(object_a)->value != |
| 302 | BT_VALUE_TO_BOOL(object_b)->value) { |
| 303 | BT_LOGV("Boolean value objects are different: " |
| 304 | "bool-a-val=%d, bool-b-val=%d", |
| 305 | BT_VALUE_TO_BOOL(object_a)->value, |
| 306 | BT_VALUE_TO_BOOL(object_b)->value); |
| 307 | return BT_FALSE; |
| 308 | } |
| 309 | |
| 310 | return BT_TRUE; |
| 311 | } |
| 312 | |
| 313 | static |
| 314 | bt_bool bt_value_integer_compare(const struct bt_value *object_a, |
| 315 | const struct bt_value *object_b) |
| 316 | { |
| 317 | if (BT_VALUE_TO_INTEGER(object_a)->value.u != |
| 318 | BT_VALUE_TO_INTEGER(object_b)->value.u) { |
| 319 | if (object_a->type == BT_VALUE_TYPE_UNSIGNED_INTEGER) { |
| 320 | BT_LOGV("Unsigned integer value objects are different: " |
| 321 | "int-a-val=%" PRIu64 ", int-b-val=%" PRIu64, |
| 322 | BT_VALUE_TO_INTEGER(object_a)->value.u, |
| 323 | BT_VALUE_TO_INTEGER(object_b)->value.u); |
| 324 | } else { |
| 325 | BT_LOGV("Signed integer value objects are different: " |
| 326 | "int-a-val=%" PRId64 ", int-b-val=%" PRId64, |
| 327 | BT_VALUE_TO_INTEGER(object_a)->value.i, |
| 328 | BT_VALUE_TO_INTEGER(object_b)->value.i); |
| 329 | } |
| 330 | |
| 331 | return BT_FALSE; |
| 332 | } |
| 333 | |
| 334 | return BT_TRUE; |
| 335 | } |
| 336 | |
| 337 | static |
| 338 | bt_bool bt_value_real_compare(const struct bt_value *object_a, |
| 339 | const struct bt_value *object_b) |
| 340 | { |
| 341 | if (BT_VALUE_TO_REAL(object_a)->value != |
| 342 | BT_VALUE_TO_REAL(object_b)->value) { |
| 343 | BT_LOGV("Real number value objects are different: " |
| 344 | "real-a-val=%f, real-b-val=%f", |
| 345 | BT_VALUE_TO_REAL(object_a)->value, |
| 346 | BT_VALUE_TO_REAL(object_b)->value); |
| 347 | return BT_FALSE; |
| 348 | } |
| 349 | |
| 350 | return BT_TRUE; |
| 351 | } |
| 352 | |
| 353 | static |
| 354 | bt_bool bt_value_string_compare(const struct bt_value *object_a, |
| 355 | const struct bt_value *object_b) |
| 356 | { |
| 357 | if (strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str, |
| 358 | BT_VALUE_TO_STRING(object_b)->gstr->str) != 0) { |
| 359 | BT_LOGV("String value objects are different: " |
| 360 | "string-a-val=\"%s\", string-b-val=\"%s\"", |
| 361 | BT_VALUE_TO_STRING(object_a)->gstr->str, |
| 362 | BT_VALUE_TO_STRING(object_b)->gstr->str); |
| 363 | return BT_FALSE; |
| 364 | } |
| 365 | |
| 366 | return BT_TRUE; |
| 367 | } |
| 368 | |
| 369 | static |
| 370 | bt_bool bt_value_array_compare(const struct bt_value *object_a, |
| 371 | const struct bt_value *object_b) |
| 372 | { |
| 373 | int i; |
| 374 | bt_bool ret = BT_TRUE; |
| 375 | const struct bt_value_array *array_obj_a = |
| 376 | BT_VALUE_TO_ARRAY(object_a); |
| 377 | |
| 378 | if (bt_value_array_get_size(object_a) != |
| 379 | bt_value_array_get_size(object_b)) { |
| 380 | BT_LOGV("Array values are different: size mismatch " |
| 381 | "value-a-addr=%p, value-b-addr=%p, " |
| 382 | "value-a-size=%" PRId64 ", value-b-size=%" PRId64, |
| 383 | object_a, object_b, |
| 384 | bt_value_array_get_size(object_a), |
| 385 | bt_value_array_get_size(object_b)); |
| 386 | ret = BT_FALSE; |
| 387 | goto end; |
| 388 | } |
| 389 | |
| 390 | for (i = 0; i < array_obj_a->garray->len; ++i) { |
| 391 | const struct bt_value *element_obj_a; |
| 392 | const struct bt_value *element_obj_b; |
| 393 | |
| 394 | element_obj_a = bt_value_array_borrow_element_by_index_const( |
| 395 | object_a, i); |
| 396 | element_obj_b = bt_value_array_borrow_element_by_index_const( |
| 397 | object_b, i); |
| 398 | |
| 399 | if (!bt_value_compare(element_obj_a, element_obj_b)) { |
| 400 | BT_LOGV("Array values's elements are different: " |
| 401 | "value-a-addr=%p, value-b-addr=%p, index=%d", |
| 402 | element_obj_a, element_obj_b, i); |
| 403 | ret = BT_FALSE; |
| 404 | goto end; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | end: |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static |
| 413 | bt_bool bt_value_map_compare(const struct bt_value *object_a, |
| 414 | const struct bt_value *object_b) |
| 415 | { |
| 416 | bt_bool ret = BT_TRUE; |
| 417 | GHashTableIter iter; |
| 418 | gpointer key, element_obj_a; |
| 419 | const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a); |
| 420 | |
| 421 | if (bt_value_map_get_size(object_a) != |
| 422 | bt_value_map_get_size(object_b)) { |
| 423 | BT_LOGV("Map values are different: size mismatch " |
| 424 | "value-a-addr=%p, value-b-addr=%p, " |
| 425 | "value-a-size=%" PRId64 ", value-b-size=%" PRId64, |
| 426 | object_a, object_b, |
| 427 | bt_value_map_get_size(object_a), |
| 428 | bt_value_map_get_size(object_b)); |
| 429 | ret = BT_FALSE; |
| 430 | goto end; |
| 431 | } |
| 432 | |
| 433 | g_hash_table_iter_init(&iter, map_obj_a->ght); |
| 434 | |
| 435 | while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) { |
| 436 | const struct bt_value *element_obj_b; |
| 437 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
| 438 | |
| 439 | element_obj_b = bt_value_map_borrow_entry_value_const(object_b, |
| 440 | key_str); |
| 441 | |
| 442 | if (!bt_value_compare(element_obj_a, element_obj_b)) { |
| 443 | BT_LOGV("Map values's elements are different: " |
| 444 | "value-a-addr=%p, value-b-addr=%p, key=\"%s\"", |
| 445 | element_obj_a, element_obj_b, key_str); |
| 446 | ret = BT_FALSE; |
| 447 | goto end; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | end: |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static |
| 456 | bt_bool (* const compare_funcs[])(const struct bt_value *, |
| 457 | const struct bt_value *) = { |
| 458 | [BT_VALUE_TYPE_NULL] = bt_value_null_compare, |
| 459 | [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare, |
| 460 | [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_compare, |
| 461 | [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_compare, |
| 462 | [BT_VALUE_TYPE_REAL] = bt_value_real_compare, |
| 463 | [BT_VALUE_TYPE_STRING] = bt_value_string_compare, |
| 464 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare, |
| 465 | [BT_VALUE_TYPE_MAP] = bt_value_map_compare, |
| 466 | }; |
| 467 | |
| 468 | static |
| 469 | void bt_value_null_freeze(struct bt_value *object) |
| 470 | { |
| 471 | } |
| 472 | |
| 473 | static |
| 474 | void bt_value_generic_freeze(struct bt_value *object) |
| 475 | { |
| 476 | object->frozen = BT_TRUE; |
| 477 | } |
| 478 | |
| 479 | static |
| 480 | void bt_value_array_freeze(struct bt_value *object) |
| 481 | { |
| 482 | int i; |
| 483 | struct bt_value_array *typed_array_obj = |
| 484 | BT_VALUE_TO_ARRAY(object); |
| 485 | |
| 486 | for (i = 0; i < typed_array_obj->garray->len; ++i) { |
| 487 | bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i)); |
| 488 | } |
| 489 | |
| 490 | bt_value_generic_freeze(object); |
| 491 | } |
| 492 | |
| 493 | static |
| 494 | void bt_value_map_freeze(struct bt_value *object) |
| 495 | { |
| 496 | GHashTableIter iter; |
| 497 | gpointer key, element_obj; |
| 498 | const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object); |
| 499 | |
| 500 | g_hash_table_iter_init(&iter, map_obj->ght); |
| 501 | |
| 502 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { |
| 503 | bt_value_freeze(element_obj); |
| 504 | } |
| 505 | |
| 506 | bt_value_generic_freeze(object); |
| 507 | } |
| 508 | |
| 509 | static |
| 510 | void (* const freeze_funcs[])(struct bt_value *) = { |
| 511 | [BT_VALUE_TYPE_NULL] = bt_value_null_freeze, |
| 512 | [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze, |
| 513 | [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_generic_freeze, |
| 514 | [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_generic_freeze, |
| 515 | [BT_VALUE_TYPE_REAL] = bt_value_generic_freeze, |
| 516 | [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze, |
| 517 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze, |
| 518 | [BT_VALUE_TYPE_MAP] = bt_value_map_freeze, |
| 519 | }; |
| 520 | |
| 521 | static |
| 522 | void bt_value_destroy(struct bt_object *obj) |
| 523 | { |
| 524 | struct bt_value *value; |
| 525 | |
| 526 | value = container_of(obj, struct bt_value, base); |
| 527 | BT_LOGD("Destroying value: addr=%p", value); |
| 528 | |
| 529 | if (bt_value_is_null(value)) { |
| 530 | BT_LOGD_STR("Not destroying the null value singleton."); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | if (destroy_funcs[value->type]) { |
| 535 | destroy_funcs[value->type](value); |
| 536 | } |
| 537 | |
| 538 | g_free(value); |
| 539 | } |
| 540 | |
| 541 | BT_HIDDEN |
| 542 | enum bt_value_status _bt_value_freeze(const struct bt_value *c_object) |
| 543 | { |
| 544 | const struct bt_value *object = (void *) c_object; |
| 545 | enum bt_value_status ret = BT_VALUE_STATUS_OK; |
| 546 | |
| 547 | BT_ASSERT(object); |
| 548 | |
| 549 | if (object->frozen) { |
| 550 | goto end; |
| 551 | } |
| 552 | |
| 553 | BT_LOGD("Freezing value: addr=%p", object); |
| 554 | freeze_funcs[object->type]((void *) object); |
| 555 | |
| 556 | end: |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | enum bt_value_type bt_value_get_type(const struct bt_value *object) |
| 561 | { |
| 562 | BT_ASSERT_PRE_NON_NULL(object, "Value object"); |
| 563 | return object->type; |
| 564 | } |
| 565 | |
| 566 | static |
| 567 | struct bt_value bt_value_create_base(enum bt_value_type type) |
| 568 | { |
| 569 | struct bt_value value; |
| 570 | |
| 571 | value.type = type; |
| 572 | value.frozen = BT_FALSE; |
| 573 | bt_object_init_shared(&value.base, bt_value_destroy); |
| 574 | return value; |
| 575 | } |
| 576 | |
| 577 | struct bt_value *bt_value_bool_create_init(bt_bool val) |
| 578 | { |
| 579 | struct bt_value_bool *bool_obj; |
| 580 | |
| 581 | BT_LOGD("Creating boolean value object: val=%d", val); |
| 582 | bool_obj = g_new0(struct bt_value_bool, 1); |
| 583 | if (!bool_obj) { |
| 584 | BT_LOGE_STR("Failed to allocate one boolean value object."); |
| 585 | goto end; |
| 586 | } |
| 587 | |
| 588 | bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL); |
| 589 | bool_obj->value = val; |
| 590 | BT_LOGD("Created boolean value object: addr=%p", bool_obj); |
| 591 | |
| 592 | end: |
| 593 | return (void *) bool_obj; |
| 594 | } |
| 595 | |
| 596 | struct bt_value *bt_value_bool_create(void) |
| 597 | { |
| 598 | return bt_value_bool_create_init(BT_FALSE); |
| 599 | } |
| 600 | |
| 601 | static inline |
| 602 | struct bt_value *bt_value_integer_create_init(enum bt_value_type type, |
| 603 | uint64_t uval) |
| 604 | { |
| 605 | struct bt_value_integer *integer_obj; |
| 606 | |
| 607 | BT_ASSERT(type == BT_VALUE_TYPE_UNSIGNED_INTEGER || |
| 608 | type == BT_VALUE_TYPE_SIGNED_INTEGER); |
| 609 | |
| 610 | if (type == BT_VALUE_TYPE_UNSIGNED_INTEGER) { |
| 611 | BT_LOGD("Creating unsigned integer value object: val=%" PRIu64, |
| 612 | uval); |
| 613 | } else { |
| 614 | BT_LOGD("Creating signed integer value object: val=%" PRId64, |
| 615 | (int64_t) uval); |
| 616 | } |
| 617 | |
| 618 | integer_obj = g_new0(struct bt_value_integer, 1); |
| 619 | if (!integer_obj) { |
| 620 | BT_LOGE_STR("Failed to allocate one integer value object."); |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | integer_obj->base = bt_value_create_base(type); |
| 625 | integer_obj->value.u = uval; |
| 626 | BT_LOGD("Created %ssigned integer value object: addr=%p", |
| 627 | type == BT_VALUE_TYPE_UNSIGNED_INTEGER ? "un" : "", |
| 628 | integer_obj); |
| 629 | |
| 630 | end: |
| 631 | return (void *) integer_obj; |
| 632 | } |
| 633 | |
| 634 | struct bt_value *bt_value_unsigned_integer_create_init(uint64_t val) |
| 635 | { |
| 636 | return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER, |
| 637 | val); |
| 638 | } |
| 639 | |
| 640 | struct bt_value *bt_value_unsigned_integer_create(void) |
| 641 | { |
| 642 | return bt_value_unsigned_integer_create_init(0); |
| 643 | } |
| 644 | |
| 645 | struct bt_value *bt_value_signed_integer_create_init(int64_t val) |
| 646 | { |
| 647 | return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER, |
| 648 | (uint64_t) val); |
| 649 | } |
| 650 | |
| 651 | struct bt_value *bt_value_signed_integer_create(void) |
| 652 | { |
| 653 | return bt_value_signed_integer_create_init(0); |
| 654 | } |
| 655 | |
| 656 | struct bt_value *bt_value_real_create_init(double val) |
| 657 | { |
| 658 | struct bt_value_real *real_obj; |
| 659 | |
| 660 | BT_LOGD("Creating real number value object: val=%f", val); |
| 661 | real_obj = g_new0(struct bt_value_real, 1); |
| 662 | if (!real_obj) { |
| 663 | BT_LOGE_STR("Failed to allocate one real number value object."); |
| 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | real_obj->base = bt_value_create_base(BT_VALUE_TYPE_REAL); |
| 668 | real_obj->value = val; |
| 669 | BT_LOGD("Created real number value object: addr=%p", |
| 670 | real_obj); |
| 671 | |
| 672 | end: |
| 673 | return (void *) real_obj; |
| 674 | } |
| 675 | |
| 676 | struct bt_value *bt_value_real_create(void) |
| 677 | { |
| 678 | return bt_value_real_create_init(0.); |
| 679 | } |
| 680 | |
| 681 | struct bt_value *bt_value_string_create_init(const char *val) |
| 682 | { |
| 683 | struct bt_value_string *string_obj = NULL; |
| 684 | |
| 685 | if (!val) { |
| 686 | BT_LOGW_STR("Invalid parameter: value is NULL."); |
| 687 | goto end; |
| 688 | } |
| 689 | |
| 690 | BT_LOGD("Creating string value object: val-len=%zu", strlen(val)); |
| 691 | string_obj = g_new0(struct bt_value_string, 1); |
| 692 | if (!string_obj) { |
| 693 | BT_LOGE_STR("Failed to allocate one string object."); |
| 694 | goto end; |
| 695 | } |
| 696 | |
| 697 | string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING); |
| 698 | string_obj->gstr = g_string_new(val); |
| 699 | if (!string_obj->gstr) { |
| 700 | BT_LOGE_STR("Failed to allocate a GString."); |
| 701 | g_free(string_obj); |
| 702 | string_obj = NULL; |
| 703 | goto end; |
| 704 | } |
| 705 | |
| 706 | BT_LOGD("Created string value object: addr=%p", |
| 707 | string_obj); |
| 708 | |
| 709 | end: |
| 710 | return (void *) string_obj; |
| 711 | } |
| 712 | |
| 713 | struct bt_value *bt_value_string_create(void) |
| 714 | { |
| 715 | return bt_value_string_create_init(""); |
| 716 | } |
| 717 | |
| 718 | struct bt_value *bt_value_array_create(void) |
| 719 | { |
| 720 | struct bt_value_array *array_obj; |
| 721 | |
| 722 | BT_LOGD_STR("Creating empty array value object."); |
| 723 | array_obj = g_new0(struct bt_value_array, 1); |
| 724 | if (!array_obj) { |
| 725 | BT_LOGE_STR("Failed to allocate one array object."); |
| 726 | goto end; |
| 727 | } |
| 728 | |
| 729 | array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY); |
| 730 | array_obj->garray = bt_g_ptr_array_new_full(0, |
| 731 | (GDestroyNotify) bt_object_put_ref); |
| 732 | if (!array_obj->garray) { |
| 733 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 734 | g_free(array_obj); |
| 735 | array_obj = NULL; |
| 736 | goto end; |
| 737 | } |
| 738 | |
| 739 | BT_LOGD("Created array value object: addr=%p", |
| 740 | array_obj); |
| 741 | |
| 742 | end: |
| 743 | return (void *) array_obj; |
| 744 | } |
| 745 | |
| 746 | struct bt_value *bt_value_map_create(void) |
| 747 | { |
| 748 | struct bt_value_map *map_obj; |
| 749 | |
| 750 | BT_LOGD_STR("Creating empty map value object."); |
| 751 | map_obj = g_new0(struct bt_value_map, 1); |
| 752 | if (!map_obj) { |
| 753 | BT_LOGE_STR("Failed to allocate one map object."); |
| 754 | goto end; |
| 755 | } |
| 756 | |
| 757 | map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP); |
| 758 | map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
| 759 | NULL, (GDestroyNotify) bt_object_put_ref); |
| 760 | if (!map_obj->ght) { |
| 761 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 762 | g_free(map_obj); |
| 763 | map_obj = NULL; |
| 764 | goto end; |
| 765 | } |
| 766 | |
| 767 | BT_LOGD("Created map value object: addr=%p", |
| 768 | map_obj); |
| 769 | |
| 770 | end: |
| 771 | return (void *) map_obj; |
| 772 | } |
| 773 | |
| 774 | bt_bool bt_value_bool_get(const struct bt_value *bool_obj) |
| 775 | { |
| 776 | BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object"); |
| 777 | BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL); |
| 778 | return BT_VALUE_TO_BOOL(bool_obj)->value; |
| 779 | } |
| 780 | |
| 781 | void bt_value_bool_set(struct bt_value *bool_obj, bt_bool val) |
| 782 | { |
| 783 | BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object"); |
| 784 | BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL); |
| 785 | BT_ASSERT_PRE_VALUE_HOT(bool_obj, "Value object"); |
| 786 | BT_VALUE_TO_BOOL(bool_obj)->value = val; |
| 787 | BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d", |
| 788 | bool_obj, val); |
| 789 | } |
| 790 | |
| 791 | uint64_t bt_value_unsigned_integer_get(const struct bt_value *integer_obj) |
| 792 | { |
| 793 | BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); |
| 794 | BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, |
| 795 | BT_VALUE_TYPE_UNSIGNED_INTEGER); |
| 796 | return BT_VALUE_TO_INTEGER(integer_obj)->value.u; |
| 797 | } |
| 798 | |
| 799 | int64_t bt_value_signed_integer_get(const struct bt_value *integer_obj) |
| 800 | { |
| 801 | BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); |
| 802 | BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, |
| 803 | BT_VALUE_TYPE_SIGNED_INTEGER); |
| 804 | return BT_VALUE_TO_INTEGER(integer_obj)->value.i; |
| 805 | } |
| 806 | |
| 807 | static inline |
| 808 | void bt_value_integer_set(struct bt_value *integer_obj, |
| 809 | enum bt_value_type expected_type, uint64_t uval) |
| 810 | { |
| 811 | BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); |
| 812 | BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, expected_type); |
| 813 | BT_ASSERT_PRE_VALUE_HOT(integer_obj, "Value object"); |
| 814 | BT_VALUE_TO_INTEGER(integer_obj)->value.u = uval; |
| 815 | } |
| 816 | |
| 817 | void bt_value_unsigned_integer_set(struct bt_value *integer_obj, |
| 818 | uint64_t val) |
| 819 | { |
| 820 | bt_value_integer_set(integer_obj, BT_VALUE_TYPE_UNSIGNED_INTEGER, val); |
| 821 | BT_LOGV("Set unsigned integer value's raw value: " |
| 822 | "value-addr=%p, value=%" PRIu64, integer_obj, val); |
| 823 | } |
| 824 | |
| 825 | void bt_value_signed_integer_set(struct bt_value *integer_obj, |
| 826 | int64_t val) |
| 827 | { |
| 828 | bt_value_integer_set(integer_obj, BT_VALUE_TYPE_SIGNED_INTEGER, |
| 829 | (uint64_t) val); |
| 830 | BT_LOGV("Set signed integer value's raw value: " |
| 831 | "value-addr=%p, value=%" PRId64, integer_obj, val); |
| 832 | } |
| 833 | |
| 834 | double bt_value_real_get(const struct bt_value *real_obj) |
| 835 | { |
| 836 | BT_ASSERT_PRE_NON_NULL(real_obj, "Value object"); |
| 837 | BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL); |
| 838 | return BT_VALUE_TO_REAL(real_obj)->value; |
| 839 | } |
| 840 | |
| 841 | void bt_value_real_set(struct bt_value *real_obj, double val) |
| 842 | { |
| 843 | BT_ASSERT_PRE_NON_NULL(real_obj, "Value object"); |
| 844 | BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL); |
| 845 | BT_ASSERT_PRE_VALUE_HOT(real_obj, "Value object"); |
| 846 | BT_VALUE_TO_REAL(real_obj)->value = val; |
| 847 | BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f", |
| 848 | real_obj, val); |
| 849 | } |
| 850 | |
| 851 | const char *bt_value_string_get(const struct bt_value *string_obj) |
| 852 | { |
| 853 | BT_ASSERT_PRE_NON_NULL(string_obj, "Value object"); |
| 854 | BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING); |
| 855 | return BT_VALUE_TO_STRING(string_obj)->gstr->str; |
| 856 | } |
| 857 | |
| 858 | enum bt_value_status bt_value_string_set( |
| 859 | struct bt_value *string_obj, const char *val) |
| 860 | { |
| 861 | BT_ASSERT_PRE_NON_NULL(string_obj, "Value object"); |
| 862 | BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING); |
| 863 | BT_ASSERT_PRE_VALUE_HOT(string_obj, "Value object"); |
| 864 | g_string_assign(BT_VALUE_TO_STRING(string_obj)->gstr, val); |
| 865 | BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p", |
| 866 | string_obj, val); |
| 867 | return BT_VALUE_STATUS_OK; |
| 868 | } |
| 869 | |
| 870 | uint64_t bt_value_array_get_size(const struct bt_value *array_obj) |
| 871 | { |
| 872 | BT_ASSERT_PRE_NON_NULL(array_obj, "Value object"); |
| 873 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); |
| 874 | return (uint64_t) BT_VALUE_TO_ARRAY(array_obj)->garray->len; |
| 875 | } |
| 876 | |
| 877 | struct bt_value *bt_value_array_borrow_element_by_index( |
| 878 | struct bt_value *array_obj, uint64_t index) |
| 879 | { |
| 880 | struct bt_value_array *typed_array_obj = |
| 881 | BT_VALUE_TO_ARRAY(array_obj); |
| 882 | |
| 883 | BT_ASSERT_PRE_NON_NULL(array_obj, "Value object"); |
| 884 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); |
| 885 | BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index, |
| 886 | typed_array_obj->garray->len); |
| 887 | return g_ptr_array_index(typed_array_obj->garray, index); |
| 888 | } |
| 889 | |
| 890 | const struct bt_value *bt_value_array_borrow_element_by_index_const( |
| 891 | const struct bt_value *array_obj, |
| 892 | uint64_t index) |
| 893 | { |
| 894 | return bt_value_array_borrow_element_by_index( |
| 895 | (void *) array_obj, index); |
| 896 | } |
| 897 | |
| 898 | enum bt_value_status bt_value_array_append_element( |
| 899 | struct bt_value *array_obj, |
| 900 | struct bt_value *element_obj) |
| 901 | { |
| 902 | struct bt_value_array *typed_array_obj = |
| 903 | BT_VALUE_TO_ARRAY(array_obj); |
| 904 | |
| 905 | BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object"); |
| 906 | BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object"); |
| 907 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); |
| 908 | BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object"); |
| 909 | g_ptr_array_add(typed_array_obj->garray, element_obj); |
| 910 | bt_object_get_ref(element_obj); |
| 911 | BT_LOGV("Appended element to array value: array-value-addr=%p, " |
| 912 | "element-value-addr=%p, new-size=%u", |
| 913 | array_obj, element_obj, typed_array_obj->garray->len); |
| 914 | return BT_VALUE_STATUS_OK; |
| 915 | } |
| 916 | |
| 917 | enum bt_value_status bt_value_array_append_bool_element( |
| 918 | struct bt_value *array_obj, bt_bool val) |
| 919 | { |
| 920 | enum bt_value_status ret; |
| 921 | struct bt_value *bool_obj = NULL; |
| 922 | |
| 923 | bool_obj = bt_value_bool_create_init(val); |
| 924 | ret = bt_value_array_append_element(array_obj, |
| 925 | (void *) bool_obj); |
| 926 | bt_object_put_ref(bool_obj); |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | enum bt_value_status bt_value_array_append_unsigned_integer_element( |
| 931 | struct bt_value *array_obj, uint64_t val) |
| 932 | { |
| 933 | enum bt_value_status ret; |
| 934 | struct bt_value *integer_obj = NULL; |
| 935 | |
| 936 | integer_obj = bt_value_unsigned_integer_create_init(val); |
| 937 | ret = bt_value_array_append_element(array_obj, |
| 938 | (void *) integer_obj); |
| 939 | bt_object_put_ref(integer_obj); |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | enum bt_value_status bt_value_array_append_signed_integer_element( |
| 944 | struct bt_value *array_obj, int64_t val) |
| 945 | { |
| 946 | enum bt_value_status ret; |
| 947 | struct bt_value *integer_obj = NULL; |
| 948 | |
| 949 | integer_obj = bt_value_signed_integer_create_init(val); |
| 950 | ret = bt_value_array_append_element(array_obj, |
| 951 | (void *) integer_obj); |
| 952 | bt_object_put_ref(integer_obj); |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | enum bt_value_status bt_value_array_append_real_element( |
| 957 | struct bt_value *array_obj, double val) |
| 958 | { |
| 959 | enum bt_value_status ret; |
| 960 | struct bt_value *real_obj = NULL; |
| 961 | |
| 962 | real_obj = bt_value_real_create_init(val); |
| 963 | ret = bt_value_array_append_element(array_obj, |
| 964 | (void *) real_obj); |
| 965 | bt_object_put_ref(real_obj); |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | enum bt_value_status bt_value_array_append_string_element( |
| 970 | struct bt_value *array_obj, const char *val) |
| 971 | { |
| 972 | enum bt_value_status ret; |
| 973 | struct bt_value *string_obj = NULL; |
| 974 | |
| 975 | string_obj = bt_value_string_create_init(val); |
| 976 | ret = bt_value_array_append_element(array_obj, |
| 977 | (void *) string_obj); |
| 978 | bt_object_put_ref(string_obj); |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | enum bt_value_status bt_value_array_append_empty_array_element( |
| 983 | struct bt_value *array_obj) |
| 984 | { |
| 985 | enum bt_value_status ret; |
| 986 | struct bt_value *empty_array_obj = NULL; |
| 987 | |
| 988 | empty_array_obj = bt_value_array_create(); |
| 989 | ret = bt_value_array_append_element(array_obj, |
| 990 | (void *) empty_array_obj); |
| 991 | bt_object_put_ref(empty_array_obj); |
| 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | enum bt_value_status bt_value_array_append_empty_map_element( |
| 996 | struct bt_value *array_obj) |
| 997 | { |
| 998 | enum bt_value_status ret; |
| 999 | struct bt_value *map_obj = NULL; |
| 1000 | |
| 1001 | map_obj = bt_value_map_create(); |
| 1002 | ret = bt_value_array_append_element(array_obj, |
| 1003 | (void *) map_obj); |
| 1004 | bt_object_put_ref(map_obj); |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | enum bt_value_status bt_value_array_set_element_by_index( |
| 1009 | struct bt_value *array_obj, uint64_t index, |
| 1010 | struct bt_value *element_obj) |
| 1011 | { |
| 1012 | struct bt_value_array *typed_array_obj = |
| 1013 | BT_VALUE_TO_ARRAY(array_obj); |
| 1014 | |
| 1015 | BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object"); |
| 1016 | BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object"); |
| 1017 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); |
| 1018 | BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object"); |
| 1019 | BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index, |
| 1020 | typed_array_obj->garray->len); |
| 1021 | bt_object_put_ref(g_ptr_array_index(typed_array_obj->garray, index)); |
| 1022 | g_ptr_array_index(typed_array_obj->garray, index) = element_obj; |
| 1023 | bt_object_get_ref(element_obj); |
| 1024 | BT_LOGV("Set array value's element: array-value-addr=%p, " |
| 1025 | "index=%" PRIu64 ", element-value-addr=%p", |
| 1026 | array_obj, index, element_obj); |
| 1027 | return BT_VALUE_STATUS_OK; |
| 1028 | } |
| 1029 | |
| 1030 | uint64_t bt_value_map_get_size(const struct bt_value *map_obj) |
| 1031 | { |
| 1032 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
| 1033 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); |
| 1034 | return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj)->ght); |
| 1035 | } |
| 1036 | |
| 1037 | struct bt_value *bt_value_map_borrow_entry_value(struct bt_value *map_obj, |
| 1038 | const char *key) |
| 1039 | { |
| 1040 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
| 1041 | BT_ASSERT_PRE_NON_NULL(key, "Key"); |
| 1042 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); |
| 1043 | return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght, |
| 1044 | GUINT_TO_POINTER(g_quark_from_string(key))); |
| 1045 | } |
| 1046 | |
| 1047 | const struct bt_value *bt_value_map_borrow_entry_value_const( |
| 1048 | const struct bt_value *map_obj, const char *key) |
| 1049 | { |
| 1050 | return bt_value_map_borrow_entry_value((void *) map_obj, key); |
| 1051 | } |
| 1052 | |
| 1053 | bt_bool bt_value_map_has_entry(const struct bt_value *map_obj, const char *key) |
| 1054 | { |
| 1055 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
| 1056 | BT_ASSERT_PRE_NON_NULL(key, "Key"); |
| 1057 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); |
| 1058 | return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj)->ght, |
| 1059 | GUINT_TO_POINTER(g_quark_from_string(key))); |
| 1060 | } |
| 1061 | |
| 1062 | enum bt_value_status bt_value_map_insert_entry( |
| 1063 | struct bt_value *map_obj, |
| 1064 | const char *key, struct bt_value *element_obj) |
| 1065 | { |
| 1066 | BT_ASSERT_PRE_NON_NULL(map_obj, "Map value object"); |
| 1067 | BT_ASSERT_PRE_NON_NULL(key, "Key"); |
| 1068 | BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object"); |
| 1069 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); |
| 1070 | BT_ASSERT_PRE_VALUE_HOT(map_obj, "Map value object"); |
| 1071 | g_hash_table_insert(BT_VALUE_TO_MAP(map_obj)->ght, |
| 1072 | GUINT_TO_POINTER(g_quark_from_string(key)), element_obj); |
| 1073 | bt_object_get_ref(element_obj); |
| 1074 | BT_LOGV("Inserted value into map value: map-value-addr=%p, " |
| 1075 | "key=\"%s\", element-value-addr=%p", |
| 1076 | map_obj, key, element_obj); |
| 1077 | return BT_VALUE_STATUS_OK; |
| 1078 | } |
| 1079 | |
| 1080 | enum bt_value_status bt_value_map_insert_bool_entry( |
| 1081 | struct bt_value *map_obj, const char *key, bt_bool val) |
| 1082 | { |
| 1083 | enum bt_value_status ret; |
| 1084 | struct bt_value *bool_obj = NULL; |
| 1085 | |
| 1086 | bool_obj = bt_value_bool_create_init(val); |
| 1087 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1088 | (void *) bool_obj); |
| 1089 | bt_object_put_ref(bool_obj); |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | enum bt_value_status bt_value_map_insert_unsigned_integer_entry( |
| 1094 | struct bt_value *map_obj, const char *key, uint64_t val) |
| 1095 | { |
| 1096 | enum bt_value_status ret; |
| 1097 | struct bt_value *integer_obj = NULL; |
| 1098 | |
| 1099 | integer_obj = bt_value_unsigned_integer_create_init(val); |
| 1100 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1101 | (void *) integer_obj); |
| 1102 | bt_object_put_ref(integer_obj); |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | enum bt_value_status bt_value_map_insert_signed_integer_entry( |
| 1107 | struct bt_value *map_obj, const char *key, int64_t val) |
| 1108 | { |
| 1109 | enum bt_value_status ret; |
| 1110 | struct bt_value *integer_obj = NULL; |
| 1111 | |
| 1112 | integer_obj = bt_value_signed_integer_create_init(val); |
| 1113 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1114 | (void *) integer_obj); |
| 1115 | bt_object_put_ref(integer_obj); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | enum bt_value_status bt_value_map_insert_real_entry( |
| 1120 | struct bt_value *map_obj, const char *key, double val) |
| 1121 | { |
| 1122 | enum bt_value_status ret; |
| 1123 | struct bt_value *real_obj = NULL; |
| 1124 | |
| 1125 | real_obj = bt_value_real_create_init(val); |
| 1126 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1127 | (void *) real_obj); |
| 1128 | bt_object_put_ref(real_obj); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | enum bt_value_status bt_value_map_insert_string_entry( |
| 1133 | struct bt_value *map_obj, const char *key, |
| 1134 | const char *val) |
| 1135 | { |
| 1136 | enum bt_value_status ret; |
| 1137 | struct bt_value *string_obj = NULL; |
| 1138 | |
| 1139 | string_obj = bt_value_string_create_init(val); |
| 1140 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1141 | (void *) string_obj); |
| 1142 | bt_object_put_ref(string_obj); |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
| 1146 | enum bt_value_status bt_value_map_insert_empty_array_entry( |
| 1147 | struct bt_value *map_obj, const char *key) |
| 1148 | { |
| 1149 | enum bt_value_status ret; |
| 1150 | struct bt_value *array_obj = NULL; |
| 1151 | |
| 1152 | array_obj = bt_value_array_create(); |
| 1153 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1154 | (void *) array_obj); |
| 1155 | bt_object_put_ref(array_obj); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | enum bt_value_status bt_value_map_insert_empty_map_entry( |
| 1160 | struct bt_value *map_obj, const char *key) |
| 1161 | { |
| 1162 | enum bt_value_status ret; |
| 1163 | struct bt_value *empty_map_obj = NULL; |
| 1164 | |
| 1165 | empty_map_obj = bt_value_map_create(); |
| 1166 | ret = bt_value_map_insert_entry(map_obj, key, |
| 1167 | (void *) empty_map_obj); |
| 1168 | bt_object_put_ref(empty_map_obj); |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | enum bt_value_status bt_value_map_foreach_entry(struct bt_value *map_obj, |
| 1173 | bt_value_map_foreach_entry_func func, void *data) |
| 1174 | { |
| 1175 | enum bt_value_status ret = BT_VALUE_STATUS_OK; |
| 1176 | gpointer key, element_obj; |
| 1177 | GHashTableIter iter; |
| 1178 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); |
| 1179 | |
| 1180 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
| 1181 | BT_ASSERT_PRE_NON_NULL(func, "Callback"); |
| 1182 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); |
| 1183 | g_hash_table_iter_init(&iter, typed_map_obj->ght); |
| 1184 | |
| 1185 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { |
| 1186 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
| 1187 | |
| 1188 | if (!func(key_str, element_obj, data)) { |
| 1189 | BT_LOGV("User canceled the loop: key=\"%s\", " |
| 1190 | "value-addr=%p, data=%p", |
| 1191 | key_str, element_obj, data); |
| 1192 | ret = BT_VALUE_STATUS_CANCELED; |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | return ret; |
| 1198 | } |
| 1199 | |
| 1200 | enum bt_value_status bt_value_map_foreach_entry_const( |
| 1201 | const struct bt_value *map_obj, |
| 1202 | bt_value_map_foreach_entry_const_func func, void *data) |
| 1203 | { |
| 1204 | return bt_value_map_foreach_entry((void *) map_obj, |
| 1205 | (bt_value_map_foreach_entry_func) func, data); |
| 1206 | } |
| 1207 | |
| 1208 | struct extend_map_element_data { |
| 1209 | struct bt_value *extended_obj; |
| 1210 | enum bt_value_status status; |
| 1211 | }; |
| 1212 | |
| 1213 | static |
| 1214 | bt_bool extend_map_element(const char *key, |
| 1215 | const struct bt_value *extension_obj_elem, void *data) |
| 1216 | { |
| 1217 | bt_bool ret = BT_TRUE; |
| 1218 | struct extend_map_element_data *extend_data = data; |
| 1219 | struct bt_value *extension_obj_elem_copy = NULL; |
| 1220 | |
| 1221 | /* Copy object which is to replace the current one */ |
| 1222 | extend_data->status = bt_value_copy(extension_obj_elem, |
| 1223 | &extension_obj_elem_copy); |
| 1224 | if (extend_data->status) { |
| 1225 | BT_LOGE("Cannot copy map element: addr=%p", |
| 1226 | extension_obj_elem); |
| 1227 | goto error; |
| 1228 | } |
| 1229 | |
| 1230 | BT_ASSERT(extension_obj_elem_copy); |
| 1231 | |
| 1232 | /* Replace in extended object */ |
| 1233 | extend_data->status = bt_value_map_insert_entry( |
| 1234 | extend_data->extended_obj, key, |
| 1235 | (void *) extension_obj_elem_copy); |
| 1236 | if (extend_data->status) { |
| 1237 | BT_LOGE("Cannot replace value in extended value: key=\"%s\", " |
| 1238 | "extended-value-addr=%p, element-value-addr=%p", |
| 1239 | key, extend_data->extended_obj, |
| 1240 | extension_obj_elem_copy); |
| 1241 | goto error; |
| 1242 | } |
| 1243 | |
| 1244 | goto end; |
| 1245 | |
| 1246 | error: |
| 1247 | BT_ASSERT(extend_data->status != BT_VALUE_STATUS_OK); |
| 1248 | ret = BT_FALSE; |
| 1249 | |
| 1250 | end: |
| 1251 | BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy); |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | enum bt_value_status bt_value_map_extend( |
| 1256 | const struct bt_value *base_map_obj, |
| 1257 | const struct bt_value *extension_obj, |
| 1258 | struct bt_value **extended_map_obj) |
| 1259 | { |
| 1260 | struct extend_map_element_data extend_data = { |
| 1261 | .extended_obj = NULL, |
| 1262 | .status = BT_VALUE_STATUS_OK, |
| 1263 | }; |
| 1264 | |
| 1265 | BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object"); |
| 1266 | BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object"); |
| 1267 | BT_ASSERT_PRE_NON_NULL(extended_map_obj, |
| 1268 | "Extended value object (output)"); |
| 1269 | BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP); |
| 1270 | BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP); |
| 1271 | BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p", |
| 1272 | base_map_obj, extension_obj); |
| 1273 | *extended_map_obj = NULL; |
| 1274 | |
| 1275 | /* Create copy of base map object to start with */ |
| 1276 | extend_data.status = bt_value_copy(base_map_obj, extended_map_obj); |
| 1277 | if (extend_data.status) { |
| 1278 | BT_LOGE("Cannot copy base value: base-value-addr=%p", |
| 1279 | base_map_obj); |
| 1280 | goto error; |
| 1281 | } |
| 1282 | |
| 1283 | BT_ASSERT(extended_map_obj); |
| 1284 | |
| 1285 | /* |
| 1286 | * For each key in the extension map object, replace this key |
| 1287 | * in the copied map object. |
| 1288 | */ |
| 1289 | extend_data.extended_obj = *extended_map_obj; |
| 1290 | |
| 1291 | if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element, |
| 1292 | &extend_data)) { |
| 1293 | BT_LOGE("Cannot iterate on the extension object's elements: " |
| 1294 | "extension-value-addr=%p", extension_obj); |
| 1295 | goto error; |
| 1296 | } |
| 1297 | |
| 1298 | if (extend_data.status) { |
| 1299 | BT_LOGE("Failed to successfully iterate on the extension object's elements: " |
| 1300 | "extension-value-addr=%p", extension_obj); |
| 1301 | goto error; |
| 1302 | } |
| 1303 | |
| 1304 | BT_LOGD("Extended map value: extended-value-addr=%p", |
| 1305 | *extended_map_obj); |
| 1306 | goto end; |
| 1307 | |
| 1308 | error: |
| 1309 | BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj); |
| 1310 | *extended_map_obj = NULL; |
| 1311 | |
| 1312 | end: |
| 1313 | return extend_data.status; |
| 1314 | } |
| 1315 | |
| 1316 | enum bt_value_status bt_value_copy(const struct bt_value *object, |
| 1317 | struct bt_value **copy_obj) |
| 1318 | { |
| 1319 | enum bt_value_status status = BT_VALUE_STATUS_OK; |
| 1320 | |
| 1321 | BT_ASSERT_PRE_NON_NULL(object, "Value object"); |
| 1322 | BT_ASSERT_PRE_NON_NULL(copy_obj, "Value object copy (output)"); |
| 1323 | BT_LOGD("Copying value object: addr=%p", object); |
| 1324 | *copy_obj = copy_funcs[object->type](object); |
| 1325 | if (*copy_obj) { |
| 1326 | BT_LOGD("Copied value object: copy-value-addr=%p", |
| 1327 | copy_obj); |
| 1328 | } else { |
| 1329 | status = BT_VALUE_STATUS_NOMEM; |
| 1330 | *copy_obj = NULL; |
| 1331 | BT_LOGE_STR("Failed to copy value object."); |
| 1332 | } |
| 1333 | |
| 1334 | return status; |
| 1335 | } |
| 1336 | |
| 1337 | bt_bool bt_value_compare(const struct bt_value *object_a, |
| 1338 | const struct bt_value *object_b) |
| 1339 | { |
| 1340 | bt_bool ret = BT_FALSE; |
| 1341 | |
| 1342 | BT_ASSERT_PRE_NON_NULL(object_a, "Value object A"); |
| 1343 | BT_ASSERT_PRE_NON_NULL(object_b, "Value object B"); |
| 1344 | |
| 1345 | if (object_a->type != object_b->type) { |
| 1346 | BT_LOGV("Values are different: type mismatch: " |
| 1347 | "value-a-addr=%p, value-b-addr=%p, " |
| 1348 | "value-a-type=%s, value-b-type=%s", |
| 1349 | object_a, object_b, |
| 1350 | bt_common_value_type_string(object_a->type), |
| 1351 | bt_common_value_type_string(object_b->type)); |
| 1352 | goto end; |
| 1353 | } |
| 1354 | |
| 1355 | ret = compare_funcs[object_a->type](object_a, object_b); |
| 1356 | |
| 1357 | end: |
| 1358 | return ret; |
| 1359 | } |
| 1360 | |
| 1361 | void bt_value_get_ref(const struct bt_value *value) |
| 1362 | { |
| 1363 | bt_object_get_ref(value); |
| 1364 | } |
| 1365 | |
| 1366 | void bt_value_put_ref(const struct bt_value *value) |
| 1367 | { |
| 1368 | bt_object_put_ref(value); |
| 1369 | } |