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