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