Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
de9dd397 | 2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
3 | * |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
fc25abce PP |
25 | #define BT_LOG_TAG "FIELDS" |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
3dca2276 | 28 | #include <babeltrace/assert-pre-internal.h> |
e5be10ef | 29 | #include <babeltrace/trace-ir/private-fields.h> |
56e18c4c PP |
30 | #include <babeltrace/trace-ir/fields.h> |
31 | #include <babeltrace/trace-ir/fields-internal.h> | |
5cd6d0e5 | 32 | #include <babeltrace/trace-ir/field-classes-internal.h> |
83509119 | 33 | #include <babeltrace/object-internal.h> |
65300d60 | 34 | #include <babeltrace/object.h> |
3d9990ac PP |
35 | #include <babeltrace/compiler-internal.h> |
36 | #include <babeltrace/compat/fcntl-internal.h> | |
37 | #include <babeltrace/align-internal.h> | |
f6ccaed9 | 38 | #include <babeltrace/assert-internal.h> |
fc25abce | 39 | #include <inttypes.h> |
273b65be | 40 | |
cb6f1f7d | 41 | static |
44c440bc | 42 | void reset_single_field(struct bt_field *field); |
cb6f1f7d PP |
43 | |
44 | static | |
44c440bc | 45 | void reset_array_field(struct bt_field *field); |
cb6f1f7d PP |
46 | |
47 | static | |
44c440bc | 48 | void reset_structure_field(struct bt_field *field); |
cb6f1f7d PP |
49 | |
50 | static | |
44c440bc | 51 | void reset_variant_field(struct bt_field *field); |
cb6f1f7d PP |
52 | |
53 | static | |
44c440bc | 54 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
55 | |
56 | static | |
44c440bc | 57 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
58 | |
59 | static | |
44c440bc | 60 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
61 | |
62 | static | |
44c440bc | 63 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
64 | |
65 | static | |
44c440bc | 66 | bool single_field_is_set(struct bt_field *field); |
cb6f1f7d PP |
67 | |
68 | static | |
44c440bc | 69 | bool array_field_is_set(struct bt_field *field); |
cb6f1f7d PP |
70 | |
71 | static | |
44c440bc | 72 | bool structure_field_is_set(struct bt_field *field); |
cb6f1f7d PP |
73 | |
74 | static | |
44c440bc | 75 | bool variant_field_is_set(struct bt_field *field); |
cb6f1f7d PP |
76 | |
77 | static | |
44c440bc PP |
78 | struct bt_field_methods integer_field_methods = { |
79 | .set_is_frozen = set_single_field_is_frozen, | |
80 | .is_set = single_field_is_set, | |
81 | .reset = reset_single_field, | |
3dca2276 | 82 | }; |
273b65be | 83 | |
44c440bc PP |
84 | static |
85 | struct bt_field_methods real_field_methods = { | |
86 | .set_is_frozen = set_single_field_is_frozen, | |
87 | .is_set = single_field_is_set, | |
88 | .reset = reset_single_field, | |
12c8a1a3 JG |
89 | }; |
90 | ||
44c440bc PP |
91 | static |
92 | struct bt_field_methods string_field_methods = { | |
93 | .set_is_frozen = set_single_field_is_frozen, | |
94 | .is_set = single_field_is_set, | |
95 | .reset = reset_single_field, | |
273b65be JG |
96 | }; |
97 | ||
44c440bc PP |
98 | static |
99 | struct bt_field_methods structure_field_methods = { | |
100 | .set_is_frozen = set_structure_field_is_frozen, | |
101 | .is_set = structure_field_is_set, | |
102 | .reset = reset_structure_field, | |
87d43dc1 JG |
103 | }; |
104 | ||
44c440bc PP |
105 | static |
106 | struct bt_field_methods array_field_methods = { | |
107 | .set_is_frozen = set_array_field_is_frozen, | |
108 | .is_set = array_field_is_set, | |
109 | .reset = reset_array_field, | |
918be005 PP |
110 | }; |
111 | ||
76f869ab | 112 | static |
44c440bc PP |
113 | struct bt_field_methods variant_field_methods = { |
114 | .set_is_frozen = set_variant_field_is_frozen, | |
115 | .is_set = variant_field_is_set, | |
116 | .reset = reset_variant_field, | |
117 | }; | |
76f869ab | 118 | |
3dca2276 | 119 | static |
5cd6d0e5 | 120 | struct bt_field *create_integer_field(struct bt_field_class *); |
3dca2276 PP |
121 | |
122 | static | |
5cd6d0e5 | 123 | struct bt_field *create_real_field(struct bt_field_class *); |
3dca2276 PP |
124 | |
125 | static | |
5cd6d0e5 | 126 | struct bt_field *create_string_field(struct bt_field_class *); |
3dca2276 PP |
127 | |
128 | static | |
5cd6d0e5 | 129 | struct bt_field *create_structure_field(struct bt_field_class *); |
3dca2276 PP |
130 | |
131 | static | |
5cd6d0e5 | 132 | struct bt_field *create_static_array_field(struct bt_field_class *); |
f6ccaed9 | 133 | |
3dca2276 | 134 | static |
5cd6d0e5 | 135 | struct bt_field *create_dynamic_array_field(struct bt_field_class *); |
f6ccaed9 | 136 | |
3dca2276 | 137 | static |
5cd6d0e5 | 138 | struct bt_field *create_variant_field(struct bt_field_class *); |
f6ccaed9 | 139 | |
3dca2276 | 140 | static |
5cd6d0e5 | 141 | struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = { |
864cad70 PP |
142 | [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = create_integer_field, |
143 | [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = create_integer_field, | |
144 | [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = create_integer_field, | |
145 | [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = create_integer_field, | |
146 | [BT_FIELD_CLASS_TYPE_REAL] = create_real_field, | |
147 | [BT_FIELD_CLASS_TYPE_STRING] = create_string_field, | |
148 | [BT_FIELD_CLASS_TYPE_STRUCTURE] = create_structure_field, | |
149 | [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = create_static_array_field, | |
150 | [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = create_dynamic_array_field, | |
151 | [BT_FIELD_CLASS_TYPE_VARIANT] = create_variant_field, | |
3dca2276 | 152 | }; |
f6ccaed9 | 153 | |
312c056a | 154 | static |
44c440bc | 155 | void destroy_integer_field(struct bt_field *field); |
312c056a PP |
156 | |
157 | static | |
44c440bc | 158 | void destroy_real_field(struct bt_field *field); |
312c056a PP |
159 | |
160 | static | |
44c440bc | 161 | void destroy_string_field(struct bt_field *field); |
312c056a PP |
162 | |
163 | static | |
44c440bc | 164 | void destroy_structure_field(struct bt_field *field); |
312c056a PP |
165 | |
166 | static | |
44c440bc | 167 | void destroy_array_field(struct bt_field *field); |
312c056a PP |
168 | |
169 | static | |
44c440bc | 170 | void destroy_variant_field(struct bt_field *field); |
312c056a PP |
171 | |
172 | static | |
173 | void (* const field_destroy_funcs[])(struct bt_field *) = { | |
864cad70 PP |
174 | [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = destroy_integer_field, |
175 | [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = destroy_integer_field, | |
176 | [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = destroy_integer_field, | |
177 | [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = destroy_integer_field, | |
178 | [BT_FIELD_CLASS_TYPE_REAL] = destroy_real_field, | |
179 | [BT_FIELD_CLASS_TYPE_STRING] = destroy_string_field, | |
180 | [BT_FIELD_CLASS_TYPE_STRUCTURE] = destroy_structure_field, | |
181 | [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = destroy_array_field, | |
182 | [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = destroy_array_field, | |
183 | [BT_FIELD_CLASS_TYPE_VARIANT] = destroy_variant_field, | |
312c056a PP |
184 | }; |
185 | ||
5cd6d0e5 | 186 | struct bt_field_class *bt_field_borrow_class(struct bt_field *field) |
cb6f1f7d | 187 | { |
44c440bc | 188 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
5cd6d0e5 | 189 | return field->class; |
cb6f1f7d PP |
190 | } |
191 | ||
28e6ca8b | 192 | struct bt_private_field_class *bt_private_field_borrow_class( |
e5be10ef PP |
193 | struct bt_private_field *field) |
194 | { | |
195 | return (void *) bt_field_borrow_class((void *) field); | |
196 | } | |
197 | ||
864cad70 | 198 | enum bt_field_class_type bt_field_get_class_type(struct bt_field *field) |
cb6f1f7d | 199 | { |
44c440bc | 200 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 | 201 | return field->class->type; |
cb6f1f7d PP |
202 | } |
203 | ||
312c056a | 204 | BT_HIDDEN |
5cd6d0e5 | 205 | struct bt_field *bt_field_create(struct bt_field_class *fc) |
273b65be | 206 | { |
50842bdc | 207 | struct bt_field *field = NULL; |
44c440bc | 208 | |
5cd6d0e5 | 209 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 PP |
210 | BT_ASSERT(bt_field_class_has_known_type(fc)); |
211 | field = field_create_funcs[fc->type](fc); | |
273b65be | 212 | if (!field) { |
5cd6d0e5 PP |
213 | BT_LIB_LOGE("Cannot create field object from field classe: " |
214 | "%![fc-]+F", fc); | |
3dca2276 | 215 | goto end; |
273b65be JG |
216 | } |
217 | ||
3dca2276 PP |
218 | end: |
219 | return field; | |
273b65be JG |
220 | } |
221 | ||
44c440bc | 222 | static inline |
5cd6d0e5 | 223 | void init_field(struct bt_field *field, struct bt_field_class *fc, |
44c440bc | 224 | struct bt_field_methods *methods) |
cd95e351 | 225 | { |
44c440bc | 226 | BT_ASSERT(field); |
5cd6d0e5 | 227 | BT_ASSERT(fc); |
44c440bc PP |
228 | bt_object_init_unique(&field->base); |
229 | field->methods = methods; | |
65300d60 | 230 | field->class = bt_object_get_ref(fc); |
cd95e351 JG |
231 | } |
232 | ||
44c440bc | 233 | static |
5cd6d0e5 | 234 | struct bt_field *create_integer_field(struct bt_field_class *fc) |
4ebcc695 | 235 | { |
44c440bc PP |
236 | struct bt_field_integer *int_field; |
237 | ||
5cd6d0e5 | 238 | BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc); |
44c440bc PP |
239 | int_field = g_new0(struct bt_field_integer, 1); |
240 | if (!int_field) { | |
241 | BT_LOGE_STR("Failed to allocate one integer field."); | |
242 | goto end; | |
243 | } | |
244 | ||
5cd6d0e5 | 245 | init_field((void *) int_field, fc, &integer_field_methods); |
44c440bc PP |
246 | BT_LIB_LOGD("Created integer field object: %!+f", int_field); |
247 | ||
248 | end: | |
249 | return (void *) int_field; | |
4ebcc695 PP |
250 | } |
251 | ||
44c440bc | 252 | static |
5cd6d0e5 | 253 | struct bt_field *create_real_field(struct bt_field_class *fc) |
2e8876d3 | 254 | { |
44c440bc | 255 | struct bt_field_real *real_field; |
cb6f1f7d | 256 | |
5cd6d0e5 | 257 | BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc); |
44c440bc PP |
258 | real_field = g_new0(struct bt_field_real, 1); |
259 | if (!real_field) { | |
260 | BT_LOGE_STR("Failed to allocate one real field."); | |
261 | goto end; | |
262 | } | |
263 | ||
5cd6d0e5 | 264 | init_field((void *) real_field, fc, &real_field_methods); |
44c440bc PP |
265 | BT_LIB_LOGD("Created real field object: %!+f", real_field); |
266 | ||
267 | end: | |
268 | return (void *) real_field; | |
2e8876d3 PP |
269 | } |
270 | ||
44c440bc | 271 | static |
5cd6d0e5 | 272 | struct bt_field *create_string_field(struct bt_field_class *fc) |
273b65be | 273 | { |
44c440bc | 274 | struct bt_field_string *string_field; |
cb6f1f7d | 275 | |
5cd6d0e5 | 276 | BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc); |
44c440bc PP |
277 | string_field = g_new0(struct bt_field_string, 1); |
278 | if (!string_field) { | |
279 | BT_LOGE_STR("Failed to allocate one string field."); | |
280 | goto end; | |
281 | } | |
cb6f1f7d | 282 | |
5cd6d0e5 | 283 | init_field((void *) string_field, fc, &string_field_methods); |
44c440bc PP |
284 | string_field->buf = g_array_sized_new(FALSE, FALSE, |
285 | sizeof(char), 1); | |
286 | if (!string_field->buf) { | |
287 | BT_LOGE_STR("Failed to allocate a GArray."); | |
65300d60 | 288 | BT_OBJECT_PUT_REF_AND_RESET(string_field); |
44c440bc PP |
289 | goto end; |
290 | } | |
cb6f1f7d | 291 | |
44c440bc PP |
292 | g_array_index(string_field->buf, char, 0) = '\0'; |
293 | BT_LIB_LOGD("Created string field object: %!+f", string_field); | |
cb6f1f7d | 294 | |
44c440bc PP |
295 | end: |
296 | return (void *) string_field; | |
297 | } | |
cb6f1f7d | 298 | |
44c440bc | 299 | static inline |
5cd6d0e5 PP |
300 | int create_fields_from_named_field_classes( |
301 | struct bt_field_class_named_field_class_container *fc, | |
44c440bc PP |
302 | GPtrArray **fields) |
303 | { | |
304 | int ret = 0; | |
305 | uint64_t i; | |
cb6f1f7d | 306 | |
44c440bc PP |
307 | *fields = g_ptr_array_new_with_free_func( |
308 | (GDestroyNotify) bt_field_destroy); | |
309 | if (!*fields) { | |
310 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
311 | ret = -1; | |
312 | goto end; | |
cb6f1f7d PP |
313 | } |
314 | ||
5cd6d0e5 | 315 | g_ptr_array_set_size(*fields, fc->named_fcs->len); |
44c440bc | 316 | |
5cd6d0e5 | 317 | for (i = 0; i < fc->named_fcs->len; i++) { |
44c440bc | 318 | struct bt_field *field; |
5cd6d0e5 PP |
319 | struct bt_named_field_class *named_fc = |
320 | BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, i); | |
44c440bc | 321 | |
5cd6d0e5 | 322 | field = bt_field_create(named_fc->fc); |
44c440bc PP |
323 | if (!field) { |
324 | BT_LIB_LOGE("Failed to create structure member or variant option field: " | |
5cd6d0e5 PP |
325 | "name=\"%s\", %![fc-]+F", |
326 | named_fc->name->str, named_fc->fc); | |
44c440bc PP |
327 | ret = -1; |
328 | goto end; | |
329 | } | |
330 | ||
331 | g_ptr_array_index(*fields, i) = field; | |
332 | } | |
cb6f1f7d PP |
333 | |
334 | end: | |
335 | return ret; | |
273b65be JG |
336 | } |
337 | ||
44c440bc | 338 | static |
5cd6d0e5 | 339 | struct bt_field *create_structure_field(struct bt_field_class *fc) |
cd95e351 | 340 | { |
44c440bc | 341 | struct bt_field_structure *struct_field; |
cb6f1f7d | 342 | |
5cd6d0e5 | 343 | BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc); |
44c440bc PP |
344 | struct_field = g_new0(struct bt_field_structure, 1); |
345 | if (!struct_field) { | |
346 | BT_LOGE_STR("Failed to allocate one structure field."); | |
347 | goto end; | |
348 | } | |
fc25abce | 349 | |
5cd6d0e5 | 350 | init_field((void *) struct_field, fc, &structure_field_methods); |
44c440bc | 351 | |
5cd6d0e5 | 352 | if (create_fields_from_named_field_classes((void *) fc, |
44c440bc PP |
353 | &struct_field->fields)) { |
354 | BT_LIB_LOGE("Cannot create structure member fields: " | |
5cd6d0e5 | 355 | "%![fc-]+F", fc); |
65300d60 | 356 | BT_OBJECT_PUT_REF_AND_RESET(struct_field); |
44c440bc | 357 | goto end; |
cb6f1f7d PP |
358 | } |
359 | ||
44c440bc | 360 | BT_LIB_LOGD("Created structure field object: %!+f", struct_field); |
cb6f1f7d | 361 | |
44c440bc PP |
362 | end: |
363 | return (void *) struct_field; | |
cd95e351 JG |
364 | } |
365 | ||
44c440bc | 366 | static |
5cd6d0e5 | 367 | struct bt_field *create_variant_field(struct bt_field_class *fc) |
273b65be | 368 | { |
44c440bc | 369 | struct bt_field_variant *var_field; |
cb6f1f7d | 370 | |
5cd6d0e5 | 371 | BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc); |
44c440bc PP |
372 | var_field = g_new0(struct bt_field_variant, 1); |
373 | if (!var_field) { | |
374 | BT_LOGE_STR("Failed to allocate one variant field."); | |
375 | goto end; | |
376 | } | |
f6ccaed9 | 377 | |
5cd6d0e5 | 378 | init_field((void *) var_field, fc, &variant_field_methods); |
cb6f1f7d | 379 | |
5cd6d0e5 | 380 | if (create_fields_from_named_field_classes((void *) fc, |
44c440bc PP |
381 | &var_field->fields)) { |
382 | BT_LIB_LOGE("Cannot create variant member fields: " | |
5cd6d0e5 | 383 | "%![fc-]+F", fc); |
65300d60 | 384 | BT_OBJECT_PUT_REF_AND_RESET(var_field); |
44c440bc PP |
385 | goto end; |
386 | } | |
273b65be | 387 | |
44c440bc | 388 | BT_LIB_LOGD("Created variant field object: %!+f", var_field); |
cb6f1f7d | 389 | |
44c440bc PP |
390 | end: |
391 | return (void *) var_field; | |
cb6f1f7d PP |
392 | } |
393 | ||
394 | static inline | |
44c440bc | 395 | int init_array_field_fields(struct bt_field_array *array_field) |
cb6f1f7d PP |
396 | { |
397 | int ret = 0; | |
44c440bc | 398 | uint64_t i; |
5cd6d0e5 | 399 | struct bt_field_class_array *array_fc; |
cb6f1f7d | 400 | |
44c440bc | 401 | BT_ASSERT(array_field); |
5cd6d0e5 | 402 | array_fc = (void *) array_field->common.class; |
44c440bc PP |
403 | array_field->fields = g_ptr_array_sized_new(array_field->length); |
404 | if (!array_field->fields) { | |
405 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
cb6f1f7d PP |
406 | ret = -1; |
407 | goto end; | |
408 | } | |
409 | ||
44c440bc PP |
410 | g_ptr_array_set_free_func(array_field->fields, |
411 | (GDestroyNotify) bt_field_destroy); | |
412 | g_ptr_array_set_size(array_field->fields, array_field->length); | |
413 | ||
414 | for (i = 0; i < array_field->length; i++) { | |
415 | array_field->fields->pdata[i] = bt_field_create( | |
5cd6d0e5 | 416 | array_fc->element_fc); |
44c440bc PP |
417 | if (!array_field->fields->pdata[i]) { |
418 | BT_LIB_LOGE("Cannot create array field's element field: " | |
5cd6d0e5 | 419 | "index=%" PRIu64 ", %![fc-]+F", i, array_fc); |
44c440bc PP |
420 | ret = -1; |
421 | goto end; | |
422 | } | |
423 | } | |
cb6f1f7d PP |
424 | |
425 | end: | |
426 | return ret; | |
3f4a108d PP |
427 | } |
428 | ||
44c440bc | 429 | static |
5cd6d0e5 | 430 | struct bt_field *create_static_array_field(struct bt_field_class *fc) |
f78d67fb | 431 | { |
5cd6d0e5 | 432 | struct bt_field_class_static_array *array_fc = (void *) fc; |
44c440bc | 433 | struct bt_field_array *array_field; |
312c056a | 434 | |
5cd6d0e5 | 435 | BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc); |
44c440bc PP |
436 | array_field = g_new0(struct bt_field_array, 1); |
437 | if (!array_field) { | |
438 | BT_LOGE_STR("Failed to allocate one static array field."); | |
439 | goto end; | |
440 | } | |
f78d67fb | 441 | |
5cd6d0e5 PP |
442 | init_field((void *) array_field, fc, &array_field_methods); |
443 | array_field->length = array_fc->length; | |
cb6f1f7d | 444 | |
44c440bc PP |
445 | if (init_array_field_fields(array_field)) { |
446 | BT_LIB_LOGE("Cannot create static array fields: " | |
5cd6d0e5 | 447 | "%![fc-]+F", fc); |
65300d60 | 448 | BT_OBJECT_PUT_REF_AND_RESET(array_field); |
44c440bc PP |
449 | goto end; |
450 | } | |
312c056a | 451 | |
44c440bc | 452 | BT_LIB_LOGD("Created static array field object: %!+f", array_field); |
cb6f1f7d | 453 | |
44c440bc PP |
454 | end: |
455 | return (void *) array_field; | |
273b65be JG |
456 | } |
457 | ||
44c440bc | 458 | static |
5cd6d0e5 | 459 | struct bt_field *create_dynamic_array_field(struct bt_field_class *fc) |
cd95e351 | 460 | { |
44c440bc | 461 | struct bt_field_array *array_field; |
312c056a | 462 | |
5cd6d0e5 | 463 | BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc); |
44c440bc PP |
464 | array_field = g_new0(struct bt_field_array, 1); |
465 | if (!array_field) { | |
466 | BT_LOGE_STR("Failed to allocate one dynamic array field."); | |
467 | goto end; | |
468 | } | |
469 | ||
5cd6d0e5 | 470 | init_field((void *) array_field, fc, &array_field_methods); |
44c440bc PP |
471 | |
472 | if (init_array_field_fields(array_field)) { | |
473 | BT_LIB_LOGE("Cannot create dynamic array fields: " | |
5cd6d0e5 | 474 | "%![fc-]+F", fc); |
65300d60 | 475 | BT_OBJECT_PUT_REF_AND_RESET(array_field); |
44c440bc | 476 | goto end; |
cb6f1f7d PP |
477 | } |
478 | ||
44c440bc PP |
479 | BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field); |
480 | ||
481 | end: | |
482 | return (void *) array_field; | |
312c056a PP |
483 | } |
484 | ||
44c440bc | 485 | int64_t bt_field_signed_integer_get_value(struct bt_field *field) |
312c056a | 486 | { |
cb6f1f7d | 487 | struct bt_field_integer *int_field = (void *) field; |
312c056a | 488 | |
44c440bc PP |
489 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
490 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
491 | BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field, "Field"); | |
492 | return int_field->value.i; | |
cd95e351 JG |
493 | } |
494 | ||
e5be10ef PP |
495 | void bt_private_field_signed_integer_set_value( |
496 | struct bt_private_field *priv_field, int64_t value) | |
cd95e351 | 497 | { |
e5be10ef | 498 | struct bt_field *field = (void *) priv_field; |
44c440bc | 499 | struct bt_field_integer *int_field = (void *) field; |
312c056a | 500 | |
44c440bc PP |
501 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
502 | BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field, "Field"); | |
503 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); | |
504 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed( | |
5cd6d0e5 | 505 | ((struct bt_field_class_integer *) field->class)->range, value), |
44c440bc | 506 | "Value is out of bounds: value=%" PRId64 ", %![field-]+f, " |
5cd6d0e5 | 507 | "%![fc-]+F", value, field, field->class); |
44c440bc PP |
508 | int_field->value.i = value; |
509 | bt_field_set_single(field, true); | |
cd95e351 JG |
510 | } |
511 | ||
44c440bc | 512 | uint64_t bt_field_unsigned_integer_get_value(struct bt_field *field) |
273b65be | 513 | { |
44c440bc PP |
514 | struct bt_field_integer *int_field = (void *) field; |
515 | ||
516 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
517 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
518 | BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field, "Field"); | |
519 | return int_field->value.u; | |
273b65be JG |
520 | } |
521 | ||
e5be10ef PP |
522 | void bt_private_field_unsigned_integer_set_value( |
523 | struct bt_private_field *priv_field, uint64_t value) | |
cd95e351 | 524 | { |
e5be10ef | 525 | struct bt_field *field = (void *) priv_field; |
44c440bc | 526 | struct bt_field_integer *int_field = (void *) field; |
312c056a | 527 | |
44c440bc PP |
528 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
529 | BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field, "Field"); | |
530 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); | |
531 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned( | |
5cd6d0e5 | 532 | ((struct bt_field_class_integer *) field->class)->range, value), |
44c440bc | 533 | "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, " |
5cd6d0e5 | 534 | "%![fc-]+F", value, field, field->class); |
44c440bc PP |
535 | int_field->value.u = value; |
536 | bt_field_set_single(field, true); | |
cd95e351 JG |
537 | } |
538 | ||
44c440bc | 539 | double bt_field_real_get_value(struct bt_field *field) |
273b65be | 540 | { |
44c440bc PP |
541 | struct bt_field_real *real_field = (void *) field; |
542 | ||
543 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
544 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
864cad70 | 545 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field"); |
44c440bc | 546 | return real_field->value; |
f6ccaed9 PP |
547 | } |
548 | ||
e5be10ef PP |
549 | void bt_private_field_real_set_value(struct bt_private_field *priv_field, |
550 | double value) | |
f6ccaed9 | 551 | { |
e5be10ef | 552 | struct bt_field *field = (void *) priv_field; |
44c440bc | 553 | struct bt_field_real *real_field = (void *) field; |
cb6f1f7d | 554 | |
44c440bc | 555 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 | 556 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field"); |
44c440bc PP |
557 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
558 | BT_ASSERT_PRE( | |
5cd6d0e5 | 559 | !((struct bt_field_class_real *) field->class)->is_single_precision || |
44c440bc PP |
560 | (double) (float) value == value, |
561 | "Invalid value for a single-precision real number: value=%f, " | |
5cd6d0e5 | 562 | "%![fc-]+F", value, field->class); |
44c440bc PP |
563 | real_field->value = value; |
564 | bt_field_set_single(field, true); | |
565 | } | |
566 | ||
567 | int bt_field_unsigned_enumeration_get_mapping_labels(struct bt_field *field, | |
5cd6d0e5 | 568 | bt_field_class_enumeration_mapping_label_array *label_array, |
44c440bc PP |
569 | uint64_t *count) |
570 | { | |
571 | struct bt_field_integer *int_field = (void *) field; | |
572 | ||
573 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
574 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
575 | BT_ASSERT_PRE_NON_NULL(label_array, "Count (output)"); | |
576 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
864cad70 PP |
577 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
578 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field"); | |
5cd6d0e5 PP |
579 | return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value( |
580 | field->class, int_field->value.u, label_array, count); | |
273b65be JG |
581 | } |
582 | ||
44c440bc | 583 | int bt_field_signed_enumeration_get_mapping_labels(struct bt_field *field, |
5cd6d0e5 | 584 | bt_field_class_enumeration_mapping_label_array *label_array, |
44c440bc | 585 | uint64_t *count) |
cd95e351 | 586 | { |
44c440bc | 587 | struct bt_field_integer *int_field = (void *) field; |
cb6f1f7d | 588 | |
44c440bc PP |
589 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
590 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
591 | BT_ASSERT_PRE_NON_NULL(label_array, "Count (output)"); | |
592 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
864cad70 PP |
593 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
594 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field"); | |
5cd6d0e5 PP |
595 | return bt_field_class_signed_enumeration_get_mapping_labels_by_value( |
596 | field->class, int_field->value.i, label_array, count); | |
f6ccaed9 | 597 | } |
fc25abce | 598 | |
3dca2276 | 599 | const char *bt_field_string_get_value(struct bt_field *field) |
f6ccaed9 | 600 | { |
44c440bc PP |
601 | struct bt_field_string *string_field = (void *) field; |
602 | ||
603 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
604 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
864cad70 | 605 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
44c440bc PP |
606 | "Field"); |
607 | return (const char *) string_field->buf->data; | |
608 | } | |
609 | ||
610 | uint64_t bt_field_string_get_length(struct bt_field *field) | |
611 | { | |
612 | struct bt_field_string *string_field = (void *) field; | |
cb6f1f7d | 613 | |
44c440bc PP |
614 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
615 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
864cad70 | 616 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
44c440bc PP |
617 | "Field"); |
618 | return string_field->length; | |
cd95e351 JG |
619 | } |
620 | ||
e5be10ef PP |
621 | int bt_private_field_string_set_value(struct bt_private_field *priv_field, |
622 | const char *value) | |
273b65be | 623 | { |
e5be10ef PP |
624 | struct bt_field *field = (void *) priv_field; |
625 | ||
44c440bc | 626 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
cb6f1f7d | 627 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
44c440bc | 628 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
864cad70 | 629 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
44c440bc | 630 | "Field"); |
e5be10ef PP |
631 | bt_private_field_string_clear(priv_field); |
632 | return bt_private_field_string_append_with_length(priv_field, value, | |
44c440bc | 633 | (uint64_t) strlen(value)); |
273b65be JG |
634 | } |
635 | ||
e5be10ef PP |
636 | int bt_private_field_string_append(struct bt_private_field *field, |
637 | const char *value) | |
cd95e351 | 638 | { |
e5be10ef PP |
639 | return bt_private_field_string_append_with_length(field, |
640 | value, (uint64_t) strlen(value)); | |
cd95e351 JG |
641 | } |
642 | ||
e5be10ef PP |
643 | int bt_private_field_string_append_with_length( |
644 | struct bt_private_field *priv_field, | |
44c440bc | 645 | const char *value, uint64_t length) |
273b65be | 646 | { |
e5be10ef | 647 | struct bt_field *field = (void *) priv_field; |
cb6f1f7d PP |
648 | struct bt_field_string *string_field = (void *) field; |
649 | char *data; | |
44c440bc | 650 | uint64_t new_length; |
273b65be | 651 | |
44c440bc | 652 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
cb6f1f7d | 653 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
44c440bc | 654 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
864cad70 PP |
655 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
656 | BT_FIELD_CLASS_TYPE_STRING, "Field"); | |
312c056a | 657 | |
cb6f1f7d PP |
658 | /* Make sure no null bytes are appended */ |
659 | BT_ASSERT_PRE(memchr(value, '\0', length) == NULL, | |
660 | "String value to append contains a null character: " | |
44c440bc | 661 | "partial-value=\"%.32s\", length=%" PRIu64, value, length); |
c6f9c5a3 | 662 | |
44c440bc | 663 | new_length = length + string_field->length; |
cb6f1f7d | 664 | |
44c440bc PP |
665 | if (unlikely(new_length + 1 > string_field->buf->len)) { |
666 | g_array_set_size(string_field->buf, new_length + 1); | |
c6f9c5a3 PP |
667 | } |
668 | ||
cb6f1f7d | 669 | data = string_field->buf->data; |
44c440bc PP |
670 | memcpy(data + string_field->length, value, length); |
671 | ((char *) string_field->buf->data)[new_length] = '\0'; | |
672 | string_field->length = new_length; | |
673 | bt_field_set_single(field, true); | |
cb6f1f7d PP |
674 | return 0; |
675 | } | |
3dca2276 | 676 | |
e5be10ef | 677 | int bt_private_field_string_clear(struct bt_private_field *priv_field) |
cb6f1f7d | 678 | { |
e5be10ef | 679 | struct bt_field *field = (void *) priv_field; |
cb6f1f7d PP |
680 | struct bt_field_string *string_field = (void *) field; |
681 | ||
44c440bc PP |
682 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
683 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); | |
864cad70 PP |
684 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
685 | BT_FIELD_CLASS_TYPE_STRING, "Field"); | |
44c440bc PP |
686 | string_field->length = 0; |
687 | bt_field_set_single(field, true); | |
cb6f1f7d PP |
688 | return 0; |
689 | } | |
690 | ||
44c440bc | 691 | uint64_t bt_field_array_get_length(struct bt_field *field) |
cb6f1f7d | 692 | { |
44c440bc | 693 | struct bt_field_array *array_field = (void *) field; |
c6f9c5a3 | 694 | |
44c440bc PP |
695 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
696 | BT_ASSERT_PRE_FIELD_IS_ARRAY(field, "Field"); | |
697 | return array_field->length; | |
3dca2276 | 698 | } |
f98c6554 | 699 | |
e5be10ef PP |
700 | int bt_private_field_dynamic_array_set_length( |
701 | struct bt_private_field *priv_field, uint64_t length) | |
3dca2276 | 702 | { |
44c440bc | 703 | int ret = 0; |
e5be10ef | 704 | struct bt_field *field = (void *) priv_field; |
44c440bc | 705 | struct bt_field_array *array_field = (void *) field; |
f98c6554 | 706 | |
44c440bc | 707 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 PP |
708 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
709 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, "Field"); | |
44c440bc | 710 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
273b65be | 711 | |
44c440bc PP |
712 | if (unlikely(length > array_field->fields->len)) { |
713 | /* Make more room */ | |
5cd6d0e5 | 714 | struct bt_field_class_array *array_fc; |
44c440bc PP |
715 | uint64_t cur_len = array_field->fields->len; |
716 | uint64_t i; | |
cb6f1f7d | 717 | |
44c440bc | 718 | g_ptr_array_set_size(array_field->fields, length); |
5cd6d0e5 | 719 | array_fc = (void *) field->class; |
cb6f1f7d | 720 | |
44c440bc PP |
721 | for (i = cur_len; i < array_field->fields->len; i++) { |
722 | struct bt_field *elem_field = bt_field_create( | |
5cd6d0e5 | 723 | array_fc->element_fc); |
273b65be | 724 | |
44c440bc PP |
725 | if (!elem_field) { |
726 | BT_LIB_LOGE("Cannot create element field for " | |
727 | "dynamic array field: " | |
728 | "index=%" PRIu64 ", " | |
729 | "%![array-field-]+f", i, field); | |
730 | ret = -1; | |
731 | goto end; | |
732 | } | |
c58b9c62 | 733 | |
44c440bc PP |
734 | BT_ASSERT(!array_field->fields->pdata[i]); |
735 | array_field->fields->pdata[i] = elem_field; | |
c58b9c62 | 736 | } |
c58b9c62 JG |
737 | } |
738 | ||
44c440bc | 739 | array_field->length = length; |
3dca2276 | 740 | |
273b65be | 741 | end: |
c58b9c62 | 742 | return ret; |
273b65be JG |
743 | } |
744 | ||
44c440bc PP |
745 | struct bt_field *bt_field_array_borrow_element_field_by_index( |
746 | struct bt_field *field, uint64_t index) | |
312c056a | 747 | { |
44c440bc | 748 | struct bt_field_array *array_field = (void *) field; |
312c056a | 749 | |
44c440bc PP |
750 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
751 | BT_ASSERT_PRE_FIELD_IS_ARRAY(field, "Field"); | |
752 | BT_ASSERT_PRE_VALID_INDEX(index, array_field->length); | |
753 | return array_field->fields->pdata[index]; | |
312c056a PP |
754 | } |
755 | ||
e5be10ef | 756 | struct bt_private_field * |
28e6ca8b | 757 | bt_private_field_array_borrow_element_field_by_index( |
e5be10ef PP |
758 | struct bt_private_field *field, uint64_t index) |
759 | { | |
760 | return (void *) bt_field_array_borrow_element_field_by_index( | |
761 | (void *) field, index); | |
762 | } | |
763 | ||
44c440bc PP |
764 | struct bt_field *bt_field_structure_borrow_member_field_by_index( |
765 | struct bt_field *field, uint64_t index) | |
4d4b475d | 766 | { |
44c440bc | 767 | struct bt_field_structure *struct_field = (void *) field; |
4d4b475d | 768 | |
44c440bc | 769 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 PP |
770 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
771 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); | |
44c440bc PP |
772 | BT_ASSERT_PRE_VALID_INDEX(index, struct_field->fields->len); |
773 | return struct_field->fields->pdata[index]; | |
4d4b475d PP |
774 | } |
775 | ||
e5be10ef | 776 | struct bt_private_field * |
28e6ca8b | 777 | bt_private_field_structure_borrow_member_field_by_index( |
e5be10ef PP |
778 | struct bt_private_field *field, uint64_t index) |
779 | { | |
780 | return (void *) bt_field_structure_borrow_member_field_by_index( | |
781 | (void *) field, index); | |
782 | } | |
783 | ||
44c440bc PP |
784 | struct bt_field *bt_field_structure_borrow_member_field_by_name( |
785 | struct bt_field *field, const char *name) | |
273b65be | 786 | { |
44c440bc | 787 | struct bt_field *ret_field = NULL; |
5cd6d0e5 | 788 | struct bt_field_class_structure *struct_fc; |
44c440bc PP |
789 | struct bt_field_structure *struct_field = (void *) field; |
790 | gpointer orig_key; | |
791 | gpointer index; | |
fc25abce | 792 | |
44c440bc PP |
793 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
794 | BT_ASSERT_PRE_NON_NULL(name, "Field name"); | |
864cad70 PP |
795 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
796 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); | |
5cd6d0e5 | 797 | struct_fc = (void *) field->class; |
312c056a | 798 | |
5cd6d0e5 | 799 | if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name, |
44c440bc | 800 | &orig_key, &index)) { |
312c056a | 801 | goto end; |
fc25abce PP |
802 | } |
803 | ||
44c440bc PP |
804 | ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)]; |
805 | BT_ASSERT(ret_field); | |
312c056a PP |
806 | |
807 | end: | |
44c440bc | 808 | return ret_field; |
273b65be JG |
809 | } |
810 | ||
e5be10ef | 811 | struct bt_private_field * |
28e6ca8b | 812 | bt_private_field_structure_borrow_member_field_by_name( |
e5be10ef PP |
813 | struct bt_private_field *field, const char *name) |
814 | { | |
815 | return (void *) bt_field_structure_borrow_member_field_by_name( | |
816 | (void *) field, name); | |
817 | } | |
818 | ||
44c440bc PP |
819 | struct bt_field *bt_field_variant_borrow_selected_option_field( |
820 | struct bt_field *field) | |
273b65be | 821 | { |
44c440bc | 822 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 823 | |
44c440bc | 824 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 PP |
825 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
826 | BT_FIELD_CLASS_TYPE_VARIANT, "Field"); | |
44c440bc PP |
827 | BT_ASSERT_PRE(var_field->selected_field, |
828 | "Variant field has no selected field: %!+f", field); | |
829 | return var_field->selected_field; | |
273b65be JG |
830 | } |
831 | ||
e5be10ef | 832 | struct bt_private_field * |
28e6ca8b | 833 | bt_private_field_variant_borrow_selected_option_field( |
e5be10ef | 834 | struct bt_private_field *field) |
273b65be | 835 | { |
e5be10ef PP |
836 | return (void *) bt_field_variant_borrow_selected_option_field( |
837 | (void *) field); | |
838 | } | |
839 | ||
28e6ca8b | 840 | int bt_private_field_variant_select_option_field( |
e5be10ef PP |
841 | struct bt_private_field *priv_field, uint64_t index) |
842 | { | |
843 | struct bt_field *field = (void *) priv_field; | |
44c440bc | 844 | struct bt_field_variant *var_field = (void *) field; |
fc25abce | 845 | |
44c440bc | 846 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 PP |
847 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
848 | BT_FIELD_CLASS_TYPE_VARIANT, "Field"); | |
44c440bc PP |
849 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
850 | BT_ASSERT_PRE_VALID_INDEX(index, var_field->fields->len); | |
851 | var_field->selected_field = var_field->fields->pdata[index]; | |
852 | var_field->selected_index = index; | |
853 | return 0; | |
273b65be JG |
854 | } |
855 | ||
44c440bc PP |
856 | uint64_t bt_field_variant_get_selected_option_field_index( |
857 | struct bt_field *field) | |
312c056a | 858 | { |
44c440bc | 859 | struct bt_field_variant *var_field = (void *) field; |
312c056a | 860 | |
44c440bc | 861 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
864cad70 PP |
862 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
863 | BT_FIELD_CLASS_TYPE_VARIANT, "Field"); | |
44c440bc PP |
864 | BT_ASSERT_PRE(var_field->selected_field, |
865 | "Variant field has no selected field: %!+f", field); | |
866 | return var_field->selected_index; | |
312c056a PP |
867 | } |
868 | ||
44c440bc PP |
869 | static inline |
870 | void bt_field_finalize(struct bt_field *field) | |
273b65be | 871 | { |
44c440bc | 872 | BT_ASSERT(field); |
5cd6d0e5 | 873 | BT_LOGD_STR("Putting field's class."); |
65300d60 | 874 | bt_object_put_ref(field->class); |
273b65be JG |
875 | } |
876 | ||
877 | static | |
44c440bc | 878 | void destroy_integer_field(struct bt_field *field) |
273b65be | 879 | { |
44c440bc PP |
880 | BT_ASSERT(field); |
881 | BT_LIB_LOGD("Destroying integer field object: %!+f", field); | |
882 | bt_field_finalize(field); | |
883 | g_free(field); | |
273b65be JG |
884 | } |
885 | ||
cb6f1f7d | 886 | static |
44c440bc | 887 | void destroy_real_field(struct bt_field *field) |
273b65be | 888 | { |
44c440bc PP |
889 | BT_ASSERT(field); |
890 | BT_LIB_LOGD("Destroying real field object: %!+f", field); | |
891 | bt_field_finalize(field); | |
892 | g_free(field); | |
273b65be JG |
893 | } |
894 | ||
cb6f1f7d | 895 | static |
44c440bc | 896 | void destroy_structure_field(struct bt_field *field) |
273b65be | 897 | { |
44c440bc | 898 | struct bt_field_structure *struct_field = (void *) field; |
273b65be | 899 | |
f6ccaed9 | 900 | BT_ASSERT(field); |
44c440bc PP |
901 | BT_LIB_LOGD("Destroying structure field object: %!+f", field); |
902 | bt_field_finalize(field); | |
f6ccaed9 | 903 | |
44c440bc PP |
904 | if (struct_field->fields) { |
905 | g_ptr_array_free(struct_field->fields, TRUE); | |
273b65be | 906 | } |
f6ccaed9 | 907 | |
44c440bc | 908 | g_free(field); |
273b65be JG |
909 | } |
910 | ||
cb6f1f7d | 911 | static |
44c440bc | 912 | void destroy_variant_field(struct bt_field *field) |
273b65be | 913 | { |
44c440bc | 914 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 915 | |
f6ccaed9 | 916 | BT_ASSERT(field); |
44c440bc PP |
917 | BT_LIB_LOGD("Destroying variant field object: %!+f", field); |
918 | bt_field_finalize(field); | |
312c056a | 919 | |
44c440bc PP |
920 | if (var_field->fields) { |
921 | g_ptr_array_free(var_field->fields, TRUE); | |
fc25abce | 922 | } |
f6ccaed9 | 923 | |
44c440bc | 924 | g_free(field); |
273b65be JG |
925 | } |
926 | ||
cb6f1f7d | 927 | static |
44c440bc | 928 | void destroy_array_field(struct bt_field *field) |
273b65be | 929 | { |
44c440bc | 930 | struct bt_field_array *array_field = (void *) field; |
273b65be | 931 | |
f6ccaed9 | 932 | BT_ASSERT(field); |
44c440bc PP |
933 | BT_LIB_LOGD("Destroying array field object: %!+f", field); |
934 | bt_field_finalize(field); | |
3dca2276 | 935 | |
44c440bc PP |
936 | if (array_field->fields) { |
937 | g_ptr_array_free(array_field->fields, TRUE); | |
273b65be | 938 | } |
f6ccaed9 | 939 | |
44c440bc | 940 | g_free(field); |
273b65be JG |
941 | } |
942 | ||
cb6f1f7d | 943 | static |
44c440bc | 944 | void destroy_string_field(struct bt_field *field) |
273b65be | 945 | { |
44c440bc | 946 | struct bt_field_string *string_field = (void *) field; |
273b65be | 947 | |
f6ccaed9 | 948 | BT_ASSERT(field); |
44c440bc PP |
949 | BT_LIB_LOGD("Destroying string field object: %!+f", field); |
950 | bt_field_finalize(field); | |
3dca2276 | 951 | |
44c440bc PP |
952 | if (string_field->buf) { |
953 | g_array_free(string_field->buf, TRUE); | |
273b65be | 954 | } |
44c440bc PP |
955 | |
956 | g_free(field); | |
273b65be JG |
957 | } |
958 | ||
44c440bc PP |
959 | BT_HIDDEN |
960 | void bt_field_destroy(struct bt_field *field) | |
12c8a1a3 | 961 | { |
f6ccaed9 | 962 | BT_ASSERT(field); |
864cad70 PP |
963 | BT_ASSERT(bt_field_class_has_known_type(field->class)); |
964 | field_destroy_funcs[field->class->type](field); | |
12c8a1a3 JG |
965 | } |
966 | ||
cb6f1f7d | 967 | static |
44c440bc | 968 | void reset_single_field(struct bt_field *field) |
12c8a1a3 | 969 | { |
f6ccaed9 | 970 | BT_ASSERT(field); |
44c440bc | 971 | field->is_set = false; |
12c8a1a3 JG |
972 | } |
973 | ||
cb6f1f7d | 974 | static |
44c440bc | 975 | void reset_structure_field(struct bt_field *field) |
12c8a1a3 | 976 | { |
44c440bc PP |
977 | uint64_t i; |
978 | struct bt_field_structure *struct_field = (void *) field; | |
12c8a1a3 | 979 | |
f6ccaed9 | 980 | BT_ASSERT(field); |
44c440bc PP |
981 | |
982 | for (i = 0; i < struct_field->fields->len; i++) { | |
983 | bt_field_reset(struct_field->fields->pdata[i]); | |
984 | } | |
12c8a1a3 JG |
985 | } |
986 | ||
cb6f1f7d | 987 | static |
44c440bc | 988 | void reset_variant_field(struct bt_field *field) |
12c8a1a3 | 989 | { |
44c440bc PP |
990 | uint64_t i; |
991 | struct bt_field_variant *var_field = (void *) field; | |
12c8a1a3 | 992 | |
f6ccaed9 | 993 | BT_ASSERT(field); |
f6ccaed9 | 994 | |
44c440bc PP |
995 | for (i = 0; i < var_field->fields->len; i++) { |
996 | bt_field_reset(var_field->fields->pdata[i]); | |
12c8a1a3 | 997 | } |
12c8a1a3 JG |
998 | } |
999 | ||
cb6f1f7d | 1000 | static |
44c440bc | 1001 | void reset_array_field(struct bt_field *field) |
12c8a1a3 | 1002 | { |
312c056a | 1003 | uint64_t i; |
44c440bc | 1004 | struct bt_field_array *array_field = (void *) field; |
12c8a1a3 | 1005 | |
f6ccaed9 | 1006 | BT_ASSERT(field); |
f6ccaed9 | 1007 | |
44c440bc PP |
1008 | for (i = 0; i < array_field->fields->len; i++) { |
1009 | bt_field_reset(array_field->fields->pdata[i]); | |
12c8a1a3 | 1010 | } |
12c8a1a3 JG |
1011 | } |
1012 | ||
cb6f1f7d | 1013 | static |
44c440bc | 1014 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1015 | { |
312c056a | 1016 | field->frozen = is_frozen; |
918be005 PP |
1017 | } |
1018 | ||
cb6f1f7d | 1019 | static |
44c440bc | 1020 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1021 | { |
312c056a | 1022 | uint64_t i; |
44c440bc | 1023 | struct bt_field_structure *struct_field = (void *) field; |
918be005 | 1024 | |
44c440bc PP |
1025 | BT_LIB_LOGD("Setting structure field's frozen state: " |
1026 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1027 | |
44c440bc PP |
1028 | for (i = 0; i < struct_field->fields->len; i++) { |
1029 | struct bt_field *member_field = struct_field->fields->pdata[i]; | |
918be005 | 1030 | |
44c440bc PP |
1031 | BT_LIB_LOGD("Setting structure field's member field's " |
1032 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1033 | member_field, i); | |
1034 | bt_field_set_is_frozen(member_field, is_frozen); | |
918be005 PP |
1035 | } |
1036 | ||
44c440bc | 1037 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1038 | } |
1039 | ||
cb6f1f7d | 1040 | static |
44c440bc | 1041 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1042 | { |
312c056a | 1043 | uint64_t i; |
44c440bc | 1044 | struct bt_field_variant *var_field = (void *) field; |
918be005 | 1045 | |
44c440bc PP |
1046 | BT_LIB_LOGD("Setting variant field's frozen state: " |
1047 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
312c056a | 1048 | |
44c440bc PP |
1049 | for (i = 0; i < var_field->fields->len; i++) { |
1050 | struct bt_field *option_field = var_field->fields->pdata[i]; | |
312c056a | 1051 | |
44c440bc PP |
1052 | BT_LIB_LOGD("Setting variant field's option field's " |
1053 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1054 | option_field, i); | |
1055 | bt_field_set_is_frozen(option_field, is_frozen); | |
312c056a PP |
1056 | } |
1057 | ||
44c440bc | 1058 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1059 | } |
1060 | ||
cb6f1f7d | 1061 | static |
44c440bc | 1062 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1063 | { |
44c440bc | 1064 | uint64_t i; |
cb6f1f7d | 1065 | struct bt_field_array *array_field = (void *) field; |
918be005 | 1066 | |
44c440bc PP |
1067 | BT_LIB_LOGD("Setting array field's frozen state: " |
1068 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1069 | |
44c440bc PP |
1070 | for (i = 0; i < array_field->fields->len; i++) { |
1071 | struct bt_field *elem_field = array_field->fields->pdata[i]; | |
918be005 | 1072 | |
44c440bc PP |
1073 | BT_LIB_LOGD("Setting array field's element field's " |
1074 | "frozen state: %![field-]+f, index=%" PRIu64, | |
fc25abce | 1075 | elem_field, i); |
44c440bc | 1076 | bt_field_set_is_frozen(elem_field, is_frozen); |
918be005 PP |
1077 | } |
1078 | ||
44c440bc | 1079 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1080 | } |
1081 | ||
1082 | BT_HIDDEN | |
44c440bc | 1083 | void _bt_field_set_is_frozen(struct bt_field *field, |
312c056a | 1084 | bool is_frozen) |
918be005 | 1085 | { |
44c440bc PP |
1086 | BT_ASSERT(field); |
1087 | BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d", | |
312c056a | 1088 | field, is_frozen); |
312c056a PP |
1089 | BT_ASSERT(field->methods->set_is_frozen); |
1090 | field->methods->set_is_frozen(field, is_frozen); | |
918be005 | 1091 | } |
76f869ab | 1092 | |
cb6f1f7d | 1093 | static |
44c440bc | 1094 | bool single_field_is_set(struct bt_field *field) |
76f869ab | 1095 | { |
44c440bc PP |
1096 | BT_ASSERT(field); |
1097 | return field->is_set; | |
76f869ab JG |
1098 | } |
1099 | ||
cb6f1f7d | 1100 | static |
44c440bc | 1101 | bool structure_field_is_set(struct bt_field *field) |
76f869ab | 1102 | { |
44c440bc PP |
1103 | bool is_set = true; |
1104 | uint64_t i; | |
1105 | struct bt_field_structure *struct_field = (void *) field; | |
76f869ab | 1106 | |
f6ccaed9 | 1107 | BT_ASSERT(field); |
3dca2276 | 1108 | |
44c440bc PP |
1109 | for (i = 0; i < struct_field->fields->len; i++) { |
1110 | is_set = bt_field_is_set(struct_field->fields->pdata[i]); | |
d4bf905a | 1111 | if (!is_set) { |
76f869ab JG |
1112 | goto end; |
1113 | } | |
1114 | } | |
3dca2276 | 1115 | |
76f869ab | 1116 | end: |
d4bf905a | 1117 | return is_set; |
76f869ab JG |
1118 | } |
1119 | ||
cb6f1f7d | 1120 | static |
44c440bc | 1121 | bool variant_field_is_set(struct bt_field *field) |
76f869ab | 1122 | { |
44c440bc PP |
1123 | struct bt_field_variant *var_field = (void *) field; |
1124 | bool is_set = false; | |
76f869ab | 1125 | |
f6ccaed9 | 1126 | BT_ASSERT(field); |
3dca2276 | 1127 | |
44c440bc PP |
1128 | if (var_field->selected_field) { |
1129 | is_set = bt_field_is_set(var_field->selected_field); | |
76f869ab | 1130 | } |
3dca2276 | 1131 | |
d4bf905a | 1132 | return is_set; |
76f869ab JG |
1133 | } |
1134 | ||
cb6f1f7d | 1135 | static |
44c440bc | 1136 | bool array_field_is_set(struct bt_field *field) |
76f869ab | 1137 | { |
44c440bc PP |
1138 | bool is_set = true; |
1139 | uint64_t i; | |
1140 | struct bt_field_array *array_field = (void *) field; | |
76f869ab | 1141 | |
f6ccaed9 | 1142 | BT_ASSERT(field); |
3dca2276 | 1143 | |
44c440bc PP |
1144 | for (i = 0; i < array_field->length; i++) { |
1145 | is_set = bt_field_is_set(array_field->fields->pdata[i]); | |
d4bf905a | 1146 | if (!is_set) { |
76f869ab JG |
1147 | goto end; |
1148 | } | |
1149 | } | |
3dca2276 | 1150 | |
76f869ab | 1151 | end: |
d4bf905a | 1152 | return is_set; |
76f869ab | 1153 | } |