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