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