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