Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
f2b0325d | 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 | ||
b03487ab | 24 | #define BT_LOG_TAG "LIB/FIELD" |
1633ef46 | 25 | #include "lib/logging.h" |
fc25abce | 26 | |
57952005 | 27 | #include "lib/assert-pre.h" |
71c5da58 | 28 | #include <babeltrace2/trace-ir/field.h> |
57952005 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> |
969c1d8a | 35 | #include <stdbool.h> |
273b65be | 36 | |
57952005 MJ |
37 | #include "field.h" |
38 | #include "field-class.h" | |
fb25b9e3 | 39 | #include "lib/func-status.h" |
57952005 | 40 | |
18acc6f8 | 41 | static |
7b33a0e0 | 42 | void reset_single_field(struct bt_field *field); |
18acc6f8 PP |
43 | |
44 | static | |
7b33a0e0 | 45 | void reset_array_field(struct bt_field *field); |
18acc6f8 PP |
46 | |
47 | static | |
7b33a0e0 | 48 | void reset_structure_field(struct bt_field *field); |
18acc6f8 | 49 | |
247fa9e3 PP |
50 | static |
51 | void reset_option_field(struct bt_field *field); | |
52 | ||
18acc6f8 | 53 | static |
7b33a0e0 | 54 | void reset_variant_field(struct bt_field *field); |
18acc6f8 PP |
55 | |
56 | static | |
7b33a0e0 | 57 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
58 | |
59 | static | |
7b33a0e0 | 60 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
61 | |
62 | static | |
7b33a0e0 | 63 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 | 64 | |
247fa9e3 PP |
65 | static |
66 | void set_option_field_is_frozen(struct bt_field *field, bool is_frozen); | |
67 | ||
18acc6f8 | 68 | static |
7b33a0e0 | 69 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
70 | |
71 | static | |
78cf9df6 | 72 | bool single_field_is_set(const struct bt_field *field); |
18acc6f8 PP |
73 | |
74 | static | |
78cf9df6 | 75 | bool array_field_is_set(const struct bt_field *field); |
18acc6f8 PP |
76 | |
77 | static | |
78cf9df6 | 78 | bool structure_field_is_set(const struct bt_field *field); |
18acc6f8 | 79 | |
247fa9e3 PP |
80 | static |
81 | bool option_field_is_set(const struct bt_field *field); | |
82 | ||
18acc6f8 | 83 | static |
78cf9df6 | 84 | bool variant_field_is_set(const struct bt_field *field); |
18acc6f8 | 85 | |
9414b439 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 | ||
6943da6a 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 | ||
18acc6f8 | 100 | static |
7b33a0e0 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, | |
8deee039 | 105 | }; |
273b65be | 106 | |
7b33a0e0 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 | ||
7b33a0e0 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 | ||
7b33a0e0 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 | ||
7b33a0e0 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 | ||
247fa9e3 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 |
7b33a0e0 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 | |
9414b439 PP |
149 | static |
150 | struct bt_field *create_bool_field(struct bt_field_class *); | |
151 | ||
6943da6a PP |
152 | static |
153 | struct bt_field *create_bit_array_field(struct bt_field_class *); | |
154 | ||
8deee039 | 155 | static |
939190b3 | 156 | struct bt_field *create_integer_field(struct bt_field_class *); |
8deee039 PP |
157 | |
158 | static | |
939190b3 | 159 | struct bt_field *create_real_field(struct bt_field_class *); |
8deee039 PP |
160 | |
161 | static | |
939190b3 | 162 | struct bt_field *create_string_field(struct bt_field_class *); |
8deee039 PP |
163 | |
164 | static | |
939190b3 | 165 | struct bt_field *create_structure_field(struct bt_field_class *); |
5d2d8d18 | 166 | |
8deee039 | 167 | static |
939190b3 | 168 | struct bt_field *create_static_array_field(struct bt_field_class *); |
8b45963b | 169 | |
8deee039 | 170 | static |
939190b3 | 171 | struct bt_field *create_dynamic_array_field(struct bt_field_class *); |
8b45963b | 172 | |
247fa9e3 PP |
173 | static |
174 | struct bt_field *create_option_field(struct bt_field_class *); | |
175 | ||
8deee039 | 176 | static |
939190b3 | 177 | struct bt_field *create_variant_field(struct bt_field_class *); |
8b45963b | 178 | |
9414b439 PP |
179 | static |
180 | void destroy_bool_field(struct bt_field *field); | |
181 | ||
6943da6a PP |
182 | static |
183 | void destroy_bit_array_field(struct bt_field *field); | |
184 | ||
a6918753 | 185 | static |
7b33a0e0 | 186 | void destroy_integer_field(struct bt_field *field); |
a6918753 PP |
187 | |
188 | static | |
7b33a0e0 | 189 | void destroy_real_field(struct bt_field *field); |
a6918753 PP |
190 | |
191 | static | |
7b33a0e0 | 192 | void destroy_string_field(struct bt_field *field); |
a6918753 PP |
193 | |
194 | static | |
7b33a0e0 | 195 | void destroy_structure_field(struct bt_field *field); |
a6918753 PP |
196 | |
197 | static | |
7b33a0e0 | 198 | void destroy_array_field(struct bt_field *field); |
a6918753 | 199 | |
247fa9e3 PP |
200 | static |
201 | void destroy_option_field(struct bt_field *field); | |
202 | ||
a6918753 | 203 | static |
7b33a0e0 | 204 | void destroy_variant_field(struct bt_field *field); |
a6918753 | 205 | |
844d5df6 | 206 | struct bt_field_class *bt_field_borrow_class(struct bt_field *field) |
18acc6f8 | 207 | { |
fa6cfec3 | 208 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
939190b3 | 209 | return field->class; |
18acc6f8 PP |
210 | } |
211 | ||
78cf9df6 PP |
212 | const struct bt_field_class *bt_field_borrow_class_const( |
213 | const struct bt_field *field) | |
9e550e5f | 214 | { |
fa6cfec3 | 215 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
78cf9df6 | 216 | return field->class; |
9e550e5f PP |
217 | } |
218 | ||
78cf9df6 | 219 | enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field) |
18acc6f8 | 220 | { |
fa6cfec3 | 221 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
af0c18e3 | 222 | return field->class->type; |
18acc6f8 PP |
223 | } |
224 | ||
a6918753 | 225 | BT_HIDDEN |
939190b3 | 226 | struct bt_field *bt_field_create(struct bt_field_class *fc) |
273b65be | 227 | { |
839d52a5 | 228 | struct bt_field *field = NULL; |
7b33a0e0 | 229 | |
fa6cfec3 | 230 | BT_ASSERT(fc); |
5d2d8d18 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: | |
24847fc7 | 274 | bt_common_abort(); |
5d2d8d18 PP |
275 | } |
276 | ||
273b65be | 277 | if (!field) { |
a8f90e5d | 278 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: " |
939190b3 | 279 | "%![fc-]+F", fc); |
8deee039 | 280 | goto end; |
273b65be JG |
281 | } |
282 | ||
8deee039 PP |
283 | end: |
284 | return field; | |
273b65be JG |
285 | } |
286 | ||
7b33a0e0 | 287 | static inline |
939190b3 | 288 | void init_field(struct bt_field *field, struct bt_field_class *fc, |
7b33a0e0 | 289 | struct bt_field_methods *methods) |
cd95e351 | 290 | { |
7b33a0e0 | 291 | BT_ASSERT(field); |
939190b3 | 292 | BT_ASSERT(fc); |
7b33a0e0 PP |
293 | bt_object_init_unique(&field->base); |
294 | field->methods = methods; | |
4b70020d | 295 | field->class = fc; |
864aa43f | 296 | bt_object_get_ref_no_null_check(fc); |
cd95e351 JG |
297 | } |
298 | ||
9414b439 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 | ||
6943da6a 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 | ||
7b33a0e0 | 339 | static |
939190b3 | 340 | struct bt_field *create_integer_field(struct bt_field_class *fc) |
4ebcc695 | 341 | { |
7b33a0e0 PP |
342 | struct bt_field_integer *int_field; |
343 | ||
939190b3 | 344 | BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc); |
7b33a0e0 PP |
345 | int_field = g_new0(struct bt_field_integer, 1); |
346 | if (!int_field) { | |
a8f90e5d PP |
347 | BT_LIB_LOGE_APPEND_CAUSE( |
348 | "Failed to allocate one integer field."); | |
7b33a0e0 PP |
349 | goto end; |
350 | } | |
351 | ||
939190b3 | 352 | init_field((void *) int_field, fc, &integer_field_methods); |
7b33a0e0 PP |
353 | BT_LIB_LOGD("Created integer field object: %!+f", int_field); |
354 | ||
355 | end: | |
356 | return (void *) int_field; | |
4ebcc695 PP |
357 | } |
358 | ||
7b33a0e0 | 359 | static |
939190b3 | 360 | struct bt_field *create_real_field(struct bt_field_class *fc) |
fea75608 | 361 | { |
7b33a0e0 | 362 | struct bt_field_real *real_field; |
18acc6f8 | 363 | |
939190b3 | 364 | BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc); |
7b33a0e0 PP |
365 | real_field = g_new0(struct bt_field_real, 1); |
366 | if (!real_field) { | |
a8f90e5d | 367 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field."); |
7b33a0e0 PP |
368 | goto end; |
369 | } | |
370 | ||
939190b3 | 371 | init_field((void *) real_field, fc, &real_field_methods); |
7b33a0e0 PP |
372 | BT_LIB_LOGD("Created real field object: %!+f", real_field); |
373 | ||
374 | end: | |
375 | return (void *) real_field; | |
fea75608 PP |
376 | } |
377 | ||
7b33a0e0 | 378 | static |
939190b3 | 379 | struct bt_field *create_string_field(struct bt_field_class *fc) |
273b65be | 380 | { |
7b33a0e0 | 381 | struct bt_field_string *string_field; |
18acc6f8 | 382 | |
939190b3 | 383 | BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc); |
7b33a0e0 PP |
384 | string_field = g_new0(struct bt_field_string, 1); |
385 | if (!string_field) { | |
a8f90e5d PP |
386 | BT_LIB_LOGE_APPEND_CAUSE( |
387 | "Failed to allocate one string field."); | |
7b33a0e0 PP |
388 | goto end; |
389 | } | |
18acc6f8 | 390 | |
939190b3 | 391 | init_field((void *) string_field, fc, &string_field_methods); |
7b33a0e0 PP |
392 | string_field->buf = g_array_sized_new(FALSE, FALSE, |
393 | sizeof(char), 1); | |
394 | if (!string_field->buf) { | |
a8f90e5d | 395 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
6a9bd19d FD |
396 | bt_field_destroy((void *) string_field); |
397 | string_field = NULL; | |
7b33a0e0 PP |
398 | goto end; |
399 | } | |
18acc6f8 | 400 | |
7b33a0e0 PP |
401 | g_array_index(string_field->buf, char, 0) = '\0'; |
402 | BT_LIB_LOGD("Created string field object: %!+f", string_field); | |
18acc6f8 | 403 | |
7b33a0e0 PP |
404 | end: |
405 | return (void *) string_field; | |
406 | } | |
18acc6f8 | 407 | |
7b33a0e0 | 408 | static inline |
939190b3 PP |
409 | int create_fields_from_named_field_classes( |
410 | struct bt_field_class_named_field_class_container *fc, | |
7b33a0e0 PP |
411 | GPtrArray **fields) |
412 | { | |
413 | int ret = 0; | |
414 | uint64_t i; | |
18acc6f8 | 415 | |
7b33a0e0 PP |
416 | *fields = g_ptr_array_new_with_free_func( |
417 | (GDestroyNotify) bt_field_destroy); | |
418 | if (!*fields) { | |
a8f90e5d | 419 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
7b33a0e0 PP |
420 | ret = -1; |
421 | goto end; | |
18acc6f8 PP |
422 | } |
423 | ||
939190b3 | 424 | g_ptr_array_set_size(*fields, fc->named_fcs->len); |
7b33a0e0 | 425 | |
939190b3 | 426 | for (i = 0; i < fc->named_fcs->len; i++) { |
7b33a0e0 | 427 | struct bt_field *field; |
02b61fe0 | 428 | struct bt_named_field_class *named_fc = fc->named_fcs->pdata[i]; |
7b33a0e0 | 429 | |
939190b3 | 430 | field = bt_field_create(named_fc->fc); |
7b33a0e0 | 431 | if (!field) { |
a8f90e5d PP |
432 | BT_LIB_LOGE_APPEND_CAUSE( |
433 | "Failed to create structure member or variant option field: " | |
939190b3 PP |
434 | "name=\"%s\", %![fc-]+F", |
435 | named_fc->name->str, named_fc->fc); | |
7b33a0e0 PP |
436 | ret = -1; |
437 | goto end; | |
438 | } | |
439 | ||
440 | g_ptr_array_index(*fields, i) = field; | |
441 | } | |
18acc6f8 PP |
442 | |
443 | end: | |
444 | return ret; | |
273b65be JG |
445 | } |
446 | ||
7b33a0e0 | 447 | static |
939190b3 | 448 | struct bt_field *create_structure_field(struct bt_field_class *fc) |
cd95e351 | 449 | { |
7b33a0e0 | 450 | struct bt_field_structure *struct_field; |
18acc6f8 | 451 | |
939190b3 | 452 | BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc); |
7b33a0e0 PP |
453 | struct_field = g_new0(struct bt_field_structure, 1); |
454 | if (!struct_field) { | |
a8f90e5d PP |
455 | BT_LIB_LOGE_APPEND_CAUSE( |
456 | "Failed to allocate one structure field."); | |
7b33a0e0 PP |
457 | goto end; |
458 | } | |
fc25abce | 459 | |
939190b3 | 460 | init_field((void *) struct_field, fc, &structure_field_methods); |
7b33a0e0 | 461 | |
939190b3 | 462 | if (create_fields_from_named_field_classes((void *) fc, |
7b33a0e0 | 463 | &struct_field->fields)) { |
a8f90e5d PP |
464 | BT_LIB_LOGE_APPEND_CAUSE( |
465 | "Cannot create structure member fields: %![fc-]+F", fc); | |
6a9bd19d FD |
466 | bt_field_destroy((void *) struct_field); |
467 | struct_field = NULL; | |
7b33a0e0 | 468 | goto end; |
18acc6f8 PP |
469 | } |
470 | ||
7b33a0e0 | 471 | BT_LIB_LOGD("Created structure field object: %!+f", struct_field); |
18acc6f8 | 472 | |
7b33a0e0 PP |
473 | end: |
474 | return (void *) struct_field; | |
cd95e351 JG |
475 | } |
476 | ||
247fa9e3 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); | |
6a9bd19d FD |
498 | bt_field_destroy((void *) opt_field); |
499 | opt_field = NULL; | |
247fa9e3 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 | ||
7b33a0e0 | 509 | static |
939190b3 | 510 | struct bt_field *create_variant_field(struct bt_field_class *fc) |
273b65be | 511 | { |
7b33a0e0 | 512 | struct bt_field_variant *var_field; |
18acc6f8 | 513 | |
939190b3 | 514 | BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc); |
7b33a0e0 PP |
515 | var_field = g_new0(struct bt_field_variant, 1); |
516 | if (!var_field) { | |
a8f90e5d PP |
517 | BT_LIB_LOGE_APPEND_CAUSE( |
518 | "Failed to allocate one variant field."); | |
7b33a0e0 PP |
519 | goto end; |
520 | } | |
8b45963b | 521 | |
939190b3 | 522 | init_field((void *) var_field, fc, &variant_field_methods); |
18acc6f8 | 523 | |
939190b3 | 524 | if (create_fields_from_named_field_classes((void *) fc, |
7b33a0e0 | 525 | &var_field->fields)) { |
a8f90e5d | 526 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: " |
939190b3 | 527 | "%![fc-]+F", fc); |
6a9bd19d FD |
528 | bt_field_destroy((void *) var_field); |
529 | var_field = NULL; | |
7b33a0e0 PP |
530 | goto end; |
531 | } | |
273b65be | 532 | |
7b33a0e0 | 533 | BT_LIB_LOGD("Created variant field object: %!+f", var_field); |
18acc6f8 | 534 | |
7b33a0e0 PP |
535 | end: |
536 | return (void *) var_field; | |
18acc6f8 PP |
537 | } |
538 | ||
539 | static inline | |
7b33a0e0 | 540 | int init_array_field_fields(struct bt_field_array *array_field) |
18acc6f8 PP |
541 | { |
542 | int ret = 0; | |
7b33a0e0 | 543 | uint64_t i; |
939190b3 | 544 | struct bt_field_class_array *array_fc; |
18acc6f8 | 545 | |
7b33a0e0 | 546 | BT_ASSERT(array_field); |
939190b3 | 547 | array_fc = (void *) array_field->common.class; |
7b33a0e0 PP |
548 | array_field->fields = g_ptr_array_sized_new(array_field->length); |
549 | if (!array_field->fields) { | |
a8f90e5d | 550 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
18acc6f8 PP |
551 | ret = -1; |
552 | goto end; | |
553 | } | |
554 | ||
7b33a0e0 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( | |
939190b3 | 561 | array_fc->element_fc); |
7b33a0e0 | 562 | if (!array_field->fields->pdata[i]) { |
a8f90e5d PP |
563 | BT_LIB_LOGE_APPEND_CAUSE( |
564 | "Cannot create array field's element field: " | |
939190b3 | 565 | "index=%" PRIu64 ", %![fc-]+F", i, array_fc); |
7b33a0e0 PP |
566 | ret = -1; |
567 | goto end; | |
568 | } | |
569 | } | |
18acc6f8 PP |
570 | |
571 | end: | |
572 | return ret; | |
3f4a108d PP |
573 | } |
574 | ||
7b33a0e0 | 575 | static |
939190b3 | 576 | struct bt_field *create_static_array_field(struct bt_field_class *fc) |
f78d67fb | 577 | { |
60bbfc7c | 578 | struct bt_field_class_array_static *array_fc = (void *) fc; |
7b33a0e0 | 579 | struct bt_field_array *array_field; |
a6918753 | 580 | |
939190b3 | 581 | BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc); |
7b33a0e0 PP |
582 | array_field = g_new0(struct bt_field_array, 1); |
583 | if (!array_field) { | |
a8f90e5d PP |
584 | BT_LIB_LOGE_APPEND_CAUSE( |
585 | "Failed to allocate one static array field."); | |
7b33a0e0 PP |
586 | goto end; |
587 | } | |
f78d67fb | 588 | |
939190b3 PP |
589 | init_field((void *) array_field, fc, &array_field_methods); |
590 | array_field->length = array_fc->length; | |
18acc6f8 | 591 | |
7b33a0e0 | 592 | if (init_array_field_fields(array_field)) { |
a8f90e5d | 593 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: " |
939190b3 | 594 | "%![fc-]+F", fc); |
6a9bd19d FD |
595 | bt_field_destroy((void *) array_field); |
596 | array_field = NULL; | |
7b33a0e0 PP |
597 | goto end; |
598 | } | |
a6918753 | 599 | |
7b33a0e0 | 600 | BT_LIB_LOGD("Created static array field object: %!+f", array_field); |
18acc6f8 | 601 | |
7b33a0e0 PP |
602 | end: |
603 | return (void *) array_field; | |
273b65be JG |
604 | } |
605 | ||
7b33a0e0 | 606 | static |
939190b3 | 607 | struct bt_field *create_dynamic_array_field(struct bt_field_class *fc) |
cd95e351 | 608 | { |
7b33a0e0 | 609 | struct bt_field_array *array_field; |
a6918753 | 610 | |
939190b3 | 611 | BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc); |
7b33a0e0 PP |
612 | array_field = g_new0(struct bt_field_array, 1); |
613 | if (!array_field) { | |
a8f90e5d PP |
614 | BT_LIB_LOGE_APPEND_CAUSE( |
615 | "Failed to allocate one dynamic array field."); | |
7b33a0e0 PP |
616 | goto end; |
617 | } | |
618 | ||
939190b3 | 619 | init_field((void *) array_field, fc, &array_field_methods); |
7b33a0e0 PP |
620 | |
621 | if (init_array_field_fields(array_field)) { | |
a8f90e5d | 622 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: " |
939190b3 | 623 | "%![fc-]+F", fc); |
6a9bd19d FD |
624 | bt_field_destroy((void *) array_field); |
625 | array_field = NULL; | |
7b33a0e0 | 626 | goto end; |
18acc6f8 PP |
627 | } |
628 | ||
7b33a0e0 PP |
629 | BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field); |
630 | ||
631 | end: | |
632 | return (void *) array_field; | |
a6918753 PP |
633 | } |
634 | ||
9414b439 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 | ||
6943da6a 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 | ||
60bbfc7c | 690 | int64_t bt_field_integer_signed_get_value(const struct bt_field *field) |
a6918753 | 691 | { |
78cf9df6 | 692 | const struct bt_field_integer *int_field = (const void *) field; |
a6918753 | 693 | |
fa6cfec3 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"); | |
7b33a0e0 | 697 | return int_field->value.i; |
cd95e351 JG |
698 | } |
699 | ||
60bbfc7c | 700 | void bt_field_integer_signed_set_value(struct bt_field *field, int64_t value) |
cd95e351 | 701 | { |
7b33a0e0 | 702 | struct bt_field_integer *int_field = (void *) field; |
a6918753 | 703 | |
fa6cfec3 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( | |
939190b3 | 708 | ((struct bt_field_class_integer *) field->class)->range, value), |
7b33a0e0 | 709 | "Value is out of bounds: value=%" PRId64 ", %![field-]+f, " |
939190b3 | 710 | "%![fc-]+F", value, field, field->class); |
7b33a0e0 PP |
711 | int_field->value.i = value; |
712 | bt_field_set_single(field, true); | |
cd95e351 JG |
713 | } |
714 | ||
60bbfc7c | 715 | uint64_t bt_field_integer_unsigned_get_value(const struct bt_field *field) |
273b65be | 716 | { |
78cf9df6 | 717 | const struct bt_field_integer *int_field = (const void *) field; |
7b33a0e0 | 718 | |
fa6cfec3 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"); | |
7b33a0e0 | 722 | return int_field->value.u; |
273b65be JG |
723 | } |
724 | ||
60bbfc7c | 725 | void bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value) |
cd95e351 | 726 | { |
7b33a0e0 | 727 | struct bt_field_integer *int_field = (void *) field; |
a6918753 | 728 | |
fa6cfec3 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( | |
939190b3 | 733 | ((struct bt_field_class_integer *) field->class)->range, value), |
7b33a0e0 | 734 | "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, " |
939190b3 | 735 | "%![fc-]+F", value, field, field->class); |
7b33a0e0 PP |
736 | int_field->value.u = value; |
737 | bt_field_set_single(field, true); | |
cd95e351 JG |
738 | } |
739 | ||
76276a81 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 | { |
78cf9df6 | 753 | const struct bt_field_real *real_field = (const void *) field; |
7b33a0e0 | 754 | |
fa6cfec3 PP |
755 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
756 | BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); | |
76276a81 FD |
757 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
758 | BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field"); | |
759 | ||
7b33a0e0 | 760 | return real_field->value; |
8b45963b PP |
761 | } |
762 | ||
76276a81 FD |
763 | void bt_field_real_single_precision_set_value(struct bt_field *field, |
764 | float value) | |
8b45963b | 765 | { |
7b33a0e0 | 766 | struct bt_field_real *real_field = (void *) field; |
18acc6f8 | 767 | |
fa6cfec3 | 768 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
76276a81 FD |
769 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, |
770 | BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field"); | |
fa6cfec3 | 771 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
76276a81 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 | ||
7b33a0e0 PP |
787 | real_field->value = value; |
788 | bt_field_set_single(field, true); | |
789 | } | |
790 | ||
fb25b9e3 | 791 | enum bt_field_enumeration_get_mapping_labels_status |
60bbfc7c | 792 | bt_field_enumeration_unsigned_get_mapping_labels( |
78cf9df6 | 793 | const struct bt_field *field, |
939190b3 | 794 | bt_field_class_enumeration_mapping_label_array *label_array, |
7b33a0e0 PP |
795 | uint64_t *count) |
796 | { | |
78cf9df6 | 797 | const struct bt_field_integer *int_field = (const void *) field; |
7b33a0e0 | 798 | |
7c7324d3 | 799 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 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, | |
af0c18e3 | 805 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field"); |
937c4e48 | 806 | return (int) |
60bbfc7c | 807 | bt_field_class_enumeration_unsigned_get_mapping_labels_for_value( |
937c4e48 | 808 | field->class, int_field->value.u, label_array, count); |
273b65be JG |
809 | } |
810 | ||
fb25b9e3 | 811 | enum bt_field_enumeration_get_mapping_labels_status |
60bbfc7c | 812 | bt_field_enumeration_signed_get_mapping_labels( |
78cf9df6 | 813 | const struct bt_field *field, |
939190b3 | 814 | bt_field_class_enumeration_mapping_label_array *label_array, |
7b33a0e0 | 815 | uint64_t *count) |
cd95e351 | 816 | { |
78cf9df6 | 817 | const struct bt_field_integer *int_field = (const void *) field; |
18acc6f8 | 818 | |
7c7324d3 | 819 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 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, | |
af0c18e3 | 825 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field"); |
937c4e48 | 826 | return (int) |
60bbfc7c | 827 | bt_field_class_enumeration_signed_get_mapping_labels_for_value( |
937c4e48 | 828 | field->class, int_field->value.i, label_array, count); |
8b45963b | 829 | } |
fc25abce | 830 | |
78cf9df6 | 831 | const char *bt_field_string_get_value(const struct bt_field *field) |
8b45963b | 832 | { |
78cf9df6 | 833 | const struct bt_field_string *string_field = (const void *) field; |
7b33a0e0 | 834 | |
fa6cfec3 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, | |
7b33a0e0 PP |
838 | "Field"); |
839 | return (const char *) string_field->buf->data; | |
840 | } | |
841 | ||
78cf9df6 | 842 | uint64_t bt_field_string_get_length(const struct bt_field *field) |
7b33a0e0 | 843 | { |
78cf9df6 | 844 | const struct bt_field_string *string_field = (const void *) field; |
18acc6f8 | 845 | |
fa6cfec3 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, | |
7b33a0e0 PP |
849 | "Field"); |
850 | return string_field->length; | |
cd95e351 JG |
851 | } |
852 | ||
937c4e48 PP |
853 | static inline |
854 | void clear_string_field(struct bt_field *field) | |
855 | { | |
856 | struct bt_field_string *string_field = (void *) field; | |
857 | ||
ec4a3354 | 858 | BT_ASSERT_DBG(field); |
937c4e48 | 859 | string_field->length = 0; |
d321ba03 | 860 | g_array_index(string_field->buf, char, 0) = '\0'; |
937c4e48 PP |
861 | bt_field_set_single(field, true); |
862 | } | |
863 | ||
fb25b9e3 PP |
864 | enum bt_field_string_set_value_status bt_field_string_set_value( |
865 | struct bt_field *field, const char *value) | |
273b65be | 866 | { |
7c7324d3 | 867 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 PP |
868 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
869 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); | |
870 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
871 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, | |
7b33a0e0 | 872 | "Field"); |
937c4e48 | 873 | clear_string_field(field); |
fb25b9e3 | 874 | return (int) bt_field_string_append_with_length(field, value, |
7b33a0e0 | 875 | (uint64_t) strlen(value)); |
273b65be JG |
876 | } |
877 | ||
fb25b9e3 PP |
878 | enum bt_field_string_append_status bt_field_string_append( |
879 | struct bt_field *field, const char *value) | |
cd95e351 | 880 | { |
7c7324d3 SM |
881 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
882 | ||
78cf9df6 | 883 | return bt_field_string_append_with_length(field, |
9e550e5f | 884 | value, (uint64_t) strlen(value)); |
cd95e351 JG |
885 | } |
886 | ||
fb25b9e3 PP |
887 | enum bt_field_string_append_status bt_field_string_append_with_length( |
888 | struct bt_field *field, const char *value, uint64_t length) | |
273b65be | 889 | { |
18acc6f8 PP |
890 | struct bt_field_string *string_field = (void *) field; |
891 | char *data; | |
7b33a0e0 | 892 | uint64_t new_length; |
273b65be | 893 | |
7c7324d3 | 894 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 PP |
895 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
896 | BT_ASSERT_PRE_DEV_NON_NULL(value, "Value"); | |
897 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
898 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
af0c18e3 | 899 | BT_FIELD_CLASS_TYPE_STRING, "Field"); |
a6918753 | 900 | |
18acc6f8 | 901 | /* Make sure no null bytes are appended */ |
8e01f2d9 | 902 | BT_ASSERT_PRE_DEV(!memchr(value, '\0', length), |
18acc6f8 | 903 | "String value to append contains a null character: " |
7b33a0e0 | 904 | "partial-value=\"%.32s\", length=%" PRIu64, value, length); |
c6f9c5a3 | 905 | |
7b33a0e0 | 906 | new_length = length + string_field->length; |
18acc6f8 | 907 | |
85e7137b | 908 | if (G_UNLIKELY(new_length + 1 > string_field->buf->len)) { |
7b33a0e0 | 909 | g_array_set_size(string_field->buf, new_length + 1); |
c6f9c5a3 PP |
910 | } |
911 | ||
18acc6f8 | 912 | data = string_field->buf->data; |
7b33a0e0 PP |
913 | memcpy(data + string_field->length, value, length); |
914 | ((char *) string_field->buf->data)[new_length] = '\0'; | |
915 | string_field->length = new_length; | |
916 | bt_field_set_single(field, true); | |
fb25b9e3 | 917 | return BT_FUNC_STATUS_OK; |
18acc6f8 | 918 | } |
8deee039 | 919 | |
fb25b9e3 | 920 | void bt_field_string_clear(struct bt_field *field) |
18acc6f8 | 921 | { |
fa6cfec3 PP |
922 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
923 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); | |
924 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
af0c18e3 | 925 | BT_FIELD_CLASS_TYPE_STRING, "Field"); |
937c4e48 | 926 | clear_string_field(field); |
18acc6f8 PP |
927 | } |
928 | ||
78cf9df6 | 929 | uint64_t bt_field_array_get_length(const struct bt_field *field) |
18acc6f8 | 930 | { |
78cf9df6 | 931 | const struct bt_field_array *array_field = (const void *) field; |
c6f9c5a3 | 932 | |
fa6cfec3 PP |
933 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
934 | BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field"); | |
7b33a0e0 | 935 | return array_field->length; |
8deee039 | 936 | } |
f98c6554 | 937 | |
60bbfc7c | 938 | enum bt_field_array_dynamic_set_length_status bt_field_array_dynamic_set_length( |
fb25b9e3 | 939 | struct bt_field *field, uint64_t length) |
8deee039 | 940 | { |
fb25b9e3 | 941 | int ret = BT_FUNC_STATUS_OK; |
7b33a0e0 | 942 | struct bt_field_array *array_field = (void *) field; |
f98c6554 | 943 | |
7c7324d3 | 944 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 | 945 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
b8ddb4f0 | 946 | BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(field, "Field"); |
fa6cfec3 | 947 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
273b65be | 948 | |
85e7137b | 949 | if (G_UNLIKELY(length > array_field->fields->len)) { |
7b33a0e0 | 950 | /* Make more room */ |
939190b3 | 951 | struct bt_field_class_array *array_fc; |
7b33a0e0 PP |
952 | uint64_t cur_len = array_field->fields->len; |
953 | uint64_t i; | |
18acc6f8 | 954 | |
7b33a0e0 | 955 | g_ptr_array_set_size(array_field->fields, length); |
939190b3 | 956 | array_fc = (void *) field->class; |
18acc6f8 | 957 | |
7b33a0e0 PP |
958 | for (i = cur_len; i < array_field->fields->len; i++) { |
959 | struct bt_field *elem_field = bt_field_create( | |
939190b3 | 960 | array_fc->element_fc); |
273b65be | 961 | |
7b33a0e0 | 962 | if (!elem_field) { |
a8f90e5d PP |
963 | BT_LIB_LOGE_APPEND_CAUSE( |
964 | "Cannot create element field for " | |
7b33a0e0 PP |
965 | "dynamic array field: " |
966 | "index=%" PRIu64 ", " | |
967 | "%![array-field-]+f", i, field); | |
fb25b9e3 | 968 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
7b33a0e0 PP |
969 | goto end; |
970 | } | |
9be89173 | 971 | |
ec4a3354 | 972 | BT_ASSERT_DBG(!array_field->fields->pdata[i]); |
7b33a0e0 | 973 | array_field->fields->pdata[i] = elem_field; |
9be89173 | 974 | } |
9be89173 JG |
975 | } |
976 | ||
7b33a0e0 | 977 | array_field->length = length; |
8deee039 | 978 | |
273b65be | 979 | end: |
9be89173 | 980 | return ret; |
273b65be JG |
981 | } |
982 | ||
78cf9df6 PP |
983 | static inline |
984 | struct bt_field *borrow_array_field_element_field_by_index( | |
7b33a0e0 | 985 | struct bt_field *field, uint64_t index) |
a6918753 | 986 | { |
7b33a0e0 | 987 | struct bt_field_array *array_field = (void *) field; |
a6918753 | 988 | |
fa6cfec3 PP |
989 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
990 | BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field"); | |
991 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, array_field->length); | |
7b33a0e0 | 992 | return array_field->fields->pdata[index]; |
a6918753 PP |
993 | } |
994 | ||
78cf9df6 PP |
995 | struct bt_field *bt_field_array_borrow_element_field_by_index( |
996 | struct bt_field *field, uint64_t index) | |
9e550e5f | 997 | { |
78cf9df6 | 998 | return borrow_array_field_element_field_by_index(field, index); |
9e550e5f PP |
999 | } |
1000 | ||
78cf9df6 PP |
1001 | const struct bt_field * |
1002 | bt_field_array_borrow_element_field_by_index_const( | |
1003 | const struct bt_field *field, uint64_t index) | |
1004 | { | |
1005 | return borrow_array_field_element_field_by_index((void *) field, index); | |
1006 | } | |
1007 | ||
1008 | static inline | |
1009 | struct bt_field *borrow_structure_field_member_field_by_index( | |
7b33a0e0 | 1010 | struct bt_field *field, uint64_t index) |
e42dd763 | 1011 | { |
7b33a0e0 | 1012 | struct bt_field_structure *struct_field = (void *) field; |
e42dd763 | 1013 | |
fa6cfec3 PP |
1014 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
1015 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
af0c18e3 | 1016 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); |
fa6cfec3 | 1017 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, struct_field->fields->len); |
7b33a0e0 | 1018 | return struct_field->fields->pdata[index]; |
e42dd763 PP |
1019 | } |
1020 | ||
78cf9df6 PP |
1021 | struct bt_field *bt_field_structure_borrow_member_field_by_index( |
1022 | struct bt_field *field, uint64_t index) | |
1023 | { | |
1024 | return borrow_structure_field_member_field_by_index(field, | |
1025 | index); | |
1026 | } | |
1027 | ||
1028 | const struct bt_field * | |
1029 | bt_field_structure_borrow_member_field_by_index_const( | |
1030 | const struct bt_field *field, uint64_t index) | |
9e550e5f | 1031 | { |
78cf9df6 | 1032 | return borrow_structure_field_member_field_by_index( |
9e550e5f PP |
1033 | (void *) field, index); |
1034 | } | |
1035 | ||
78cf9df6 PP |
1036 | static inline |
1037 | struct bt_field *borrow_structure_field_member_field_by_name( | |
7b33a0e0 | 1038 | struct bt_field *field, const char *name) |
273b65be | 1039 | { |
7b33a0e0 | 1040 | struct bt_field *ret_field = NULL; |
939190b3 | 1041 | struct bt_field_class_structure *struct_fc; |
7b33a0e0 PP |
1042 | struct bt_field_structure *struct_field = (void *) field; |
1043 | gpointer orig_key; | |
1044 | gpointer index; | |
fc25abce | 1045 | |
fa6cfec3 PP |
1046 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
1047 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Field name"); | |
1048 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
af0c18e3 | 1049 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); |
939190b3 | 1050 | struct_fc = (void *) field->class; |
a6918753 | 1051 | |
939190b3 | 1052 | if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name, |
7b33a0e0 | 1053 | &orig_key, &index)) { |
a6918753 | 1054 | goto end; |
fc25abce PP |
1055 | } |
1056 | ||
7b33a0e0 | 1057 | ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)]; |
ec4a3354 | 1058 | BT_ASSERT_DBG(ret_field); |
a6918753 PP |
1059 | |
1060 | end: | |
7b33a0e0 | 1061 | return ret_field; |
273b65be JG |
1062 | } |
1063 | ||
78cf9df6 PP |
1064 | struct bt_field *bt_field_structure_borrow_member_field_by_name( |
1065 | struct bt_field *field, const char *name) | |
1066 | { | |
1067 | return borrow_structure_field_member_field_by_name(field, name); | |
1068 | } | |
1069 | ||
1070 | const struct bt_field *bt_field_structure_borrow_member_field_by_name_const( | |
1071 | const struct bt_field *field, const char *name) | |
9e550e5f | 1072 | { |
78cf9df6 | 1073 | return borrow_structure_field_member_field_by_name( |
9e550e5f PP |
1074 | (void *) field, name); |
1075 | } | |
1076 | ||
247fa9e3 PP |
1077 | void bt_field_option_set_has_field(struct bt_field *field, bt_bool has_field) |
1078 | { | |
1079 | struct bt_field_option *opt_field = (void *) field; | |
1080 | ||
1081 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
467673c1 | 1082 | BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(field, "Field"); |
247fa9e3 PP |
1083 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
1084 | ||
1085 | if (has_field) { | |
1086 | opt_field->selected_field = opt_field->content_field; | |
1087 | } else { | |
1088 | opt_field->selected_field = NULL; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | struct bt_field *bt_field_option_borrow_field(struct bt_field *field) | |
1093 | { | |
1094 | struct bt_field_option *opt_field = (void *) field; | |
1095 | ||
1096 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
467673c1 | 1097 | BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(field, "Field"); |
247fa9e3 PP |
1098 | return opt_field->selected_field; |
1099 | } | |
1100 | ||
1101 | const struct bt_field *bt_field_option_borrow_field_const( | |
1102 | const struct bt_field *field) | |
1103 | { | |
1104 | return (const void *) bt_field_option_borrow_field((void *) field); | |
1105 | } | |
1106 | ||
78cf9df6 PP |
1107 | static inline |
1108 | struct bt_field *borrow_variant_field_selected_option_field( | |
7b33a0e0 | 1109 | struct bt_field *field) |
273b65be | 1110 | { |
7b33a0e0 | 1111 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 1112 | |
fa6cfec3 | 1113 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
02b61fe0 | 1114 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
fa6cfec3 | 1115 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
7b33a0e0 PP |
1116 | "Variant field has no selected field: %!+f", field); |
1117 | return var_field->selected_field; | |
273b65be JG |
1118 | } |
1119 | ||
78cf9df6 PP |
1120 | struct bt_field *bt_field_variant_borrow_selected_option_field( |
1121 | struct bt_field *field) | |
1122 | { | |
1123 | return borrow_variant_field_selected_option_field(field); | |
1124 | } | |
1125 | ||
1126 | const struct bt_field *bt_field_variant_borrow_selected_option_field_const( | |
1127 | const struct bt_field *field) | |
273b65be | 1128 | { |
78cf9df6 | 1129 | return borrow_variant_field_selected_option_field((void *) field); |
9e550e5f PP |
1130 | } |
1131 | ||
02b61fe0 PP |
1132 | static |
1133 | const struct bt_field_class_variant_option * | |
1134 | borrow_variant_field_selected_class_option(const struct bt_field *field) | |
1135 | { | |
1136 | const struct bt_field_class_named_field_class_container *container_fc; | |
1137 | const struct bt_field_variant *var_field = (const void *) field; | |
1138 | ||
ec4a3354 | 1139 | BT_ASSERT_DBG(field); |
02b61fe0 PP |
1140 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
1141 | "Variant field has no selected field: %!+f", field); | |
1142 | container_fc = (const void *) field->class; | |
1143 | return container_fc->named_fcs->pdata[var_field->selected_index]; | |
1144 | } | |
1145 | ||
1146 | const struct bt_field_class_variant_option * | |
7e0b3e01 | 1147 | bt_field_variant_borrow_selected_option_class_const( |
02b61fe0 PP |
1148 | const struct bt_field *field) |
1149 | { | |
1150 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
1151 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); | |
1152 | return borrow_variant_field_selected_class_option(field); | |
1153 | } | |
1154 | ||
8bc207af | 1155 | const struct bt_field_class_variant_with_selector_field_integer_unsigned_option * |
7e0b3e01 | 1156 | bt_field_variant_with_selector_field_integer_unsigned_borrow_selected_option_class_const( |
02b61fe0 PP |
1157 | const struct bt_field *field) |
1158 | { | |
1159 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
1160 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
8bc207af | 1161 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD, "Field"); |
02b61fe0 PP |
1162 | return (const void *) borrow_variant_field_selected_class_option(field); |
1163 | } | |
1164 | ||
8bc207af | 1165 | const struct bt_field_class_variant_with_selector_field_integer_signed_option * |
7e0b3e01 | 1166 | bt_field_variant_with_selector_field_integer_signed_borrow_selected_option_class_const( |
02b61fe0 PP |
1167 | const struct bt_field *field) |
1168 | { | |
1169 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); | |
1170 | BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, | |
8bc207af | 1171 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, "Field"); |
02b61fe0 PP |
1172 | return (const void *) borrow_variant_field_selected_class_option(field); |
1173 | } | |
1174 | ||
7e0b3e01 PP |
1175 | enum bt_field_variant_select_option_by_index_status |
1176 | bt_field_variant_select_option_by_index( | |
78cf9df6 | 1177 | struct bt_field *field, uint64_t index) |
9e550e5f | 1178 | { |
7b33a0e0 | 1179 | struct bt_field_variant *var_field = (void *) field; |
fc25abce | 1180 | |
7c7324d3 | 1181 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 | 1182 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
02b61fe0 | 1183 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
fa6cfec3 PP |
1184 | BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); |
1185 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, var_field->fields->len); | |
7b33a0e0 PP |
1186 | var_field->selected_field = var_field->fields->pdata[index]; |
1187 | var_field->selected_index = index; | |
fb25b9e3 | 1188 | return BT_FUNC_STATUS_OK; |
273b65be JG |
1189 | } |
1190 | ||
7e0b3e01 | 1191 | uint64_t bt_field_variant_get_selected_option_index( |
78cf9df6 | 1192 | const struct bt_field *field) |
a6918753 | 1193 | { |
78cf9df6 | 1194 | const struct bt_field_variant *var_field = (const void *) field; |
a6918753 | 1195 | |
fa6cfec3 | 1196 | BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); |
02b61fe0 | 1197 | BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field, "Field"); |
fa6cfec3 | 1198 | BT_ASSERT_PRE_DEV(var_field->selected_field, |
7b33a0e0 PP |
1199 | "Variant field has no selected field: %!+f", field); |
1200 | return var_field->selected_index; | |
a6918753 PP |
1201 | } |
1202 | ||
7b33a0e0 PP |
1203 | static inline |
1204 | void bt_field_finalize(struct bt_field *field) | |
273b65be | 1205 | { |
7b33a0e0 | 1206 | BT_ASSERT(field); |
939190b3 | 1207 | BT_LOGD_STR("Putting field's class."); |
1248f5ea | 1208 | BT_OBJECT_PUT_REF_AND_RESET(field->class); |
273b65be JG |
1209 | } |
1210 | ||
9414b439 PP |
1211 | static |
1212 | void destroy_bool_field(struct bt_field *field) | |
1213 | { | |
1214 | BT_ASSERT(field); | |
1215 | BT_LIB_LOGD("Destroying boolean field object: %!+f", field); | |
1216 | bt_field_finalize(field); | |
1217 | g_free(field); | |
1218 | } | |
1219 | ||
6943da6a PP |
1220 | static |
1221 | void destroy_bit_array_field(struct bt_field *field) | |
1222 | { | |
1223 | BT_ASSERT(field); | |
1224 | BT_LIB_LOGD("Destroying bit array field object: %!+f", field); | |
1225 | bt_field_finalize(field); | |
1226 | g_free(field); | |
1227 | } | |
1228 | ||
273b65be | 1229 | static |
7b33a0e0 | 1230 | void destroy_integer_field(struct bt_field *field) |
273b65be | 1231 | { |
7b33a0e0 PP |
1232 | BT_ASSERT(field); |
1233 | BT_LIB_LOGD("Destroying integer field object: %!+f", field); | |
1234 | bt_field_finalize(field); | |
1235 | g_free(field); | |
273b65be JG |
1236 | } |
1237 | ||
18acc6f8 | 1238 | static |
7b33a0e0 | 1239 | void destroy_real_field(struct bt_field *field) |
273b65be | 1240 | { |
7b33a0e0 PP |
1241 | BT_ASSERT(field); |
1242 | BT_LIB_LOGD("Destroying real field object: %!+f", field); | |
1243 | bt_field_finalize(field); | |
1244 | g_free(field); | |
273b65be JG |
1245 | } |
1246 | ||
18acc6f8 | 1247 | static |
7b33a0e0 | 1248 | void destroy_structure_field(struct bt_field *field) |
273b65be | 1249 | { |
7b33a0e0 | 1250 | struct bt_field_structure *struct_field = (void *) field; |
273b65be | 1251 | |
8b45963b | 1252 | BT_ASSERT(field); |
7b33a0e0 PP |
1253 | BT_LIB_LOGD("Destroying structure field object: %!+f", field); |
1254 | bt_field_finalize(field); | |
8b45963b | 1255 | |
7b33a0e0 PP |
1256 | if (struct_field->fields) { |
1257 | g_ptr_array_free(struct_field->fields, TRUE); | |
1248f5ea | 1258 | struct_field->fields = NULL; |
273b65be | 1259 | } |
8b45963b | 1260 | |
7b33a0e0 | 1261 | g_free(field); |
273b65be JG |
1262 | } |
1263 | ||
247fa9e3 PP |
1264 | static |
1265 | void destroy_option_field(struct bt_field *field) | |
1266 | { | |
1267 | struct bt_field_option *opt_field = (void *) field; | |
1268 | ||
1269 | BT_ASSERT(field); | |
1270 | BT_LIB_LOGD("Destroying option field object: %!+f", field); | |
1271 | bt_field_finalize(field); | |
1272 | ||
1273 | if (opt_field->content_field) { | |
1274 | bt_field_destroy(opt_field->content_field); | |
1275 | } | |
1276 | ||
1277 | g_free(field); | |
1278 | } | |
1279 | ||
18acc6f8 | 1280 | static |
7b33a0e0 | 1281 | void destroy_variant_field(struct bt_field *field) |
273b65be | 1282 | { |
7b33a0e0 | 1283 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 1284 | |
8b45963b | 1285 | BT_ASSERT(field); |
7b33a0e0 PP |
1286 | BT_LIB_LOGD("Destroying variant field object: %!+f", field); |
1287 | bt_field_finalize(field); | |
a6918753 | 1288 | |
7b33a0e0 PP |
1289 | if (var_field->fields) { |
1290 | g_ptr_array_free(var_field->fields, TRUE); | |
1248f5ea | 1291 | var_field->fields = NULL; |
fc25abce | 1292 | } |
8b45963b | 1293 | |
7b33a0e0 | 1294 | g_free(field); |
273b65be JG |
1295 | } |
1296 | ||
18acc6f8 | 1297 | static |
7b33a0e0 | 1298 | void destroy_array_field(struct bt_field *field) |
273b65be | 1299 | { |
7b33a0e0 | 1300 | struct bt_field_array *array_field = (void *) field; |
273b65be | 1301 | |
8b45963b | 1302 | BT_ASSERT(field); |
7b33a0e0 PP |
1303 | BT_LIB_LOGD("Destroying array field object: %!+f", field); |
1304 | bt_field_finalize(field); | |
8deee039 | 1305 | |
7b33a0e0 PP |
1306 | if (array_field->fields) { |
1307 | g_ptr_array_free(array_field->fields, TRUE); | |
1248f5ea | 1308 | array_field->fields = NULL; |
273b65be | 1309 | } |
8b45963b | 1310 | |
7b33a0e0 | 1311 | g_free(field); |
273b65be JG |
1312 | } |
1313 | ||
18acc6f8 | 1314 | static |
7b33a0e0 | 1315 | void destroy_string_field(struct bt_field *field) |
273b65be | 1316 | { |
7b33a0e0 | 1317 | struct bt_field_string *string_field = (void *) field; |
273b65be | 1318 | |
8b45963b | 1319 | BT_ASSERT(field); |
7b33a0e0 PP |
1320 | BT_LIB_LOGD("Destroying string field object: %!+f", field); |
1321 | bt_field_finalize(field); | |
8deee039 | 1322 | |
7b33a0e0 PP |
1323 | if (string_field->buf) { |
1324 | g_array_free(string_field->buf, TRUE); | |
1248f5ea | 1325 | string_field->buf = NULL; |
273b65be | 1326 | } |
7b33a0e0 PP |
1327 | |
1328 | g_free(field); | |
273b65be JG |
1329 | } |
1330 | ||
7b33a0e0 PP |
1331 | BT_HIDDEN |
1332 | void bt_field_destroy(struct bt_field *field) | |
12c8a1a3 | 1333 | { |
8b45963b | 1334 | BT_ASSERT(field); |
5d2d8d18 PP |
1335 | |
1336 | switch (field->class->type) { | |
1337 | case BT_FIELD_CLASS_TYPE_BOOL: | |
1338 | destroy_bool_field(field); | |
1339 | break; | |
1340 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: | |
1341 | destroy_bit_array_field(field); | |
1342 | break; | |
1343 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
1344 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
1345 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
1346 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
1347 | destroy_integer_field(field); | |
1348 | break; | |
1349 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: | |
1350 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: | |
1351 | destroy_real_field(field); | |
1352 | break; | |
1353 | case BT_FIELD_CLASS_TYPE_STRING: | |
1354 | destroy_string_field(field); | |
1355 | break; | |
1356 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1357 | destroy_structure_field(field); | |
1358 | break; | |
1359 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
1360 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: | |
1361 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: | |
1362 | destroy_array_field(field); | |
1363 | break; | |
1364 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: | |
1365 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: | |
1366 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
1367 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
1368 | destroy_option_field(field); | |
1369 | break; | |
1370 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: | |
1371 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
1372 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
1373 | destroy_variant_field(field); | |
1374 | break; | |
1375 | default: | |
24847fc7 | 1376 | bt_common_abort(); |
5d2d8d18 | 1377 | } |
12c8a1a3 JG |
1378 | } |
1379 | ||
18acc6f8 | 1380 | static |
7b33a0e0 | 1381 | void reset_single_field(struct bt_field *field) |
12c8a1a3 | 1382 | { |
ec4a3354 | 1383 | BT_ASSERT_DBG(field); |
7b33a0e0 | 1384 | field->is_set = false; |
12c8a1a3 JG |
1385 | } |
1386 | ||
18acc6f8 | 1387 | static |
7b33a0e0 | 1388 | void reset_structure_field(struct bt_field *field) |
12c8a1a3 | 1389 | { |
7b33a0e0 PP |
1390 | uint64_t i; |
1391 | struct bt_field_structure *struct_field = (void *) field; | |
12c8a1a3 | 1392 | |
ec4a3354 | 1393 | BT_ASSERT_DBG(field); |
7b33a0e0 PP |
1394 | |
1395 | for (i = 0; i < struct_field->fields->len; i++) { | |
1396 | bt_field_reset(struct_field->fields->pdata[i]); | |
1397 | } | |
12c8a1a3 JG |
1398 | } |
1399 | ||
247fa9e3 PP |
1400 | static |
1401 | void reset_option_field(struct bt_field *field) | |
1402 | { | |
1403 | struct bt_field_option *opt_field = (void *) field; | |
1404 | ||
ec4a3354 | 1405 | BT_ASSERT_DBG(opt_field); |
247fa9e3 PP |
1406 | bt_field_reset(opt_field->content_field); |
1407 | opt_field->selected_field = NULL; | |
1408 | } | |
1409 | ||
18acc6f8 | 1410 | static |
7b33a0e0 | 1411 | void reset_variant_field(struct bt_field *field) |
12c8a1a3 | 1412 | { |
7b33a0e0 PP |
1413 | uint64_t i; |
1414 | struct bt_field_variant *var_field = (void *) field; | |
12c8a1a3 | 1415 | |
ec4a3354 | 1416 | BT_ASSERT_DBG(field); |
8b45963b | 1417 | |
7b33a0e0 PP |
1418 | for (i = 0; i < var_field->fields->len; i++) { |
1419 | bt_field_reset(var_field->fields->pdata[i]); | |
12c8a1a3 | 1420 | } |
12c8a1a3 JG |
1421 | } |
1422 | ||
18acc6f8 | 1423 | static |
7b33a0e0 | 1424 | void reset_array_field(struct bt_field *field) |
12c8a1a3 | 1425 | { |
a6918753 | 1426 | uint64_t i; |
7b33a0e0 | 1427 | struct bt_field_array *array_field = (void *) field; |
12c8a1a3 | 1428 | |
ec4a3354 | 1429 | BT_ASSERT_DBG(field); |
8b45963b | 1430 | |
7b33a0e0 PP |
1431 | for (i = 0; i < array_field->fields->len; i++) { |
1432 | bt_field_reset(array_field->fields->pdata[i]); | |
12c8a1a3 | 1433 | } |
12c8a1a3 JG |
1434 | } |
1435 | ||
18acc6f8 | 1436 | static |
7b33a0e0 | 1437 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1438 | { |
a6918753 | 1439 | field->frozen = is_frozen; |
918be005 PP |
1440 | } |
1441 | ||
18acc6f8 | 1442 | static |
7b33a0e0 | 1443 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1444 | { |
a6918753 | 1445 | uint64_t i; |
7b33a0e0 | 1446 | struct bt_field_structure *struct_field = (void *) field; |
918be005 | 1447 | |
7b33a0e0 PP |
1448 | BT_LIB_LOGD("Setting structure field's frozen state: " |
1449 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1450 | |
7b33a0e0 PP |
1451 | for (i = 0; i < struct_field->fields->len; i++) { |
1452 | struct bt_field *member_field = struct_field->fields->pdata[i]; | |
918be005 | 1453 | |
7b33a0e0 PP |
1454 | BT_LIB_LOGD("Setting structure field's member field's " |
1455 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1456 | member_field, i); | |
247fa9e3 | 1457 | _bt_field_set_is_frozen(member_field, is_frozen); |
918be005 PP |
1458 | } |
1459 | ||
7b33a0e0 | 1460 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1461 | } |
1462 | ||
247fa9e3 PP |
1463 | static |
1464 | void set_option_field_is_frozen(struct bt_field *field, bool is_frozen) | |
1465 | { | |
1466 | struct bt_field_option *opt_field = (void *) field; | |
1467 | ||
1468 | BT_LIB_LOGD("Setting option field's frozen state: " | |
1469 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
1470 | _bt_field_set_is_frozen(opt_field->content_field, is_frozen); | |
1471 | set_single_field_is_frozen(field, is_frozen); | |
1472 | } | |
1473 | ||
18acc6f8 | 1474 | static |
7b33a0e0 | 1475 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1476 | { |
a6918753 | 1477 | uint64_t i; |
7b33a0e0 | 1478 | struct bt_field_variant *var_field = (void *) field; |
918be005 | 1479 | |
7b33a0e0 PP |
1480 | BT_LIB_LOGD("Setting variant field's frozen state: " |
1481 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
a6918753 | 1482 | |
7b33a0e0 PP |
1483 | for (i = 0; i < var_field->fields->len; i++) { |
1484 | struct bt_field *option_field = var_field->fields->pdata[i]; | |
a6918753 | 1485 | |
7b33a0e0 PP |
1486 | BT_LIB_LOGD("Setting variant field's option field's " |
1487 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1488 | option_field, i); | |
247fa9e3 | 1489 | _bt_field_set_is_frozen(option_field, is_frozen); |
a6918753 PP |
1490 | } |
1491 | ||
7b33a0e0 | 1492 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1493 | } |
1494 | ||
18acc6f8 | 1495 | static |
7b33a0e0 | 1496 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1497 | { |
7b33a0e0 | 1498 | uint64_t i; |
18acc6f8 | 1499 | struct bt_field_array *array_field = (void *) field; |
918be005 | 1500 | |
7b33a0e0 PP |
1501 | BT_LIB_LOGD("Setting array field's frozen state: " |
1502 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1503 | |
7b33a0e0 PP |
1504 | for (i = 0; i < array_field->fields->len; i++) { |
1505 | struct bt_field *elem_field = array_field->fields->pdata[i]; | |
918be005 | 1506 | |
7b33a0e0 PP |
1507 | BT_LIB_LOGD("Setting array field's element field's " |
1508 | "frozen state: %![field-]+f, index=%" PRIu64, | |
fc25abce | 1509 | elem_field, i); |
247fa9e3 | 1510 | _bt_field_set_is_frozen(elem_field, is_frozen); |
918be005 PP |
1511 | } |
1512 | ||
7b33a0e0 | 1513 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1514 | } |
1515 | ||
1516 | BT_HIDDEN | |
78cf9df6 | 1517 | void _bt_field_set_is_frozen(const struct bt_field *field, |
a6918753 | 1518 | bool is_frozen) |
918be005 | 1519 | { |
ec4a3354 | 1520 | BT_ASSERT_DBG(field); |
7b33a0e0 | 1521 | BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d", |
a6918753 | 1522 | field, is_frozen); |
ec4a3354 | 1523 | BT_ASSERT_DBG(field->methods->set_is_frozen); |
78cf9df6 | 1524 | field->methods->set_is_frozen((void *) field, is_frozen); |
918be005 | 1525 | } |
76f869ab | 1526 | |
18acc6f8 | 1527 | static |
78cf9df6 | 1528 | bool single_field_is_set(const struct bt_field *field) |
76f869ab | 1529 | { |
ec4a3354 | 1530 | BT_ASSERT_DBG(field); |
7b33a0e0 | 1531 | return field->is_set; |
76f869ab JG |
1532 | } |
1533 | ||
18acc6f8 | 1534 | static |
78cf9df6 | 1535 | bool structure_field_is_set(const struct bt_field *field) |
76f869ab | 1536 | { |
7b33a0e0 PP |
1537 | bool is_set = true; |
1538 | uint64_t i; | |
78cf9df6 | 1539 | const struct bt_field_structure *struct_field = (const void *) field; |
76f869ab | 1540 | |
ec4a3354 | 1541 | BT_ASSERT_DBG(field); |
8deee039 | 1542 | |
7b33a0e0 PP |
1543 | for (i = 0; i < struct_field->fields->len; i++) { |
1544 | is_set = bt_field_is_set(struct_field->fields->pdata[i]); | |
53d65c1d | 1545 | if (!is_set) { |
76f869ab JG |
1546 | goto end; |
1547 | } | |
1548 | } | |
8deee039 | 1549 | |
76f869ab | 1550 | end: |
53d65c1d | 1551 | return is_set; |
76f869ab JG |
1552 | } |
1553 | ||
247fa9e3 PP |
1554 | static |
1555 | bool option_field_is_set(const struct bt_field *field) | |
1556 | { | |
1557 | const struct bt_field_option *opt_field = (const void *) field; | |
1558 | bool is_set = false; | |
1559 | ||
ec4a3354 | 1560 | BT_ASSERT_DBG(field); |
247fa9e3 PP |
1561 | |
1562 | if (opt_field->selected_field) { | |
1563 | is_set = bt_field_is_set(opt_field->selected_field); | |
1564 | } | |
1565 | ||
1566 | return is_set; | |
1567 | } | |
1568 | ||
18acc6f8 | 1569 | static |
78cf9df6 | 1570 | bool variant_field_is_set(const struct bt_field *field) |
76f869ab | 1571 | { |
78cf9df6 | 1572 | const struct bt_field_variant *var_field = (const void *) field; |
7b33a0e0 | 1573 | bool is_set = false; |
76f869ab | 1574 | |
ec4a3354 | 1575 | BT_ASSERT_DBG(field); |
8deee039 | 1576 | |
7b33a0e0 PP |
1577 | if (var_field->selected_field) { |
1578 | is_set = bt_field_is_set(var_field->selected_field); | |
76f869ab | 1579 | } |
8deee039 | 1580 | |
53d65c1d | 1581 | return is_set; |
76f869ab JG |
1582 | } |
1583 | ||
18acc6f8 | 1584 | static |
78cf9df6 | 1585 | bool array_field_is_set(const struct bt_field *field) |
76f869ab | 1586 | { |
7b33a0e0 PP |
1587 | bool is_set = true; |
1588 | uint64_t i; | |
78cf9df6 | 1589 | const struct bt_field_array *array_field = (const void *) field; |
76f869ab | 1590 | |
ec4a3354 | 1591 | BT_ASSERT_DBG(field); |
8deee039 | 1592 | |
7b33a0e0 PP |
1593 | for (i = 0; i < array_field->length; i++) { |
1594 | is_set = bt_field_is_set(array_field->fields->pdata[i]); | |
53d65c1d | 1595 | if (!is_set) { |
76f869ab JG |
1596 | goto end; |
1597 | } | |
1598 | } | |
8deee039 | 1599 | |
76f869ab | 1600 | end: |
53d65c1d | 1601 | return is_set; |
76f869ab | 1602 | } |