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