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