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