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