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