Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
e2f7325d | 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
6 | */ |
7 | ||
350ad6c1 | 8 | #define BT_LOG_TAG "LIB/FIELD" |
c2d9d9cf | 9 | #include "lib/logging.h" |
fc25abce | 10 | |
d98421f2 | 11 | #include "lib/assert-cond.h" |
3fadfbc0 | 12 | #include <babeltrace2/trace-ir/field.h> |
578e048b MJ |
13 | #include "lib/object.h" |
14 | #include "compat/compiler.h" | |
15 | #include "compat/fcntl.h" | |
16 | #include "common/align.h" | |
17 | #include "common/assert.h" | |
fc25abce | 18 | #include <inttypes.h> |
c4f23e30 | 19 | #include <stdbool.h> |
273b65be | 20 | |
578e048b MJ |
21 | #include "field.h" |
22 | #include "field-class.h" | |
d24d5663 | 23 | #include "lib/func-status.h" |
578e048b | 24 | |
cb6f1f7d | 25 | static |
44c440bc | 26 | void reset_single_field(struct bt_field *field); |
cb6f1f7d PP |
27 | |
28 | static | |
44c440bc | 29 | void reset_array_field(struct bt_field *field); |
cb6f1f7d PP |
30 | |
31 | static | |
44c440bc | 32 | void reset_structure_field(struct bt_field *field); |
cb6f1f7d | 33 | |
b38aea74 PP |
34 | static |
35 | void reset_option_field(struct bt_field *field); | |
36 | ||
cb6f1f7d | 37 | static |
44c440bc | 38 | void reset_variant_field(struct bt_field *field); |
cb6f1f7d PP |
39 | |
40 | static | |
44c440bc | 41 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
42 | |
43 | static | |
44c440bc | 44 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
45 | |
46 | static | |
44c440bc | 47 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d | 48 | |
b38aea74 PP |
49 | static |
50 | void set_option_field_is_frozen(struct bt_field *field, bool is_frozen); | |
51 | ||
cb6f1f7d | 52 | static |
44c440bc | 53 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen); |
cb6f1f7d PP |
54 | |
55 | static | |
40f4ba76 | 56 | bool single_field_is_set(const struct bt_field *field); |
cb6f1f7d PP |
57 | |
58 | static | |
40f4ba76 | 59 | bool array_field_is_set(const struct bt_field *field); |
cb6f1f7d PP |
60 | |
61 | static | |
40f4ba76 | 62 | bool structure_field_is_set(const struct bt_field *field); |
cb6f1f7d | 63 | |
b38aea74 PP |
64 | static |
65 | bool option_field_is_set(const struct bt_field *field); | |
66 | ||
cb6f1f7d | 67 | static |
40f4ba76 | 68 | bool variant_field_is_set(const struct bt_field *field); |
cb6f1f7d | 69 | |
5cebbe7f PP |
70 | static |
71 | struct bt_field_methods bool_field_methods = { | |
72 | .set_is_frozen = set_single_field_is_frozen, | |
73 | .is_set = single_field_is_set, | |
74 | .reset = reset_single_field, | |
75 | }; | |
76 | ||
1094efa4 PP |
77 | static |
78 | struct bt_field_methods bit_array_field_methods = { | |
79 | .set_is_frozen = set_single_field_is_frozen, | |
80 | .is_set = single_field_is_set, | |
81 | .reset = reset_single_field, | |
82 | }; | |
83 | ||
cb6f1f7d | 84 | static |
44c440bc PP |
85 | struct bt_field_methods integer_field_methods = { |
86 | .set_is_frozen = set_single_field_is_frozen, | |
87 | .is_set = single_field_is_set, | |
88 | .reset = reset_single_field, | |
3dca2276 | 89 | }; |
273b65be | 90 | |
44c440bc PP |
91 | static |
92 | struct bt_field_methods real_field_methods = { | |
93 | .set_is_frozen = set_single_field_is_frozen, | |
94 | .is_set = single_field_is_set, | |
95 | .reset = reset_single_field, | |
12c8a1a3 JG |
96 | }; |
97 | ||
44c440bc PP |
98 | static |
99 | struct bt_field_methods string_field_methods = { | |
100 | .set_is_frozen = set_single_field_is_frozen, | |
101 | .is_set = single_field_is_set, | |
102 | .reset = reset_single_field, | |
273b65be JG |
103 | }; |
104 | ||
44c440bc PP |
105 | static |
106 | struct bt_field_methods structure_field_methods = { | |
107 | .set_is_frozen = set_structure_field_is_frozen, | |
108 | .is_set = structure_field_is_set, | |
109 | .reset = reset_structure_field, | |
87d43dc1 JG |
110 | }; |
111 | ||
44c440bc PP |
112 | static |
113 | struct bt_field_methods array_field_methods = { | |
114 | .set_is_frozen = set_array_field_is_frozen, | |
115 | .is_set = array_field_is_set, | |
116 | .reset = reset_array_field, | |
918be005 PP |
117 | }; |
118 | ||
b38aea74 PP |
119 | static |
120 | struct bt_field_methods option_field_methods = { | |
121 | .set_is_frozen = set_option_field_is_frozen, | |
122 | .is_set = option_field_is_set, | |
123 | .reset = reset_option_field, | |
124 | }; | |
125 | ||
76f869ab | 126 | static |
44c440bc PP |
127 | struct bt_field_methods variant_field_methods = { |
128 | .set_is_frozen = set_variant_field_is_frozen, | |
129 | .is_set = variant_field_is_set, | |
130 | .reset = reset_variant_field, | |
131 | }; | |
76f869ab | 132 | |
5cebbe7f PP |
133 | static |
134 | struct bt_field *create_bool_field(struct bt_field_class *); | |
135 | ||
1094efa4 PP |
136 | static |
137 | struct bt_field *create_bit_array_field(struct bt_field_class *); | |
138 | ||
3dca2276 | 139 | static |
5cd6d0e5 | 140 | struct bt_field *create_integer_field(struct bt_field_class *); |
3dca2276 PP |
141 | |
142 | static | |
5cd6d0e5 | 143 | struct bt_field *create_real_field(struct bt_field_class *); |
3dca2276 PP |
144 | |
145 | static | |
5cd6d0e5 | 146 | struct bt_field *create_string_field(struct bt_field_class *); |
3dca2276 PP |
147 | |
148 | static | |
5cd6d0e5 | 149 | struct bt_field *create_structure_field(struct bt_field_class *); |
9c3869a9 | 150 | |
3dca2276 | 151 | static |
5cd6d0e5 | 152 | struct bt_field *create_static_array_field(struct bt_field_class *); |
f6ccaed9 | 153 | |
3dca2276 | 154 | static |
5cd6d0e5 | 155 | struct bt_field *create_dynamic_array_field(struct bt_field_class *); |
f6ccaed9 | 156 | |
b38aea74 PP |
157 | static |
158 | struct bt_field *create_option_field(struct bt_field_class *); | |
159 | ||
3dca2276 | 160 | static |
5cd6d0e5 | 161 | struct bt_field *create_variant_field(struct bt_field_class *); |
f6ccaed9 | 162 | |
5cebbe7f PP |
163 | static |
164 | void destroy_bool_field(struct bt_field *field); | |
165 | ||
1094efa4 PP |
166 | static |
167 | void destroy_bit_array_field(struct bt_field *field); | |
168 | ||
312c056a | 169 | static |
44c440bc | 170 | void destroy_integer_field(struct bt_field *field); |
312c056a PP |
171 | |
172 | static | |
44c440bc | 173 | void destroy_real_field(struct bt_field *field); |
312c056a PP |
174 | |
175 | static | |
44c440bc | 176 | void destroy_string_field(struct bt_field *field); |
312c056a PP |
177 | |
178 | static | |
44c440bc | 179 | void destroy_structure_field(struct bt_field *field); |
312c056a PP |
180 | |
181 | static | |
44c440bc | 182 | void destroy_array_field(struct bt_field *field); |
312c056a | 183 | |
b38aea74 PP |
184 | static |
185 | void destroy_option_field(struct bt_field *field); | |
186 | ||
312c056a | 187 | static |
44c440bc | 188 | void destroy_variant_field(struct bt_field *field); |
312c056a | 189 | |
d29378b1 | 190 | struct bt_field_class *bt_field_borrow_class(struct bt_field *field) |
cb6f1f7d | 191 | { |
bdb288b3 | 192 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
5cd6d0e5 | 193 | return field->class; |
cb6f1f7d PP |
194 | } |
195 | ||
40f4ba76 PP |
196 | const struct bt_field_class *bt_field_borrow_class_const( |
197 | const struct bt_field *field) | |
e5be10ef | 198 | { |
bdb288b3 | 199 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
40f4ba76 | 200 | return field->class; |
e5be10ef PP |
201 | } |
202 | ||
40f4ba76 | 203 | enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field) |
cb6f1f7d | 204 | { |
bdb288b3 | 205 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
864cad70 | 206 | return field->class->type; |
cb6f1f7d PP |
207 | } |
208 | ||
312c056a | 209 | BT_HIDDEN |
5cd6d0e5 | 210 | struct bt_field *bt_field_create(struct bt_field_class *fc) |
273b65be | 211 | { |
50842bdc | 212 | struct bt_field *field = NULL; |
44c440bc | 213 | |
bdb288b3 | 214 | BT_ASSERT(fc); |
9c3869a9 PP |
215 | |
216 | switch (fc->type) { | |
217 | case BT_FIELD_CLASS_TYPE_BOOL: | |
218 | field = create_bool_field(fc); | |
219 | break; | |
220 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: | |
221 | field = create_bit_array_field(fc); | |
222 | break; | |
223 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
224 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
225 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
226 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
227 | field = create_integer_field(fc); | |
228 | break; | |
229 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: | |
230 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: | |
231 | field = create_real_field(fc); | |
232 | break; | |
233 | case BT_FIELD_CLASS_TYPE_STRING: | |
234 | field = create_string_field(fc); | |
235 | break; | |
236 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
237 | field = create_structure_field(fc); | |
238 | break; | |
239 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
240 | field = create_static_array_field(fc); | |
241 | break; | |
242 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: | |
243 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: | |
244 | field = create_dynamic_array_field(fc); | |
245 | break; | |
246 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: | |
247 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: | |
248 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
249 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
250 | field = create_option_field(fc); | |
251 | break; | |
252 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: | |
253 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
254 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
255 | field = create_variant_field(fc); | |
256 | break; | |
257 | default: | |
498e7994 | 258 | bt_common_abort(); |
9c3869a9 PP |
259 | } |
260 | ||
273b65be | 261 | if (!field) { |
870631a2 | 262 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: " |
5cd6d0e5 | 263 | "%![fc-]+F", fc); |
3dca2276 | 264 | goto end; |
273b65be JG |
265 | } |
266 | ||
3dca2276 PP |
267 | end: |
268 | return field; | |
273b65be JG |
269 | } |
270 | ||
44c440bc | 271 | static inline |
5cd6d0e5 | 272 | void init_field(struct bt_field *field, struct bt_field_class *fc, |
44c440bc | 273 | struct bt_field_methods *methods) |
cd95e351 | 274 | { |
44c440bc | 275 | BT_ASSERT(field); |
5cd6d0e5 | 276 | BT_ASSERT(fc); |
44c440bc PP |
277 | bt_object_init_unique(&field->base); |
278 | field->methods = methods; | |
398454ed | 279 | field->class = fc; |
6871026b | 280 | bt_object_get_ref_no_null_check(fc); |
cd95e351 JG |
281 | } |
282 | ||
5cebbe7f PP |
283 | static |
284 | struct bt_field *create_bool_field(struct bt_field_class *fc) | |
285 | { | |
286 | struct bt_field_bool *bool_field; | |
287 | ||
288 | BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc); | |
289 | bool_field = g_new0(struct bt_field_bool, 1); | |
290 | if (!bool_field) { | |
291 | BT_LIB_LOGE_APPEND_CAUSE( | |
292 | "Failed to allocate one boolean field."); | |
293 | goto end; | |
294 | } | |
295 | ||
296 | init_field((void *) bool_field, fc, &bool_field_methods); | |
297 | BT_LIB_LOGD("Created boolean field object: %!+f", bool_field); | |
298 | ||
299 | end: | |
300 | return (void *) bool_field; | |
301 | } | |
302 | ||
1094efa4 PP |
303 | static |
304 | struct bt_field *create_bit_array_field(struct bt_field_class *fc) | |
305 | { | |
306 | struct bt_field_bit_array *ba_field; | |
307 | ||
308 | BT_LIB_LOGD("Creating bit array field object: %![fc-]+F", fc); | |
309 | ba_field = g_new0(struct bt_field_bit_array, 1); | |
310 | if (!ba_field) { | |
311 | BT_LIB_LOGE_APPEND_CAUSE( | |
312 | "Failed to allocate one bit array field."); | |
313 | goto end; | |
314 | } | |
315 | ||
316 | init_field((void *) ba_field, fc, &bit_array_field_methods); | |
317 | BT_LIB_LOGD("Created bit array field object: %!+f", ba_field); | |
318 | ||
319 | end: | |
320 | return (void *) ba_field; | |
321 | } | |
322 | ||
44c440bc | 323 | static |
5cd6d0e5 | 324 | struct bt_field *create_integer_field(struct bt_field_class *fc) |
4ebcc695 | 325 | { |
44c440bc PP |
326 | struct bt_field_integer *int_field; |
327 | ||
5cd6d0e5 | 328 | BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc); |
44c440bc PP |
329 | int_field = g_new0(struct bt_field_integer, 1); |
330 | if (!int_field) { | |
870631a2 PP |
331 | BT_LIB_LOGE_APPEND_CAUSE( |
332 | "Failed to allocate one integer field."); | |
44c440bc PP |
333 | goto end; |
334 | } | |
335 | ||
5cd6d0e5 | 336 | init_field((void *) int_field, fc, &integer_field_methods); |
44c440bc PP |
337 | BT_LIB_LOGD("Created integer field object: %!+f", int_field); |
338 | ||
339 | end: | |
340 | return (void *) int_field; | |
4ebcc695 PP |
341 | } |
342 | ||
44c440bc | 343 | static |
5cd6d0e5 | 344 | struct bt_field *create_real_field(struct bt_field_class *fc) |
2e8876d3 | 345 | { |
44c440bc | 346 | struct bt_field_real *real_field; |
cb6f1f7d | 347 | |
5cd6d0e5 | 348 | BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc); |
44c440bc PP |
349 | real_field = g_new0(struct bt_field_real, 1); |
350 | if (!real_field) { | |
870631a2 | 351 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field."); |
44c440bc PP |
352 | goto end; |
353 | } | |
354 | ||
5cd6d0e5 | 355 | init_field((void *) real_field, fc, &real_field_methods); |
44c440bc PP |
356 | BT_LIB_LOGD("Created real field object: %!+f", real_field); |
357 | ||
358 | end: | |
359 | return (void *) real_field; | |
2e8876d3 PP |
360 | } |
361 | ||
44c440bc | 362 | static |
5cd6d0e5 | 363 | struct bt_field *create_string_field(struct bt_field_class *fc) |
273b65be | 364 | { |
44c440bc | 365 | struct bt_field_string *string_field; |
cb6f1f7d | 366 | |
5cd6d0e5 | 367 | BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc); |
44c440bc PP |
368 | string_field = g_new0(struct bt_field_string, 1); |
369 | if (!string_field) { | |
870631a2 PP |
370 | BT_LIB_LOGE_APPEND_CAUSE( |
371 | "Failed to allocate one string field."); | |
44c440bc PP |
372 | goto end; |
373 | } | |
cb6f1f7d | 374 | |
5cd6d0e5 | 375 | init_field((void *) string_field, fc, &string_field_methods); |
44c440bc PP |
376 | string_field->buf = g_array_sized_new(FALSE, FALSE, |
377 | sizeof(char), 1); | |
378 | if (!string_field->buf) { | |
870631a2 | 379 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
3f750a2f FD |
380 | bt_field_destroy((void *) string_field); |
381 | string_field = NULL; | |
44c440bc PP |
382 | goto end; |
383 | } | |
cb6f1f7d | 384 | |
44c440bc PP |
385 | g_array_index(string_field->buf, char, 0) = '\0'; |
386 | BT_LIB_LOGD("Created string field object: %!+f", string_field); | |
cb6f1f7d | 387 | |
44c440bc PP |
388 | end: |
389 | return (void *) string_field; | |
390 | } | |
cb6f1f7d | 391 | |
44c440bc | 392 | static inline |
5cd6d0e5 PP |
393 | int create_fields_from_named_field_classes( |
394 | struct bt_field_class_named_field_class_container *fc, | |
44c440bc PP |
395 | GPtrArray **fields) |
396 | { | |
397 | int ret = 0; | |
398 | uint64_t i; | |
cb6f1f7d | 399 | |
44c440bc PP |
400 | *fields = g_ptr_array_new_with_free_func( |
401 | (GDestroyNotify) bt_field_destroy); | |
402 | if (!*fields) { | |
870631a2 | 403 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
44c440bc PP |
404 | ret = -1; |
405 | goto end; | |
cb6f1f7d PP |
406 | } |
407 | ||
5cd6d0e5 | 408 | g_ptr_array_set_size(*fields, fc->named_fcs->len); |
44c440bc | 409 | |
5cd6d0e5 | 410 | for (i = 0; i < fc->named_fcs->len; i++) { |
44c440bc | 411 | struct bt_field *field; |
45c51519 | 412 | struct bt_named_field_class *named_fc = fc->named_fcs->pdata[i]; |
44c440bc | 413 | |
5cd6d0e5 | 414 | field = bt_field_create(named_fc->fc); |
44c440bc | 415 | if (!field) { |
870631a2 PP |
416 | BT_LIB_LOGE_APPEND_CAUSE( |
417 | "Failed to create structure member or variant option field: " | |
5cd6d0e5 PP |
418 | "name=\"%s\", %![fc-]+F", |
419 | named_fc->name->str, named_fc->fc); | |
44c440bc PP |
420 | ret = -1; |
421 | goto end; | |
422 | } | |
423 | ||
424 | g_ptr_array_index(*fields, i) = field; | |
425 | } | |
cb6f1f7d PP |
426 | |
427 | end: | |
428 | return ret; | |
273b65be JG |
429 | } |
430 | ||
44c440bc | 431 | static |
5cd6d0e5 | 432 | struct bt_field *create_structure_field(struct bt_field_class *fc) |
cd95e351 | 433 | { |
44c440bc | 434 | struct bt_field_structure *struct_field; |
cb6f1f7d | 435 | |
5cd6d0e5 | 436 | BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc); |
44c440bc PP |
437 | struct_field = g_new0(struct bt_field_structure, 1); |
438 | if (!struct_field) { | |
870631a2 PP |
439 | BT_LIB_LOGE_APPEND_CAUSE( |
440 | "Failed to allocate one structure field."); | |
44c440bc PP |
441 | goto end; |
442 | } | |
fc25abce | 443 | |
5cd6d0e5 | 444 | init_field((void *) struct_field, fc, &structure_field_methods); |
44c440bc | 445 | |
5cd6d0e5 | 446 | if (create_fields_from_named_field_classes((void *) fc, |
44c440bc | 447 | &struct_field->fields)) { |
870631a2 PP |
448 | BT_LIB_LOGE_APPEND_CAUSE( |
449 | "Cannot create structure member fields: %![fc-]+F", fc); | |
3f750a2f FD |
450 | bt_field_destroy((void *) struct_field); |
451 | struct_field = NULL; | |
44c440bc | 452 | goto end; |
cb6f1f7d PP |
453 | } |
454 | ||
44c440bc | 455 | BT_LIB_LOGD("Created structure field object: %!+f", struct_field); |
cb6f1f7d | 456 | |
44c440bc PP |
457 | end: |
458 | return (void *) struct_field; | |
cd95e351 JG |
459 | } |
460 | ||
b38aea74 PP |
461 | static |
462 | struct bt_field *create_option_field(struct bt_field_class *fc) | |
463 | { | |
464 | struct bt_field_option *opt_field; | |
465 | struct bt_field_class_option *opt_fc = (void *) fc; | |
466 | ||
467 | BT_LIB_LOGD("Creating option field object: %![fc-]+F", fc); | |
468 | opt_field = g_new0(struct bt_field_option, 1); | |
469 | if (!opt_field) { | |
470 | BT_LIB_LOGE_APPEND_CAUSE( | |
471 | "Failed to allocate one option field."); | |
472 | goto end; | |
473 | } | |
474 | ||
475 | init_field((void *) opt_field, fc, &option_field_methods); | |
476 | opt_field->content_field = bt_field_create(opt_fc->content_fc); | |
477 | if (!opt_field->content_field) { | |
478 | BT_LIB_LOGE_APPEND_CAUSE( | |
479 | "Failed to create option field's content field: " | |
480 | "%![opt-fc-]+F, %![content-fc-]+F", | |
481 | opt_fc, opt_fc->content_fc); | |
3f750a2f FD |
482 | bt_field_destroy((void *) opt_field); |
483 | opt_field = NULL; | |
b38aea74 PP |
484 | goto end; |
485 | } | |
486 | ||
487 | BT_LIB_LOGD("Created option field object: %!+f", opt_field); | |
488 | ||
489 | end: | |
490 | return (void *) opt_field; | |
491 | } | |
492 | ||
44c440bc | 493 | static |
5cd6d0e5 | 494 | struct bt_field *create_variant_field(struct bt_field_class *fc) |
273b65be | 495 | { |
44c440bc | 496 | struct bt_field_variant *var_field; |
cb6f1f7d | 497 | |
5cd6d0e5 | 498 | BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc); |
44c440bc PP |
499 | var_field = g_new0(struct bt_field_variant, 1); |
500 | if (!var_field) { | |
870631a2 PP |
501 | BT_LIB_LOGE_APPEND_CAUSE( |
502 | "Failed to allocate one variant field."); | |
44c440bc PP |
503 | goto end; |
504 | } | |
f6ccaed9 | 505 | |
5cd6d0e5 | 506 | init_field((void *) var_field, fc, &variant_field_methods); |
cb6f1f7d | 507 | |
5cd6d0e5 | 508 | if (create_fields_from_named_field_classes((void *) fc, |
44c440bc | 509 | &var_field->fields)) { |
870631a2 | 510 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: " |
5cd6d0e5 | 511 | "%![fc-]+F", fc); |
3f750a2f FD |
512 | bt_field_destroy((void *) var_field); |
513 | var_field = NULL; | |
44c440bc PP |
514 | goto end; |
515 | } | |
273b65be | 516 | |
44c440bc | 517 | BT_LIB_LOGD("Created variant field object: %!+f", var_field); |
cb6f1f7d | 518 | |
44c440bc PP |
519 | end: |
520 | return (void *) var_field; | |
cb6f1f7d PP |
521 | } |
522 | ||
523 | static inline | |
44c440bc | 524 | int init_array_field_fields(struct bt_field_array *array_field) |
cb6f1f7d PP |
525 | { |
526 | int ret = 0; | |
44c440bc | 527 | uint64_t i; |
5cd6d0e5 | 528 | struct bt_field_class_array *array_fc; |
cb6f1f7d | 529 | |
44c440bc | 530 | BT_ASSERT(array_field); |
5cd6d0e5 | 531 | array_fc = (void *) array_field->common.class; |
44c440bc PP |
532 | array_field->fields = g_ptr_array_sized_new(array_field->length); |
533 | if (!array_field->fields) { | |
870631a2 | 534 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
cb6f1f7d PP |
535 | ret = -1; |
536 | goto end; | |
537 | } | |
538 | ||
44c440bc PP |
539 | g_ptr_array_set_free_func(array_field->fields, |
540 | (GDestroyNotify) bt_field_destroy); | |
541 | g_ptr_array_set_size(array_field->fields, array_field->length); | |
542 | ||
543 | for (i = 0; i < array_field->length; i++) { | |
544 | array_field->fields->pdata[i] = bt_field_create( | |
5cd6d0e5 | 545 | array_fc->element_fc); |
44c440bc | 546 | if (!array_field->fields->pdata[i]) { |
870631a2 PP |
547 | BT_LIB_LOGE_APPEND_CAUSE( |
548 | "Cannot create array field's element field: " | |
5cd6d0e5 | 549 | "index=%" PRIu64 ", %![fc-]+F", i, array_fc); |
44c440bc PP |
550 | ret = -1; |
551 | goto end; | |
552 | } | |
553 | } | |
cb6f1f7d PP |
554 | |
555 | end: | |
556 | return ret; | |
3f4a108d PP |
557 | } |
558 | ||
44c440bc | 559 | static |
5cd6d0e5 | 560 | struct bt_field *create_static_array_field(struct bt_field_class *fc) |
f78d67fb | 561 | { |
9c08c816 | 562 | struct bt_field_class_array_static *array_fc = (void *) fc; |
44c440bc | 563 | struct bt_field_array *array_field; |
312c056a | 564 | |
5cd6d0e5 | 565 | BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc); |
44c440bc PP |
566 | array_field = g_new0(struct bt_field_array, 1); |
567 | if (!array_field) { | |
870631a2 PP |
568 | BT_LIB_LOGE_APPEND_CAUSE( |
569 | "Failed to allocate one static array field."); | |
44c440bc PP |
570 | goto end; |
571 | } | |
f78d67fb | 572 | |
5cd6d0e5 PP |
573 | init_field((void *) array_field, fc, &array_field_methods); |
574 | array_field->length = array_fc->length; | |
cb6f1f7d | 575 | |
44c440bc | 576 | if (init_array_field_fields(array_field)) { |
870631a2 | 577 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: " |
5cd6d0e5 | 578 | "%![fc-]+F", fc); |
3f750a2f FD |
579 | bt_field_destroy((void *) array_field); |
580 | array_field = NULL; | |
44c440bc PP |
581 | goto end; |
582 | } | |
312c056a | 583 | |
44c440bc | 584 | BT_LIB_LOGD("Created static array field object: %!+f", array_field); |
cb6f1f7d | 585 | |
44c440bc PP |
586 | end: |
587 | return (void *) array_field; | |
273b65be JG |
588 | } |
589 | ||
44c440bc | 590 | static |
5cd6d0e5 | 591 | struct bt_field *create_dynamic_array_field(struct bt_field_class *fc) |
cd95e351 | 592 | { |
44c440bc | 593 | struct bt_field_array *array_field; |
312c056a | 594 | |
5cd6d0e5 | 595 | BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc); |
44c440bc PP |
596 | array_field = g_new0(struct bt_field_array, 1); |
597 | if (!array_field) { | |
870631a2 PP |
598 | BT_LIB_LOGE_APPEND_CAUSE( |
599 | "Failed to allocate one dynamic array field."); | |
44c440bc PP |
600 | goto end; |
601 | } | |
602 | ||
5cd6d0e5 | 603 | init_field((void *) array_field, fc, &array_field_methods); |
44c440bc PP |
604 | |
605 | if (init_array_field_fields(array_field)) { | |
870631a2 | 606 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: " |
5cd6d0e5 | 607 | "%![fc-]+F", fc); |
3f750a2f FD |
608 | bt_field_destroy((void *) array_field); |
609 | array_field = NULL; | |
44c440bc | 610 | goto end; |
cb6f1f7d PP |
611 | } |
612 | ||
44c440bc PP |
613 | BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field); |
614 | ||
615 | end: | |
616 | return (void *) array_field; | |
312c056a PP |
617 | } |
618 | ||
5cebbe7f PP |
619 | bt_bool bt_field_bool_get_value(const struct bt_field *field) |
620 | { | |
621 | const struct bt_field_bool *bool_field = (const void *) field; | |
622 | ||
623 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
624 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
625 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL, | |
626 | "Field"); | |
627 | return (bt_bool) bool_field->value; | |
628 | } | |
629 | ||
630 | void bt_field_bool_set_value(struct bt_field *field, bt_bool value) | |
631 | { | |
632 | struct bt_field_bool *bool_field = (void *) field; | |
633 | ||
634 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
635 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL, | |
636 | "Field"); | |
637 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
638 | bool_field->value = (bool) value; | |
639 | bt_field_set_single(field, true); | |
640 | } | |
641 | ||
1094efa4 PP |
642 | uint64_t bt_field_bit_array_get_value_as_integer(const struct bt_field *field) |
643 | { | |
644 | const struct bt_field_bit_array *ba_field = (const void *) field; | |
645 | ||
646 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
647 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
648 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
649 | BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field"); | |
650 | return ba_field->value_as_int; | |
651 | } | |
652 | ||
653 | void bt_field_bit_array_set_value_as_integer(struct bt_field *field, | |
654 | uint64_t value) | |
655 | { | |
656 | struct bt_field_bit_array *ba_field = (void *) field; | |
657 | struct bt_field_class_bit_array *ba_fc; | |
658 | ||
659 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
660 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
661 | BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field"); | |
662 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
663 | ba_fc = (void *) field->class; | |
664 | ba_field->value_as_int = value; | |
665 | ||
666 | if (ba_fc->length < 64) { | |
667 | /* Apply mask */ | |
668 | ba_field->value_as_int &= ((UINT64_C(1) << ba_fc->length) - 1); | |
669 | } | |
670 | ||
671 | bt_field_set_single(field, true); | |
672 | } | |
673 | ||
9c08c816 | 674 | int64_t bt_field_integer_signed_get_value(const struct bt_field *field) |
312c056a | 675 | { |
40f4ba76 | 676 | const struct bt_field_integer *int_field = (const void *) field; |
312c056a | 677 | |
bdb288b3 PP |
678 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
679 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
680 | BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field"); | |
44c440bc | 681 | return int_field->value.i; |
cd95e351 JG |
682 | } |
683 | ||
9c08c816 | 684 | void bt_field_integer_signed_set_value(struct bt_field *field, int64_t value) |
cd95e351 | 685 | { |
44c440bc | 686 | struct bt_field_integer *int_field = (void *) field; |
312c056a | 687 | |
bdb288b3 PP |
688 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
689 | BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field"); | |
690 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
691 | BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed( | |
5cd6d0e5 | 692 | ((struct bt_field_class_integer *) field->class)->range, value), |
44c440bc | 693 | "Value is out of bounds: value=%" PRId64 ", %![field-]+f, " |
5cd6d0e5 | 694 | "%![fc-]+F", value, field, field->class); |
44c440bc PP |
695 | int_field->value.i = value; |
696 | bt_field_set_single(field, true); | |
cd95e351 JG |
697 | } |
698 | ||
9c08c816 | 699 | uint64_t bt_field_integer_unsigned_get_value(const struct bt_field *field) |
273b65be | 700 | { |
40f4ba76 | 701 | const struct bt_field_integer *int_field = (const void *) field; |
44c440bc | 702 | |
bdb288b3 PP |
703 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
704 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
705 | BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field"); | |
44c440bc | 706 | return int_field->value.u; |
273b65be JG |
707 | } |
708 | ||
9c08c816 | 709 | void bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value) |
cd95e351 | 710 | { |
44c440bc | 711 | struct bt_field_integer *int_field = (void *) field; |
312c056a | 712 | |
bdb288b3 PP |
713 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
714 | BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field"); | |
715 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
716 | BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned( | |
5cd6d0e5 | 717 | ((struct bt_field_class_integer *) field->class)->range, value), |
44c440bc | 718 | "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, " |
5cd6d0e5 | 719 | "%![fc-]+F", value, field, field->class); |
44c440bc PP |
720 | int_field->value.u = value; |
721 | bt_field_set_single(field, true); | |
cd95e351 JG |
722 | } |
723 | ||
fe4df857 FD |
724 | float bt_field_real_single_precision_get_value(const struct bt_field *field) |
725 | { | |
726 | const struct bt_field_real *real_field = (const void *) field; | |
727 | ||
728 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
729 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
730 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
731 | BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field"); | |
732 | return (float) real_field->value; | |
733 | } | |
734 | ||
735 | double bt_field_real_double_precision_get_value(const struct bt_field *field) | |
273b65be | 736 | { |
40f4ba76 | 737 | const struct bt_field_real *real_field = (const void *) field; |
44c440bc | 738 | |
bdb288b3 PP |
739 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
740 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
fe4df857 FD |
741 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
742 | BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field"); | |
743 | ||
44c440bc | 744 | return real_field->value; |
f6ccaed9 PP |
745 | } |
746 | ||
fe4df857 FD |
747 | void bt_field_real_single_precision_set_value(struct bt_field *field, |
748 | float value) | |
f6ccaed9 | 749 | { |
44c440bc | 750 | struct bt_field_real *real_field = (void *) field; |
cb6f1f7d | 751 | |
bdb288b3 | 752 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
fe4df857 FD |
753 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
754 | BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field"); | |
bdb288b3 | 755 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
fe4df857 FD |
756 | |
757 | real_field->value = (double) value; | |
758 | bt_field_set_single(field, true); | |
759 | } | |
760 | ||
761 | void bt_field_real_double_precision_set_value(struct bt_field *field, | |
762 | double value) | |
763 | { | |
764 | struct bt_field_real *real_field = (void *) field; | |
765 | ||
766 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
767 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
768 | BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field"); | |
769 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
770 | ||
44c440bc PP |
771 | real_field->value = value; |
772 | bt_field_set_single(field, true); | |
773 | } | |
774 | ||
d24d5663 | 775 | enum bt_field_enumeration_get_mapping_labels_status |
9c08c816 | 776 | bt_field_enumeration_unsigned_get_mapping_labels( |
40f4ba76 | 777 | const struct bt_field *field, |
5cd6d0e5 | 778 | bt_field_class_enumeration_mapping_label_array *label_array, |
44c440bc PP |
779 | uint64_t *count) |
780 | { | |
40f4ba76 | 781 | const struct bt_field_integer *int_field = (const void *) field; |
44c440bc | 782 | |
17f3083a | 783 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 PP |
784 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
785 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); | |
786 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)"); | |
787 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
788 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
864cad70 | 789 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field"); |
743eec93 | 790 | return (int) |
9c08c816 | 791 | bt_field_class_enumeration_unsigned_get_mapping_labels_for_value( |
743eec93 | 792 | field->class, int_field->value.u, label_array, count); |
273b65be JG |
793 | } |
794 | ||
d24d5663 | 795 | enum bt_field_enumeration_get_mapping_labels_status |
9c08c816 | 796 | bt_field_enumeration_signed_get_mapping_labels( |
40f4ba76 | 797 | const struct bt_field *field, |
5cd6d0e5 | 798 | bt_field_class_enumeration_mapping_label_array *label_array, |
44c440bc | 799 | uint64_t *count) |
cd95e351 | 800 | { |
40f4ba76 | 801 | const struct bt_field_integer *int_field = (const void *) field; |
cb6f1f7d | 802 | |
17f3083a | 803 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 PP |
804 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
805 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); | |
806 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)"); | |
807 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
808 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
864cad70 | 809 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field"); |
743eec93 | 810 | return (int) |
9c08c816 | 811 | bt_field_class_enumeration_signed_get_mapping_labels_for_value( |
743eec93 | 812 | field->class, int_field->value.i, label_array, count); |
f6ccaed9 | 813 | } |
fc25abce | 814 | |
40f4ba76 | 815 | const char *bt_field_string_get_value(const struct bt_field *field) |
f6ccaed9 | 816 | { |
40f4ba76 | 817 | const struct bt_field_string *string_field = (const void *) field; |
44c440bc | 818 | |
bdb288b3 PP |
819 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
820 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
821 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, | |
44c440bc PP |
822 | "Field"); |
823 | return (const char *) string_field->buf->data; | |
824 | } | |
825 | ||
40f4ba76 | 826 | uint64_t bt_field_string_get_length(const struct bt_field *field) |
44c440bc | 827 | { |
40f4ba76 | 828 | const struct bt_field_string *string_field = (const void *) field; |
cb6f1f7d | 829 | |
bdb288b3 PP |
830 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
831 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
832 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, | |
44c440bc PP |
833 | "Field"); |
834 | return string_field->length; | |
cd95e351 JG |
835 | } |
836 | ||
743eec93 PP |
837 | static inline |
838 | void clear_string_field(struct bt_field *field) | |
839 | { | |
840 | struct bt_field_string *string_field = (void *) field; | |
841 | ||
98b15851 | 842 | BT_ASSERT_DBG(field); |
743eec93 PP |
843 | string_field->length = 0; |
844 | bt_field_set_single(field, true); | |
845 | } | |
846 | ||
d24d5663 PP |
847 | enum bt_field_string_set_value_status bt_field_string_set_value( |
848 | struct bt_field *field, const char *value) | |
273b65be | 849 | { |
17f3083a | 850 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 PP |
851 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
852 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); | |
853 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
854 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, | |
44c440bc | 855 | "Field"); |
743eec93 | 856 | clear_string_field(field); |
d24d5663 | 857 | return (int) bt_field_string_append_with_length(field, value, |
44c440bc | 858 | (uint64_t) strlen(value)); |
273b65be JG |
859 | } |
860 | ||
d24d5663 PP |
861 | enum bt_field_string_append_status bt_field_string_append( |
862 | struct bt_field *field, const char *value) | |
cd95e351 | 863 | { |
17f3083a SM |
864 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
865 | ||
40f4ba76 | 866 | return bt_field_string_append_with_length(field, |
e5be10ef | 867 | value, (uint64_t) strlen(value)); |
cd95e351 JG |
868 | } |
869 | ||
d24d5663 PP |
870 | enum bt_field_string_append_status bt_field_string_append_with_length( |
871 | struct bt_field *field, const char *value, uint64_t length) | |
273b65be | 872 | { |
cb6f1f7d PP |
873 | struct bt_field_string *string_field = (void *) field; |
874 | char *data; | |
44c440bc | 875 | uint64_t new_length; |
273b65be | 876 | |
17f3083a | 877 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 PP |
878 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
879 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); | |
880 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
881 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
864cad70 | 882 | BT_FIELD_CLASS_TYPE_STRING, "Field"); |
312c056a | 883 | |
cb6f1f7d | 884 | /* Make sure no null bytes are appended */ |
5084732e | 885 | BT_ASSERT_PRE_DEV(!memchr(value, '\0', length), |
cb6f1f7d | 886 | "String value to append contains a null character: " |
44c440bc | 887 | "partial-value=\"%.32s\", length=%" PRIu64, value, length); |
c6f9c5a3 | 888 | |
44c440bc | 889 | new_length = length + string_field->length; |
cb6f1f7d | 890 | |
91d81473 | 891 | if (G_UNLIKELY(new_length + 1 > string_field->buf->len)) { |
44c440bc | 892 | g_array_set_size(string_field->buf, new_length + 1); |
c6f9c5a3 PP |
893 | } |
894 | ||
cb6f1f7d | 895 | data = string_field->buf->data; |
44c440bc PP |
896 | memcpy(data + string_field->length, value, length); |
897 | ((char *) string_field->buf->data)[new_length] = '\0'; | |
898 | string_field->length = new_length; | |
899 | bt_field_set_single(field, true); | |
d24d5663 | 900 | return BT_FUNC_STATUS_OK; |
cb6f1f7d | 901 | } |
3dca2276 | 902 | |
d24d5663 | 903 | void bt_field_string_clear(struct bt_field *field) |
cb6f1f7d | 904 | { |
bdb288b3 PP |
905 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
906 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
907 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
864cad70 | 908 | BT_FIELD_CLASS_TYPE_STRING, "Field"); |
743eec93 | 909 | clear_string_field(field); |
cb6f1f7d PP |
910 | } |
911 | ||
40f4ba76 | 912 | uint64_t bt_field_array_get_length(const struct bt_field *field) |
cb6f1f7d | 913 | { |
40f4ba76 | 914 | const struct bt_field_array *array_field = (const void *) field; |
c6f9c5a3 | 915 | |
bdb288b3 PP |
916 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
917 | BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field"); | |
44c440bc | 918 | return array_field->length; |
3dca2276 | 919 | } |
f98c6554 | 920 | |
9c08c816 | 921 | enum bt_field_array_dynamic_set_length_status bt_field_array_dynamic_set_length( |
d24d5663 | 922 | struct bt_field *field, uint64_t length) |
3dca2276 | 923 | { |
d24d5663 | 924 | int ret = BT_FUNC_STATUS_OK; |
44c440bc | 925 | struct bt_field_array *array_field = (void *) field; |
f98c6554 | 926 | |
17f3083a | 927 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 | 928 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
81b8fa44 | 929 | BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(field, "Field"); |
bdb288b3 | 930 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
273b65be | 931 | |
91d81473 | 932 | if (G_UNLIKELY(length > array_field->fields->len)) { |
44c440bc | 933 | /* Make more room */ |
5cd6d0e5 | 934 | struct bt_field_class_array *array_fc; |
44c440bc PP |
935 | uint64_t cur_len = array_field->fields->len; |
936 | uint64_t i; | |
cb6f1f7d | 937 | |
44c440bc | 938 | g_ptr_array_set_size(array_field->fields, length); |
5cd6d0e5 | 939 | array_fc = (void *) field->class; |
cb6f1f7d | 940 | |
44c440bc PP |
941 | for (i = cur_len; i < array_field->fields->len; i++) { |
942 | struct bt_field *elem_field = bt_field_create( | |
5cd6d0e5 | 943 | array_fc->element_fc); |
273b65be | 944 | |
44c440bc | 945 | if (!elem_field) { |
870631a2 PP |
946 | BT_LIB_LOGE_APPEND_CAUSE( |
947 | "Cannot create element field for " | |
44c440bc PP |
948 | "dynamic array field: " |
949 | "index=%" PRIu64 ", " | |
950 | "%![array-field-]+f", i, field); | |
d24d5663 | 951 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
44c440bc PP |
952 | goto end; |
953 | } | |
c58b9c62 | 954 | |
98b15851 | 955 | BT_ASSERT_DBG(!array_field->fields->pdata[i]); |
44c440bc | 956 | array_field->fields->pdata[i] = elem_field; |
c58b9c62 | 957 | } |
c58b9c62 JG |
958 | } |
959 | ||
44c440bc | 960 | array_field->length = length; |
3dca2276 | 961 | |
273b65be | 962 | end: |
c58b9c62 | 963 | return ret; |
273b65be JG |
964 | } |
965 | ||
40f4ba76 PP |
966 | static inline |
967 | struct bt_field *borrow_array_field_element_field_by_index( | |
44c440bc | 968 | struct bt_field *field, uint64_t index) |
312c056a | 969 | { |
44c440bc | 970 | struct bt_field_array *array_field = (void *) field; |
312c056a | 971 | |
bdb288b3 PP |
972 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
973 | BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field"); | |
974 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, array_field->length); | |
44c440bc | 975 | return array_field->fields->pdata[index]; |
312c056a PP |
976 | } |
977 | ||
40f4ba76 PP |
978 | struct bt_field *bt_field_array_borrow_element_field_by_index( |
979 | struct bt_field *field, uint64_t index) | |
e5be10ef | 980 | { |
40f4ba76 | 981 | return borrow_array_field_element_field_by_index(field, index); |
e5be10ef PP |
982 | } |
983 | ||
40f4ba76 PP |
984 | const struct bt_field * |
985 | bt_field_array_borrow_element_field_by_index_const( | |
986 | const struct bt_field *field, uint64_t index) | |
987 | { | |
988 | return borrow_array_field_element_field_by_index((void *) field, index); | |
989 | } | |
990 | ||
991 | static inline | |
992 | struct bt_field *borrow_structure_field_member_field_by_index( | |
44c440bc | 993 | struct bt_field *field, uint64_t index) |
4d4b475d | 994 | { |
44c440bc | 995 | struct bt_field_structure *struct_field = (void *) field; |
4d4b475d | 996 | |
bdb288b3 PP |
997 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
998 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
864cad70 | 999 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); |
bdb288b3 | 1000 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, struct_field->fields->len); |
44c440bc | 1001 | return struct_field->fields->pdata[index]; |
4d4b475d PP |
1002 | } |
1003 | ||
40f4ba76 PP |
1004 | struct bt_field *bt_field_structure_borrow_member_field_by_index( |
1005 | struct bt_field *field, uint64_t index) | |
1006 | { | |
1007 | return borrow_structure_field_member_field_by_index(field, | |
1008 | index); | |
1009 | } | |
1010 | ||
1011 | const struct bt_field * | |
1012 | bt_field_structure_borrow_member_field_by_index_const( | |
1013 | const struct bt_field *field, uint64_t index) | |
e5be10ef | 1014 | { |
40f4ba76 | 1015 | return borrow_structure_field_member_field_by_index( |
e5be10ef PP |
1016 | (void *) field, index); |
1017 | } | |
1018 | ||
40f4ba76 PP |
1019 | static inline |
1020 | struct bt_field *borrow_structure_field_member_field_by_name( | |
44c440bc | 1021 | struct bt_field *field, const char *name) |
273b65be | 1022 | { |
44c440bc | 1023 | struct bt_field *ret_field = NULL; |
5cd6d0e5 | 1024 | struct bt_field_class_structure *struct_fc; |
44c440bc PP |
1025 | struct bt_field_structure *struct_field = (void *) field; |
1026 | gpointer orig_key; | |
1027 | gpointer index; | |
fc25abce | 1028 | |
bdb288b3 PP |
1029 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
1030 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Field name"); | |
1031 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
864cad70 | 1032 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); |
5cd6d0e5 | 1033 | struct_fc = (void *) field->class; |
312c056a | 1034 | |
5cd6d0e5 | 1035 | if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name, |
44c440bc | 1036 | &orig_key, &index)) { |
312c056a | 1037 | goto end; |
fc25abce PP |
1038 | } |
1039 | ||
44c440bc | 1040 | ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)]; |
98b15851 | 1041 | BT_ASSERT_DBG(ret_field); |
312c056a PP |
1042 | |
1043 | end: | |
44c440bc | 1044 | return ret_field; |
273b65be JG |
1045 | } |
1046 | ||
40f4ba76 PP |
1047 | struct bt_field *bt_field_structure_borrow_member_field_by_name( |
1048 | struct bt_field *field, const char *name) | |
1049 | { | |
1050 | return borrow_structure_field_member_field_by_name(field, name); | |
1051 | } | |
1052 | ||
1053 | const struct bt_field *bt_field_structure_borrow_member_field_by_name_const( | |
1054 | const struct bt_field *field, const char *name) | |
e5be10ef | 1055 | { |
40f4ba76 | 1056 | return borrow_structure_field_member_field_by_name( |
e5be10ef PP |
1057 | (void *) field, name); |
1058 | } | |
1059 | ||
b38aea74 PP |
1060 | void bt_field_option_set_has_field(struct bt_field *field, bt_bool has_field) |
1061 | { | |
1062 | struct bt_field_option *opt_field = (void *) field; | |
1063 | ||
1064 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
0aa006b7 | 1065 | BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(field, "Field"); |
b38aea74 PP |
1066 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
1067 | ||
1068 | if (has_field) { | |
1069 | opt_field->selected_field = opt_field->content_field; | |
1070 | } else { | |
1071 | opt_field->selected_field = NULL; | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | struct bt_field *bt_field_option_borrow_field(struct bt_field *field) | |
1076 | { | |
1077 | struct bt_field_option *opt_field = (void *) field; | |
1078 | ||
1079 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
0aa006b7 | 1080 | BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(field, "Field"); |
b38aea74 PP |
1081 | return opt_field->selected_field; |
1082 | } | |
1083 | ||
1084 | const struct bt_field *bt_field_option_borrow_field_const( | |
1085 | const struct bt_field *field) | |
1086 | { | |
1087 | return (const void *) bt_field_option_borrow_field((void *) field); | |
1088 | } | |
1089 | ||
40f4ba76 PP |
1090 | static inline |
1091 | struct bt_field *borrow_variant_field_selected_option_field( | |
44c440bc | 1092 | struct bt_field *field) |
273b65be | 1093 | { |
44c440bc | 1094 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 1095 | |
bdb288b3 | 1096 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
45c51519 | 1097 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
bdb288b3 | 1098 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
44c440bc PP |
1099 | "Variant field has no selected field: %!+f", field); |
1100 | return var_field->selected_field; | |
273b65be JG |
1101 | } |
1102 | ||
40f4ba76 PP |
1103 | struct bt_field *bt_field_variant_borrow_selected_option_field( |
1104 | struct bt_field *field) | |
1105 | { | |
1106 | return borrow_variant_field_selected_option_field(field); | |
1107 | } | |
1108 | ||
1109 | const struct bt_field *bt_field_variant_borrow_selected_option_field_const( | |
1110 | const struct bt_field *field) | |
273b65be | 1111 | { |
40f4ba76 | 1112 | return borrow_variant_field_selected_option_field((void *) field); |
e5be10ef PP |
1113 | } |
1114 | ||
45c51519 PP |
1115 | static |
1116 | const struct bt_field_class_variant_option * | |
1117 | borrow_variant_field_selected_class_option(const struct bt_field *field) | |
1118 | { | |
1119 | const struct bt_field_class_named_field_class_container *container_fc; | |
1120 | const struct bt_field_variant *var_field = (const void *) field; | |
1121 | ||
98b15851 | 1122 | BT_ASSERT_DBG(field); |
45c51519 PP |
1123 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
1124 | "Variant field has no selected field: %!+f", field); | |
1125 | container_fc = (const void *) field->class; | |
1126 | return container_fc->named_fcs->pdata[var_field->selected_index]; | |
1127 | } | |
1128 | ||
1129 | const struct bt_field_class_variant_option * | |
7b4311c1 | 1130 | bt_field_variant_borrow_selected_option_class_const( |
45c51519 PP |
1131 | const struct bt_field *field) |
1132 | { | |
1133 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
1134 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); | |
1135 | return borrow_variant_field_selected_class_option(field); | |
1136 | } | |
1137 | ||
de821fe5 | 1138 | const struct bt_field_class_variant_with_selector_field_integer_unsigned_option * |
7b4311c1 | 1139 | bt_field_variant_with_selector_field_integer_unsigned_borrow_selected_option_class_const( |
45c51519 PP |
1140 | const struct bt_field *field) |
1141 | { | |
1142 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
1143 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
de821fe5 | 1144 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD, "Field"); |
45c51519 PP |
1145 | return (const void *) borrow_variant_field_selected_class_option(field); |
1146 | } | |
1147 | ||
de821fe5 | 1148 | const struct bt_field_class_variant_with_selector_field_integer_signed_option * |
7b4311c1 | 1149 | bt_field_variant_with_selector_field_integer_signed_borrow_selected_option_class_const( |
45c51519 PP |
1150 | const struct bt_field *field) |
1151 | { | |
1152 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
1153 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
de821fe5 | 1154 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, "Field"); |
45c51519 PP |
1155 | return (const void *) borrow_variant_field_selected_class_option(field); |
1156 | } | |
1157 | ||
7b4311c1 PP |
1158 | enum bt_field_variant_select_option_by_index_status |
1159 | bt_field_variant_select_option_by_index( | |
40f4ba76 | 1160 | struct bt_field *field, uint64_t index) |
e5be10ef | 1161 | { |
44c440bc | 1162 | struct bt_field_variant *var_field = (void *) field; |
fc25abce | 1163 | |
17f3083a | 1164 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 | 1165 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
45c51519 | 1166 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
bdb288b3 PP |
1167 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
1168 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, var_field->fields->len); | |
44c440bc PP |
1169 | var_field->selected_field = var_field->fields->pdata[index]; |
1170 | var_field->selected_index = index; | |
d24d5663 | 1171 | return BT_FUNC_STATUS_OK; |
273b65be JG |
1172 | } |
1173 | ||
7b4311c1 | 1174 | uint64_t bt_field_variant_get_selected_option_index( |
40f4ba76 | 1175 | const struct bt_field *field) |
312c056a | 1176 | { |
40f4ba76 | 1177 | const struct bt_field_variant *var_field = (const void *) field; |
312c056a | 1178 | |
bdb288b3 | 1179 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
45c51519 | 1180 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
bdb288b3 | 1181 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
44c440bc PP |
1182 | "Variant field has no selected field: %!+f", field); |
1183 | return var_field->selected_index; | |
312c056a PP |
1184 | } |
1185 | ||
44c440bc PP |
1186 | static inline |
1187 | void bt_field_finalize(struct bt_field *field) | |
273b65be | 1188 | { |
44c440bc | 1189 | BT_ASSERT(field); |
5cd6d0e5 | 1190 | BT_LOGD_STR("Putting field's class."); |
238b7404 | 1191 | BT_OBJECT_PUT_REF_AND_RESET(field->class); |
273b65be JG |
1192 | } |
1193 | ||
5cebbe7f PP |
1194 | static |
1195 | void destroy_bool_field(struct bt_field *field) | |
1196 | { | |
1197 | BT_ASSERT(field); | |
1198 | BT_LIB_LOGD("Destroying boolean field object: %!+f", field); | |
1199 | bt_field_finalize(field); | |
1200 | g_free(field); | |
1201 | } | |
1202 | ||
1094efa4 PP |
1203 | static |
1204 | void destroy_bit_array_field(struct bt_field *field) | |
1205 | { | |
1206 | BT_ASSERT(field); | |
1207 | BT_LIB_LOGD("Destroying bit array field object: %!+f", field); | |
1208 | bt_field_finalize(field); | |
1209 | g_free(field); | |
1210 | } | |
1211 | ||
273b65be | 1212 | static |
44c440bc | 1213 | void destroy_integer_field(struct bt_field *field) |
273b65be | 1214 | { |
44c440bc PP |
1215 | BT_ASSERT(field); |
1216 | BT_LIB_LOGD("Destroying integer field object: %!+f", field); | |
1217 | bt_field_finalize(field); | |
1218 | g_free(field); | |
273b65be JG |
1219 | } |
1220 | ||
cb6f1f7d | 1221 | static |
44c440bc | 1222 | void destroy_real_field(struct bt_field *field) |
273b65be | 1223 | { |
44c440bc PP |
1224 | BT_ASSERT(field); |
1225 | BT_LIB_LOGD("Destroying real field object: %!+f", field); | |
1226 | bt_field_finalize(field); | |
1227 | g_free(field); | |
273b65be JG |
1228 | } |
1229 | ||
cb6f1f7d | 1230 | static |
44c440bc | 1231 | void destroy_structure_field(struct bt_field *field) |
273b65be | 1232 | { |
44c440bc | 1233 | struct bt_field_structure *struct_field = (void *) field; |
273b65be | 1234 | |
f6ccaed9 | 1235 | BT_ASSERT(field); |
44c440bc PP |
1236 | BT_LIB_LOGD("Destroying structure field object: %!+f", field); |
1237 | bt_field_finalize(field); | |
f6ccaed9 | 1238 | |
44c440bc PP |
1239 | if (struct_field->fields) { |
1240 | g_ptr_array_free(struct_field->fields, TRUE); | |
238b7404 | 1241 | struct_field->fields = NULL; |
273b65be | 1242 | } |
f6ccaed9 | 1243 | |
44c440bc | 1244 | g_free(field); |
273b65be JG |
1245 | } |
1246 | ||
b38aea74 PP |
1247 | static |
1248 | void destroy_option_field(struct bt_field *field) | |
1249 | { | |
1250 | struct bt_field_option *opt_field = (void *) field; | |
1251 | ||
1252 | BT_ASSERT(field); | |
1253 | BT_LIB_LOGD("Destroying option field object: %!+f", field); | |
1254 | bt_field_finalize(field); | |
1255 | ||
1256 | if (opt_field->content_field) { | |
1257 | bt_field_destroy(opt_field->content_field); | |
1258 | } | |
1259 | ||
1260 | g_free(field); | |
1261 | } | |
1262 | ||
cb6f1f7d | 1263 | static |
44c440bc | 1264 | void destroy_variant_field(struct bt_field *field) |
273b65be | 1265 | { |
44c440bc | 1266 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 1267 | |
f6ccaed9 | 1268 | BT_ASSERT(field); |
44c440bc PP |
1269 | BT_LIB_LOGD("Destroying variant field object: %!+f", field); |
1270 | bt_field_finalize(field); | |
312c056a | 1271 | |
44c440bc PP |
1272 | if (var_field->fields) { |
1273 | g_ptr_array_free(var_field->fields, TRUE); | |
238b7404 | 1274 | var_field->fields = NULL; |
fc25abce | 1275 | } |
f6ccaed9 | 1276 | |
44c440bc | 1277 | g_free(field); |
273b65be JG |
1278 | } |
1279 | ||
cb6f1f7d | 1280 | static |
44c440bc | 1281 | void destroy_array_field(struct bt_field *field) |
273b65be | 1282 | { |
44c440bc | 1283 | struct bt_field_array *array_field = (void *) field; |
273b65be | 1284 | |
f6ccaed9 | 1285 | BT_ASSERT(field); |
44c440bc PP |
1286 | BT_LIB_LOGD("Destroying array field object: %!+f", field); |
1287 | bt_field_finalize(field); | |
3dca2276 | 1288 | |
44c440bc PP |
1289 | if (array_field->fields) { |
1290 | g_ptr_array_free(array_field->fields, TRUE); | |
238b7404 | 1291 | array_field->fields = NULL; |
273b65be | 1292 | } |
f6ccaed9 | 1293 | |
44c440bc | 1294 | g_free(field); |
273b65be JG |
1295 | } |
1296 | ||
cb6f1f7d | 1297 | static |
44c440bc | 1298 | void destroy_string_field(struct bt_field *field) |
273b65be | 1299 | { |
44c440bc | 1300 | struct bt_field_string *string_field = (void *) field; |
273b65be | 1301 | |
f6ccaed9 | 1302 | BT_ASSERT(field); |
44c440bc PP |
1303 | BT_LIB_LOGD("Destroying string field object: %!+f", field); |
1304 | bt_field_finalize(field); | |
3dca2276 | 1305 | |
44c440bc PP |
1306 | if (string_field->buf) { |
1307 | g_array_free(string_field->buf, TRUE); | |
238b7404 | 1308 | string_field->buf = NULL; |
273b65be | 1309 | } |
44c440bc PP |
1310 | |
1311 | g_free(field); | |
273b65be JG |
1312 | } |
1313 | ||
44c440bc PP |
1314 | BT_HIDDEN |
1315 | void bt_field_destroy(struct bt_field *field) | |
12c8a1a3 | 1316 | { |
f6ccaed9 | 1317 | BT_ASSERT(field); |
9c3869a9 PP |
1318 | |
1319 | switch (field->class->type) { | |
1320 | case BT_FIELD_CLASS_TYPE_BOOL: | |
1321 | destroy_bool_field(field); | |
1322 | break; | |
1323 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: | |
1324 | destroy_bit_array_field(field); | |
1325 | break; | |
1326 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
1327 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
1328 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
1329 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
1330 | destroy_integer_field(field); | |
1331 | break; | |
1332 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: | |
1333 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: | |
1334 | destroy_real_field(field); | |
1335 | break; | |
1336 | case BT_FIELD_CLASS_TYPE_STRING: | |
1337 | destroy_string_field(field); | |
1338 | break; | |
1339 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1340 | destroy_structure_field(field); | |
1341 | break; | |
1342 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
1343 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: | |
1344 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: | |
1345 | destroy_array_field(field); | |
1346 | break; | |
1347 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: | |
1348 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: | |
1349 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
1350 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
1351 | destroy_option_field(field); | |
1352 | break; | |
1353 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: | |
1354 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
1355 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
1356 | destroy_variant_field(field); | |
1357 | break; | |
1358 | default: | |
498e7994 | 1359 | bt_common_abort(); |
9c3869a9 | 1360 | } |
12c8a1a3 JG |
1361 | } |
1362 | ||
cb6f1f7d | 1363 | static |
44c440bc | 1364 | void reset_single_field(struct bt_field *field) |
12c8a1a3 | 1365 | { |
98b15851 | 1366 | BT_ASSERT_DBG(field); |
44c440bc | 1367 | field->is_set = false; |
12c8a1a3 JG |
1368 | } |
1369 | ||
cb6f1f7d | 1370 | static |
44c440bc | 1371 | void reset_structure_field(struct bt_field *field) |
12c8a1a3 | 1372 | { |
44c440bc PP |
1373 | uint64_t i; |
1374 | struct bt_field_structure *struct_field = (void *) field; | |
12c8a1a3 | 1375 | |
98b15851 | 1376 | BT_ASSERT_DBG(field); |
44c440bc PP |
1377 | |
1378 | for (i = 0; i < struct_field->fields->len; i++) { | |
1379 | bt_field_reset(struct_field->fields->pdata[i]); | |
1380 | } | |
12c8a1a3 JG |
1381 | } |
1382 | ||
b38aea74 PP |
1383 | static |
1384 | void reset_option_field(struct bt_field *field) | |
1385 | { | |
1386 | struct bt_field_option *opt_field = (void *) field; | |
1387 | ||
98b15851 | 1388 | BT_ASSERT_DBG(opt_field); |
b38aea74 PP |
1389 | bt_field_reset(opt_field->content_field); |
1390 | opt_field->selected_field = NULL; | |
1391 | } | |
1392 | ||
cb6f1f7d | 1393 | static |
44c440bc | 1394 | void reset_variant_field(struct bt_field *field) |
12c8a1a3 | 1395 | { |
44c440bc PP |
1396 | uint64_t i; |
1397 | struct bt_field_variant *var_field = (void *) field; | |
12c8a1a3 | 1398 | |
98b15851 | 1399 | BT_ASSERT_DBG(field); |
f6ccaed9 | 1400 | |
44c440bc PP |
1401 | for (i = 0; i < var_field->fields->len; i++) { |
1402 | bt_field_reset(var_field->fields->pdata[i]); | |
12c8a1a3 | 1403 | } |
12c8a1a3 JG |
1404 | } |
1405 | ||
cb6f1f7d | 1406 | static |
44c440bc | 1407 | void reset_array_field(struct bt_field *field) |
12c8a1a3 | 1408 | { |
312c056a | 1409 | uint64_t i; |
44c440bc | 1410 | struct bt_field_array *array_field = (void *) field; |
12c8a1a3 | 1411 | |
98b15851 | 1412 | BT_ASSERT_DBG(field); |
f6ccaed9 | 1413 | |
44c440bc PP |
1414 | for (i = 0; i < array_field->fields->len; i++) { |
1415 | bt_field_reset(array_field->fields->pdata[i]); | |
12c8a1a3 | 1416 | } |
12c8a1a3 JG |
1417 | } |
1418 | ||
cb6f1f7d | 1419 | static |
44c440bc | 1420 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1421 | { |
312c056a | 1422 | field->frozen = is_frozen; |
918be005 PP |
1423 | } |
1424 | ||
cb6f1f7d | 1425 | static |
44c440bc | 1426 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1427 | { |
312c056a | 1428 | uint64_t i; |
44c440bc | 1429 | struct bt_field_structure *struct_field = (void *) field; |
918be005 | 1430 | |
44c440bc PP |
1431 | BT_LIB_LOGD("Setting structure field's frozen state: " |
1432 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1433 | |
44c440bc PP |
1434 | for (i = 0; i < struct_field->fields->len; i++) { |
1435 | struct bt_field *member_field = struct_field->fields->pdata[i]; | |
918be005 | 1436 | |
44c440bc PP |
1437 | BT_LIB_LOGD("Setting structure field's member field's " |
1438 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1439 | member_field, i); | |
b38aea74 | 1440 | _bt_field_set_is_frozen(member_field, is_frozen); |
918be005 PP |
1441 | } |
1442 | ||
44c440bc | 1443 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1444 | } |
1445 | ||
b38aea74 PP |
1446 | static |
1447 | void set_option_field_is_frozen(struct bt_field *field, bool is_frozen) | |
1448 | { | |
1449 | struct bt_field_option *opt_field = (void *) field; | |
1450 | ||
1451 | BT_LIB_LOGD("Setting option field's frozen state: " | |
1452 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
1453 | _bt_field_set_is_frozen(opt_field->content_field, is_frozen); | |
1454 | set_single_field_is_frozen(field, is_frozen); | |
1455 | } | |
1456 | ||
cb6f1f7d | 1457 | static |
44c440bc | 1458 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1459 | { |
312c056a | 1460 | uint64_t i; |
44c440bc | 1461 | struct bt_field_variant *var_field = (void *) field; |
918be005 | 1462 | |
44c440bc PP |
1463 | BT_LIB_LOGD("Setting variant field's frozen state: " |
1464 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
312c056a | 1465 | |
44c440bc PP |
1466 | for (i = 0; i < var_field->fields->len; i++) { |
1467 | struct bt_field *option_field = var_field->fields->pdata[i]; | |
312c056a | 1468 | |
44c440bc PP |
1469 | BT_LIB_LOGD("Setting variant field's option field's " |
1470 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1471 | option_field, i); | |
b38aea74 | 1472 | _bt_field_set_is_frozen(option_field, is_frozen); |
312c056a PP |
1473 | } |
1474 | ||
44c440bc | 1475 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1476 | } |
1477 | ||
cb6f1f7d | 1478 | static |
44c440bc | 1479 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1480 | { |
44c440bc | 1481 | uint64_t i; |
cb6f1f7d | 1482 | struct bt_field_array *array_field = (void *) field; |
918be005 | 1483 | |
44c440bc PP |
1484 | BT_LIB_LOGD("Setting array field's frozen state: " |
1485 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1486 | |
44c440bc PP |
1487 | for (i = 0; i < array_field->fields->len; i++) { |
1488 | struct bt_field *elem_field = array_field->fields->pdata[i]; | |
918be005 | 1489 | |
44c440bc PP |
1490 | BT_LIB_LOGD("Setting array field's element field's " |
1491 | "frozen state: %![field-]+f, index=%" PRIu64, | |
fc25abce | 1492 | elem_field, i); |
b38aea74 | 1493 | _bt_field_set_is_frozen(elem_field, is_frozen); |
918be005 PP |
1494 | } |
1495 | ||
44c440bc | 1496 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1497 | } |
1498 | ||
1499 | BT_HIDDEN | |
40f4ba76 | 1500 | void _bt_field_set_is_frozen(const struct bt_field *field, |
312c056a | 1501 | bool is_frozen) |
918be005 | 1502 | { |
98b15851 | 1503 | BT_ASSERT_DBG(field); |
44c440bc | 1504 | BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d", |
312c056a | 1505 | field, is_frozen); |
98b15851 | 1506 | BT_ASSERT_DBG(field->methods->set_is_frozen); |
40f4ba76 | 1507 | field->methods->set_is_frozen((void *) field, is_frozen); |
918be005 | 1508 | } |
76f869ab | 1509 | |
cb6f1f7d | 1510 | static |
40f4ba76 | 1511 | bool single_field_is_set(const struct bt_field *field) |
76f869ab | 1512 | { |
98b15851 | 1513 | BT_ASSERT_DBG(field); |
44c440bc | 1514 | return field->is_set; |
76f869ab JG |
1515 | } |
1516 | ||
cb6f1f7d | 1517 | static |
40f4ba76 | 1518 | bool structure_field_is_set(const struct bt_field *field) |
76f869ab | 1519 | { |
44c440bc PP |
1520 | bool is_set = true; |
1521 | uint64_t i; | |
40f4ba76 | 1522 | const struct bt_field_structure *struct_field = (const void *) field; |
76f869ab | 1523 | |
98b15851 | 1524 | BT_ASSERT_DBG(field); |
3dca2276 | 1525 | |
44c440bc PP |
1526 | for (i = 0; i < struct_field->fields->len; i++) { |
1527 | is_set = bt_field_is_set(struct_field->fields->pdata[i]); | |
d4bf905a | 1528 | if (!is_set) { |
76f869ab JG |
1529 | goto end; |
1530 | } | |
1531 | } | |
3dca2276 | 1532 | |
76f869ab | 1533 | end: |
d4bf905a | 1534 | return is_set; |
76f869ab JG |
1535 | } |
1536 | ||
b38aea74 PP |
1537 | static |
1538 | bool option_field_is_set(const struct bt_field *field) | |
1539 | { | |
1540 | const struct bt_field_option *opt_field = (const void *) field; | |
1541 | bool is_set = false; | |
1542 | ||
98b15851 | 1543 | BT_ASSERT_DBG(field); |
b38aea74 PP |
1544 | |
1545 | if (opt_field->selected_field) { | |
1546 | is_set = bt_field_is_set(opt_field->selected_field); | |
1547 | } | |
1548 | ||
1549 | return is_set; | |
1550 | } | |
1551 | ||
cb6f1f7d | 1552 | static |
40f4ba76 | 1553 | bool variant_field_is_set(const struct bt_field *field) |
76f869ab | 1554 | { |
40f4ba76 | 1555 | const struct bt_field_variant *var_field = (const void *) field; |
44c440bc | 1556 | bool is_set = false; |
76f869ab | 1557 | |
98b15851 | 1558 | BT_ASSERT_DBG(field); |
3dca2276 | 1559 | |
44c440bc PP |
1560 | if (var_field->selected_field) { |
1561 | is_set = bt_field_is_set(var_field->selected_field); | |
76f869ab | 1562 | } |
3dca2276 | 1563 | |
d4bf905a | 1564 | return is_set; |
76f869ab JG |
1565 | } |
1566 | ||
cb6f1f7d | 1567 | static |
40f4ba76 | 1568 | bool array_field_is_set(const struct bt_field *field) |
76f869ab | 1569 | { |
44c440bc PP |
1570 | bool is_set = true; |
1571 | uint64_t i; | |
40f4ba76 | 1572 | const struct bt_field_array *array_field = (const void *) field; |
76f869ab | 1573 | |
98b15851 | 1574 | BT_ASSERT_DBG(field); |
3dca2276 | 1575 | |
44c440bc PP |
1576 | for (i = 0; i < array_field->length; i++) { |
1577 | is_set = bt_field_is_set(array_field->fields->pdata[i]); | |
d4bf905a | 1578 | if (!is_set) { |
76f869ab JG |
1579 | goto end; |
1580 | } | |
1581 | } | |
3dca2276 | 1582 | |
76f869ab | 1583 | end: |
d4bf905a | 1584 | return is_set; |
76f869ab | 1585 | } |