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