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