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