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