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