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