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