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