Commit | Line | Data |
---|---|---|
939190b3 | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
939190b3 PP |
3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
939190b3 PP |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
b03487ab | 24 | #define BT_LOG_TAG "LIB/FIELD-CLASS" |
1633ef46 | 25 | #include "lib/logging.h" |
939190b3 | 26 | |
57952005 | 27 | #include "lib/assert-pre.h" |
71c5da58 | 28 | #include <babeltrace2/trace-ir/field-class.h> |
71c5da58 | 29 | #include <babeltrace2/trace-ir/field.h> |
71c5da58 | 30 | #include <babeltrace2/trace-ir/clock-class.h> |
57952005 MJ |
31 | #include "lib/object.h" |
32 | #include "compat/compiler.h" | |
33 | #include "compat/endian.h" | |
34 | #include "common/assert.h" | |
35 | #include "compat/glib.h" | |
939190b3 PP |
36 | #include <float.h> |
37 | #include <inttypes.h> | |
969c1d8a | 38 | #include <stdbool.h> |
939190b3 PP |
39 | #include <stdlib.h> |
40 | ||
57952005 MJ |
41 | #include "clock-class.h" |
42 | #include "field-class.h" | |
43 | #include "field.h" | |
44 | #include "field-path.h" | |
45 | #include "utils.h" | |
fb25b9e3 | 46 | #include "lib/func-status.h" |
02b61fe0 | 47 | #include "lib/integer-range-set.h" |
af90138b | 48 | #include "lib/value.h" |
57952005 | 49 | |
78cf9df6 PP |
50 | enum bt_field_class_type bt_field_class_get_type( |
51 | const struct bt_field_class *fc) | |
939190b3 | 52 | { |
fa6cfec3 | 53 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
af0c18e3 | 54 | return fc->type; |
939190b3 PP |
55 | } |
56 | ||
57 | static | |
af90138b | 58 | int init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, |
939190b3 PP |
59 | bt_object_release_func release_func) |
60 | { | |
af90138b PP |
61 | int ret = 0; |
62 | ||
939190b3 | 63 | BT_ASSERT(fc); |
939190b3 PP |
64 | BT_ASSERT(release_func); |
65 | bt_object_init_shared(&fc->base, release_func); | |
af0c18e3 | 66 | fc->type = type; |
af90138b PP |
67 | fc->user_attributes = bt_value_map_create(); |
68 | if (!fc->user_attributes) { | |
69 | BT_LIB_LOGE_APPEND_CAUSE( | |
70 | "Failed to create a map value object."); | |
71 | ret = -1; | |
72 | goto end; | |
73 | } | |
74 | ||
75 | end: | |
76 | return ret; | |
77 | } | |
78 | ||
79 | static | |
80 | void finalize_field_class(struct bt_field_class *fc) | |
81 | { | |
82 | BT_OBJECT_PUT_REF_AND_RESET(fc->user_attributes); | |
939190b3 PP |
83 | } |
84 | ||
6943da6a PP |
85 | static |
86 | void destroy_bit_array_field_class(struct bt_object *obj) | |
87 | { | |
88 | BT_ASSERT(obj); | |
89 | BT_LIB_LOGD("Destroying bit array field class object: %!+F", obj); | |
af90138b | 90 | finalize_field_class((void *) obj); |
6943da6a PP |
91 | g_free(obj); |
92 | } | |
93 | ||
94 | struct bt_field_class *bt_field_class_bit_array_create( | |
95 | struct bt_trace_class *trace_class, uint64_t length) | |
96 | { | |
97 | struct bt_field_class_bit_array *ba_fc = NULL; | |
98 | ||
7c7324d3 | 99 | BT_ASSERT_PRE_NO_ERROR(); |
6943da6a PP |
100 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
101 | BT_ASSERT_PRE(length > 0 && length <= 64, | |
102 | "Unsupported length for bit array field class " | |
103 | "(minimum is 1, maximum is 64): length=%" PRIu64, length); | |
104 | BT_LOGD("Creating default bit array field class object."); | |
105 | ba_fc = g_new0(struct bt_field_class_bit_array, 1); | |
106 | if (!ba_fc) { | |
107 | BT_LIB_LOGE_APPEND_CAUSE( | |
108 | "Failed to allocate one bit array field class."); | |
109 | goto error; | |
110 | } | |
111 | ||
af90138b PP |
112 | if (init_field_class((void *) ba_fc, BT_FIELD_CLASS_TYPE_BIT_ARRAY, |
113 | destroy_bit_array_field_class)) { | |
114 | goto error; | |
115 | } | |
116 | ||
6943da6a PP |
117 | ba_fc->length = length; |
118 | BT_LIB_LOGD("Created bit array field class object: %!+F", ba_fc); | |
119 | goto end; | |
120 | ||
121 | error: | |
122 | BT_OBJECT_PUT_REF_AND_RESET(ba_fc); | |
123 | ||
124 | end: | |
125 | return (void *) ba_fc; | |
126 | } | |
127 | ||
128 | uint64_t bt_field_class_bit_array_get_length(const struct bt_field_class *fc) | |
129 | { | |
130 | const struct bt_field_class_bit_array *ba_fc = (const void *) fc; | |
131 | ||
132 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
133 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_BIT_ARRAY, | |
134 | "Field class"); | |
135 | return ba_fc->length; | |
136 | } | |
137 | ||
9414b439 PP |
138 | static |
139 | void destroy_bool_field_class(struct bt_object *obj) | |
140 | { | |
141 | BT_ASSERT(obj); | |
142 | BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj); | |
af90138b | 143 | finalize_field_class((void *) obj); |
9414b439 PP |
144 | g_free(obj); |
145 | } | |
146 | ||
147 | struct bt_field_class *bt_field_class_bool_create( | |
148 | bt_trace_class *trace_class) | |
149 | { | |
150 | struct bt_field_class_bool *bool_fc = NULL; | |
151 | ||
7c7324d3 | 152 | BT_ASSERT_PRE_NO_ERROR(); |
9414b439 PP |
153 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
154 | BT_LOGD("Creating default boolean field class object."); | |
155 | bool_fc = g_new0(struct bt_field_class_bool, 1); | |
156 | if (!bool_fc) { | |
157 | BT_LIB_LOGE_APPEND_CAUSE( | |
158 | "Failed to allocate one boolean field class."); | |
159 | goto error; | |
160 | } | |
161 | ||
af90138b PP |
162 | if (init_field_class((void *) bool_fc, BT_FIELD_CLASS_TYPE_BOOL, |
163 | destroy_bool_field_class)) { | |
164 | goto error; | |
165 | } | |
166 | ||
9414b439 PP |
167 | BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc); |
168 | goto end; | |
169 | ||
170 | error: | |
171 | BT_OBJECT_PUT_REF_AND_RESET(bool_fc); | |
172 | ||
173 | end: | |
174 | return (void *) bool_fc; | |
175 | } | |
176 | ||
939190b3 | 177 | static |
af90138b | 178 | int init_integer_field_class(struct bt_field_class_integer *fc, |
af0c18e3 | 179 | enum bt_field_class_type type, |
939190b3 PP |
180 | bt_object_release_func release_func) |
181 | { | |
af90138b PP |
182 | int ret; |
183 | ||
184 | ret = init_field_class((void *) fc, type, release_func); | |
185 | if (ret) { | |
186 | goto end; | |
187 | } | |
188 | ||
939190b3 PP |
189 | fc->range = 64; |
190 | fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; | |
af90138b PP |
191 | |
192 | end: | |
193 | return ret; | |
939190b3 PP |
194 | } |
195 | ||
196 | static | |
197 | void destroy_integer_field_class(struct bt_object *obj) | |
198 | { | |
199 | BT_ASSERT(obj); | |
9e550e5f | 200 | BT_LIB_LOGD("Destroying integer field class object: %!+F", obj); |
af90138b | 201 | finalize_field_class((void *) obj); |
939190b3 PP |
202 | g_free(obj); |
203 | } | |
204 | ||
205 | static inline | |
b7396828 PP |
206 | struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class, |
207 | enum bt_field_class_type type) | |
939190b3 PP |
208 | { |
209 | struct bt_field_class_integer *int_fc = NULL; | |
210 | ||
b7396828 | 211 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
9e550e5f | 212 | BT_LOGD("Creating default integer field class object: type=%s", |
af0c18e3 | 213 | bt_common_field_class_type_string(type)); |
939190b3 PP |
214 | int_fc = g_new0(struct bt_field_class_integer, 1); |
215 | if (!int_fc) { | |
a8f90e5d PP |
216 | BT_LIB_LOGE_APPEND_CAUSE( |
217 | "Failed to allocate one integer field class."); | |
939190b3 PP |
218 | goto error; |
219 | } | |
220 | ||
af90138b PP |
221 | if (init_integer_field_class(int_fc, type, |
222 | destroy_integer_field_class)) { | |
223 | goto error; | |
224 | } | |
225 | ||
9e550e5f | 226 | BT_LIB_LOGD("Created integer field class object: %!+F", int_fc); |
939190b3 PP |
227 | goto end; |
228 | ||
229 | error: | |
8138bfe1 | 230 | BT_OBJECT_PUT_REF_AND_RESET(int_fc); |
939190b3 PP |
231 | |
232 | end: | |
233 | return (void *) int_fc; | |
234 | } | |
235 | ||
60bbfc7c | 236 | struct bt_field_class *bt_field_class_integer_unsigned_create( |
b7396828 | 237 | bt_trace_class *trace_class) |
939190b3 | 238 | { |
7c7324d3 SM |
239 | BT_ASSERT_PRE_NO_ERROR(); |
240 | ||
b7396828 | 241 | return create_integer_field_class(trace_class, |
9e550e5f | 242 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER); |
939190b3 PP |
243 | } |
244 | ||
60bbfc7c | 245 | struct bt_field_class *bt_field_class_integer_signed_create( |
b7396828 | 246 | bt_trace_class *trace_class) |
939190b3 | 247 | { |
7c7324d3 SM |
248 | BT_ASSERT_PRE_NO_ERROR(); |
249 | ||
b7396828 | 250 | return create_integer_field_class(trace_class, |
9e550e5f | 251 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); |
939190b3 PP |
252 | } |
253 | ||
254 | uint64_t bt_field_class_integer_get_field_value_range( | |
78cf9df6 | 255 | const struct bt_field_class *fc) |
939190b3 | 256 | { |
78cf9df6 | 257 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
939190b3 | 258 | |
fa6cfec3 PP |
259 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
260 | BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class"); | |
939190b3 PP |
261 | return int_fc->range; |
262 | } | |
263 | ||
939190b3 PP |
264 | static |
265 | bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc, | |
266 | uint64_t size) | |
267 | { | |
268 | // TODO | |
269 | return true; | |
270 | } | |
271 | ||
78cf9df6 PP |
272 | void bt_field_class_integer_set_field_value_range( |
273 | struct bt_field_class *fc, uint64_t size) | |
939190b3 PP |
274 | { |
275 | struct bt_field_class_integer *int_fc = (void *) fc; | |
276 | ||
277 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
278 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
fa6cfec3 | 279 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
939190b3 | 280 | BT_ASSERT_PRE(size <= 64, |
9e550e5f | 281 | "Unsupported size for integer field class's field value range " |
939190b3 | 282 | "(maximum is 64): size=%" PRIu64, size); |
af0c18e3 PP |
283 | BT_ASSERT_PRE( |
284 | int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || | |
285 | int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || | |
939190b3 | 286 | size_is_valid_for_enumeration_field_class(fc, size), |
9e550e5f | 287 | "Invalid field value range for enumeration field class: " |
939190b3 PP |
288 | "at least one of the current mapping ranges contains values " |
289 | "which are outside this range: %!+F, size=%" PRIu64, fc, size); | |
290 | int_fc->range = size; | |
a684a357 | 291 | BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc); |
939190b3 PP |
292 | } |
293 | ||
294 | enum bt_field_class_integer_preferred_display_base | |
78cf9df6 | 295 | bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc) |
939190b3 | 296 | { |
78cf9df6 | 297 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
939190b3 | 298 | |
fa6cfec3 PP |
299 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
300 | BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class"); | |
939190b3 PP |
301 | return int_fc->base; |
302 | } | |
303 | ||
78cf9df6 PP |
304 | void bt_field_class_integer_set_preferred_display_base( |
305 | struct bt_field_class *fc, | |
939190b3 PP |
306 | enum bt_field_class_integer_preferred_display_base base) |
307 | { | |
308 | struct bt_field_class_integer *int_fc = (void *) fc; | |
309 | ||
310 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
311 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
fa6cfec3 | 312 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
939190b3 | 313 | int_fc->base = base; |
a684a357 | 314 | BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc); |
939190b3 PP |
315 | } |
316 | ||
317 | static | |
318 | void finalize_enumeration_field_class_mapping( | |
319 | struct bt_field_class_enumeration_mapping *mapping) | |
320 | { | |
321 | BT_ASSERT(mapping); | |
322 | ||
323 | if (mapping->label) { | |
324 | g_string_free(mapping->label, TRUE); | |
02b61fe0 | 325 | mapping->label = NULL; |
939190b3 PP |
326 | } |
327 | ||
02b61fe0 | 328 | BT_OBJECT_PUT_REF_AND_RESET(mapping->range_set); |
939190b3 PP |
329 | } |
330 | ||
331 | static | |
332 | void destroy_enumeration_field_class(struct bt_object *obj) | |
333 | { | |
334 | struct bt_field_class_enumeration *fc = (void *) obj; | |
335 | ||
336 | BT_ASSERT(fc); | |
9e550e5f | 337 | BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc); |
af90138b | 338 | finalize_field_class((void *) obj); |
939190b3 PP |
339 | |
340 | if (fc->mappings) { | |
341 | uint64_t i; | |
342 | ||
343 | for (i = 0; i < fc->mappings->len; i++) { | |
344 | finalize_enumeration_field_class_mapping( | |
345 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i)); | |
346 | } | |
347 | ||
348 | g_array_free(fc->mappings, TRUE); | |
1248f5ea | 349 | fc->mappings = NULL; |
939190b3 PP |
350 | } |
351 | ||
352 | if (fc->label_buf) { | |
353 | g_ptr_array_free(fc->label_buf, TRUE); | |
1248f5ea | 354 | fc->label_buf = NULL; |
939190b3 PP |
355 | } |
356 | ||
357 | g_free(fc); | |
358 | } | |
359 | ||
360 | static | |
78cf9df6 | 361 | struct bt_field_class *create_enumeration_field_class( |
b7396828 | 362 | bt_trace_class *trace_class, enum bt_field_class_type type) |
939190b3 PP |
363 | { |
364 | struct bt_field_class_enumeration *enum_fc = NULL; | |
365 | ||
b7396828 | 366 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
9e550e5f | 367 | BT_LOGD("Creating default enumeration field class object: type=%s", |
af0c18e3 | 368 | bt_common_field_class_type_string(type)); |
939190b3 PP |
369 | enum_fc = g_new0(struct bt_field_class_enumeration, 1); |
370 | if (!enum_fc) { | |
a8f90e5d PP |
371 | BT_LIB_LOGE_APPEND_CAUSE( |
372 | "Failed to allocate one enumeration field class."); | |
939190b3 PP |
373 | goto error; |
374 | } | |
375 | ||
af90138b PP |
376 | if (init_integer_field_class((void *) enum_fc, type, |
377 | destroy_enumeration_field_class)) { | |
378 | goto error; | |
379 | } | |
380 | ||
939190b3 PP |
381 | enum_fc->mappings = g_array_new(FALSE, TRUE, |
382 | sizeof(struct bt_field_class_enumeration_mapping)); | |
383 | if (!enum_fc->mappings) { | |
a8f90e5d | 384 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
939190b3 PP |
385 | goto error; |
386 | } | |
387 | ||
388 | enum_fc->label_buf = g_ptr_array_new(); | |
389 | if (!enum_fc->label_buf) { | |
a8f90e5d | 390 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
939190b3 PP |
391 | goto error; |
392 | } | |
393 | ||
9e550e5f | 394 | BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc); |
939190b3 PP |
395 | goto end; |
396 | ||
397 | error: | |
8138bfe1 | 398 | BT_OBJECT_PUT_REF_AND_RESET(enum_fc); |
939190b3 PP |
399 | |
400 | end: | |
401 | return (void *) enum_fc; | |
402 | } | |
403 | ||
60bbfc7c | 404 | struct bt_field_class *bt_field_class_enumeration_unsigned_create( |
b7396828 | 405 | bt_trace_class *trace_class) |
939190b3 | 406 | { |
7c7324d3 SM |
407 | BT_ASSERT_PRE_NO_ERROR(); |
408 | ||
b7396828 | 409 | return create_enumeration_field_class(trace_class, |
af0c18e3 | 410 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
939190b3 PP |
411 | } |
412 | ||
60bbfc7c | 413 | struct bt_field_class *bt_field_class_enumeration_signed_create( |
b7396828 | 414 | bt_trace_class *trace_class) |
939190b3 | 415 | { |
7c7324d3 SM |
416 | BT_ASSERT_PRE_NO_ERROR(); |
417 | ||
b7396828 | 418 | return create_enumeration_field_class(trace_class, |
af0c18e3 | 419 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
939190b3 PP |
420 | } |
421 | ||
78cf9df6 PP |
422 | uint64_t bt_field_class_enumeration_get_mapping_count( |
423 | const struct bt_field_class *fc) | |
939190b3 | 424 | { |
78cf9df6 | 425 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
939190b3 | 426 | |
fa6cfec3 PP |
427 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
428 | BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc, "Field class"); | |
939190b3 PP |
429 | return (uint64_t) enum_fc->mappings->len; |
430 | } | |
431 | ||
60bbfc7c PP |
432 | const struct bt_field_class_enumeration_unsigned_mapping * |
433 | bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( | |
b6baf3bb | 434 | const struct bt_field_class *fc, uint64_t index) |
939190b3 | 435 | { |
78cf9df6 | 436 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
939190b3 | 437 | |
fa6cfec3 PP |
438 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
439 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len); | |
440 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, | |
939190b3 | 441 | "Field class"); |
b6baf3bb | 442 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
939190b3 PP |
443 | } |
444 | ||
60bbfc7c PP |
445 | const struct bt_field_class_enumeration_signed_mapping * |
446 | bt_field_class_enumeration_signed_borrow_mapping_by_index_const( | |
b6baf3bb | 447 | const struct bt_field_class *fc, uint64_t index) |
939190b3 | 448 | { |
78cf9df6 | 449 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
939190b3 | 450 | |
fa6cfec3 PP |
451 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
452 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len); | |
453 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, | |
939190b3 | 454 | "Field class"); |
b6baf3bb | 455 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
939190b3 PP |
456 | } |
457 | ||
02b61fe0 PP |
458 | static |
459 | const struct bt_field_class_enumeration_mapping * | |
460 | borrow_enumeration_field_class_mapping_by_label( | |
461 | const struct bt_field_class_enumeration *fc, const char *label) | |
939190b3 | 462 | { |
02b61fe0 PP |
463 | struct bt_field_class_enumeration_mapping *mapping = NULL; |
464 | uint64_t i; | |
465 | ||
ec4a3354 | 466 | BT_ASSERT_DBG(fc); |
02b61fe0 PP |
467 | BT_ASSERT_PRE_DEV_NON_NULL(label, "Label"); |
468 | ||
469 | for (i = 0; i < fc->mappings->len; i++) { | |
470 | struct bt_field_class_enumeration_mapping *this_mapping = | |
471 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i); | |
472 | ||
473 | if (strcmp(this_mapping->label->str, label) == 0) { | |
474 | mapping = this_mapping; | |
475 | goto end; | |
476 | } | |
477 | } | |
478 | ||
479 | end: | |
480 | return mapping; | |
939190b3 PP |
481 | } |
482 | ||
60bbfc7c PP |
483 | const struct bt_field_class_enumeration_signed_mapping * |
484 | bt_field_class_enumeration_signed_borrow_mapping_by_label_const( | |
02b61fe0 | 485 | const struct bt_field_class *fc, const char *label) |
939190b3 | 486 | { |
02b61fe0 PP |
487 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
488 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, | |
489 | "Field class"); | |
490 | return (const void *) borrow_enumeration_field_class_mapping_by_label( | |
491 | (const void *) fc, label); | |
939190b3 PP |
492 | } |
493 | ||
60bbfc7c PP |
494 | const struct bt_field_class_enumeration_unsigned_mapping * |
495 | bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const( | |
02b61fe0 | 496 | const struct bt_field_class *fc, const char *label) |
939190b3 | 497 | { |
02b61fe0 PP |
498 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
499 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, | |
500 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field class"); | |
501 | return (const void *) borrow_enumeration_field_class_mapping_by_label( | |
502 | (const void *) fc, label); | |
503 | } | |
939190b3 | 504 | |
02b61fe0 PP |
505 | const char *bt_field_class_enumeration_mapping_get_label( |
506 | const struct bt_field_class_enumeration_mapping *mapping) | |
507 | { | |
508 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); | |
509 | return mapping->label->str; | |
939190b3 PP |
510 | } |
511 | ||
02b61fe0 | 512 | const struct bt_integer_range_set_unsigned * |
60bbfc7c PP |
513 | bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
514 | const struct bt_field_class_enumeration_unsigned_mapping *u_mapping) | |
939190b3 | 515 | { |
02b61fe0 PP |
516 | const struct bt_field_class_enumeration_mapping *mapping = |
517 | (const void *) u_mapping; | |
518 | ||
519 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); | |
520 | return (const void *) mapping->range_set; | |
939190b3 PP |
521 | } |
522 | ||
02b61fe0 | 523 | const struct bt_integer_range_set_signed * |
60bbfc7c PP |
524 | bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
525 | const struct bt_field_class_enumeration_signed_mapping *s_mapping) | |
939190b3 | 526 | { |
02b61fe0 PP |
527 | const struct bt_field_class_enumeration_mapping *mapping = |
528 | (const void *) s_mapping; | |
529 | ||
530 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); | |
531 | return (const void *) mapping->range_set; | |
939190b3 PP |
532 | } |
533 | ||
1091bb60 | 534 | enum bt_field_class_enumeration_get_mapping_labels_for_value_status |
60bbfc7c | 535 | bt_field_class_enumeration_unsigned_get_mapping_labels_for_value( |
78cf9df6 | 536 | const struct bt_field_class *fc, uint64_t value, |
939190b3 PP |
537 | bt_field_class_enumeration_mapping_label_array *label_array, |
538 | uint64_t *count) | |
539 | { | |
78cf9df6 | 540 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
939190b3 PP |
541 | uint64_t i; |
542 | ||
7c7324d3 | 543 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 PP |
544 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
545 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); | |
546 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); | |
547 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, | |
939190b3 PP |
548 | "Field class"); |
549 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
550 | ||
551 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
552 | uint64_t j; | |
78cf9df6 | 553 | const struct bt_field_class_enumeration_mapping *mapping = |
939190b3 PP |
554 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
555 | ||
02b61fe0 PP |
556 | for (j = 0; j < mapping->range_set->ranges->len; j++) { |
557 | const struct bt_integer_range *range = (const void *) | |
558 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
559 | mapping->range_set, j); | |
939190b3 PP |
560 | |
561 | if (value >= range->lower.u && | |
562 | value <= range->upper.u) { | |
563 | g_ptr_array_add(enum_fc->label_buf, | |
564 | mapping->label->str); | |
565 | break; | |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | *label_array = (void *) enum_fc->label_buf->pdata; | |
571 | *count = (uint64_t) enum_fc->label_buf->len; | |
fb25b9e3 | 572 | return BT_FUNC_STATUS_OK; |
939190b3 PP |
573 | } |
574 | ||
1091bb60 | 575 | enum bt_field_class_enumeration_get_mapping_labels_for_value_status |
60bbfc7c | 576 | bt_field_class_enumeration_signed_get_mapping_labels_for_value( |
78cf9df6 | 577 | const struct bt_field_class *fc, int64_t value, |
939190b3 PP |
578 | bt_field_class_enumeration_mapping_label_array *label_array, |
579 | uint64_t *count) | |
580 | { | |
78cf9df6 | 581 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
939190b3 PP |
582 | uint64_t i; |
583 | ||
7c7324d3 | 584 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 PP |
585 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
586 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); | |
587 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); | |
588 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, | |
939190b3 PP |
589 | "Field class"); |
590 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
591 | ||
592 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
593 | uint64_t j; | |
78cf9df6 | 594 | const struct bt_field_class_enumeration_mapping *mapping = |
939190b3 PP |
595 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
596 | ||
02b61fe0 PP |
597 | for (j = 0; j < mapping->range_set->ranges->len; j++) { |
598 | const struct bt_integer_range *range = (const void *) | |
599 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
600 | mapping->range_set, j); | |
939190b3 PP |
601 | |
602 | if (value >= range->lower.i && | |
603 | value <= range->upper.i) { | |
604 | g_ptr_array_add(enum_fc->label_buf, | |
605 | mapping->label->str); | |
606 | break; | |
607 | } | |
608 | } | |
609 | } | |
610 | ||
611 | *label_array = (void *) enum_fc->label_buf->pdata; | |
612 | *count = (uint64_t) enum_fc->label_buf->len; | |
fb25b9e3 | 613 | return BT_FUNC_STATUS_OK; |
939190b3 PP |
614 | } |
615 | ||
02b61fe0 PP |
616 | static |
617 | bool enumeration_field_class_has_mapping_with_label( | |
618 | const struct bt_field_class_enumeration *enum_fc, | |
619 | const char *label) | |
939190b3 | 620 | { |
939190b3 | 621 | uint64_t i; |
02b61fe0 | 622 | bool exists = false; |
939190b3 | 623 | |
02b61fe0 PP |
624 | BT_ASSERT(enum_fc); |
625 | BT_ASSERT(label); | |
939190b3 | 626 | |
939190b3 PP |
627 | for (i = 0; i < enum_fc->mappings->len; i++) { |
628 | struct bt_field_class_enumeration_mapping *mapping_candidate = | |
629 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); | |
630 | ||
631 | if (strcmp(mapping_candidate->label->str, label) == 0) { | |
02b61fe0 PP |
632 | exists = true; |
633 | goto end; | |
939190b3 PP |
634 | } |
635 | } | |
636 | ||
02b61fe0 PP |
637 | end: |
638 | return exists; | |
639 | } | |
939190b3 | 640 | |
02b61fe0 PP |
641 | static inline |
642 | enum bt_field_class_enumeration_add_mapping_status | |
643 | add_mapping_to_enumeration_field_class(struct bt_field_class *fc, | |
644 | const char *label, const struct bt_integer_range_set *range_set) | |
645 | { | |
646 | enum bt_field_class_enumeration_add_mapping_status status = | |
647 | BT_FUNC_STATUS_OK; | |
648 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
649 | struct bt_field_class_enumeration_mapping mapping = { 0 }; | |
650 | ||
7c7324d3 | 651 | BT_ASSERT_PRE_NO_ERROR(); |
02b61fe0 PP |
652 | BT_ASSERT(fc); |
653 | BT_ASSERT_PRE_NON_NULL(label, "Label"); | |
88efd0a7 | 654 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
02b61fe0 PP |
655 | BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label( |
656 | enum_fc, label), | |
657 | "Duplicate mapping name in enumeration field class: " | |
658 | "%![enum-fc-]+F, label=\"%s\"", fc, label); | |
659 | mapping.range_set = range_set; | |
660 | bt_object_get_ref(mapping.range_set); | |
661 | mapping.label = g_string_new(label); | |
662 | if (!mapping.label) { | |
663 | finalize_enumeration_field_class_mapping(&mapping); | |
664 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
665 | goto end; | |
939190b3 PP |
666 | } |
667 | ||
02b61fe0 | 668 | g_array_append_val(enum_fc->mappings, mapping); |
a684a357 | 669 | BT_LIB_LOGD("Added mapping to enumeration field class: " |
02b61fe0 | 670 | "%![fc-]+F, label=\"%s\"", fc, label); |
939190b3 PP |
671 | |
672 | end: | |
02b61fe0 | 673 | return status; |
939190b3 PP |
674 | } |
675 | ||
02b61fe0 | 676 | enum bt_field_class_enumeration_add_mapping_status |
60bbfc7c | 677 | bt_field_class_enumeration_unsigned_add_mapping( |
78cf9df6 | 678 | struct bt_field_class *fc, const char *label, |
02b61fe0 | 679 | const struct bt_integer_range_set_unsigned *range_set) |
939190b3 | 680 | { |
7c7324d3 | 681 | BT_ASSERT_PRE_NO_ERROR(); |
939190b3 | 682 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
af0c18e3 | 683 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
939190b3 | 684 | "Field class"); |
02b61fe0 PP |
685 | return add_mapping_to_enumeration_field_class(fc, label, |
686 | (const void *) range_set); | |
687 | } | |
688 | ||
689 | enum bt_field_class_enumeration_add_mapping_status | |
60bbfc7c | 690 | bt_field_class_enumeration_signed_add_mapping( |
78cf9df6 | 691 | struct bt_field_class *fc, const char *label, |
02b61fe0 | 692 | const struct bt_integer_range_set_signed *range_set) |
939190b3 | 693 | { |
7c7324d3 | 694 | BT_ASSERT_PRE_NO_ERROR(); |
939190b3 | 695 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
af0c18e3 | 696 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
939190b3 | 697 | "Field class"); |
02b61fe0 PP |
698 | return add_mapping_to_enumeration_field_class(fc, label, |
699 | (const void *) range_set); | |
939190b3 PP |
700 | } |
701 | ||
702 | static | |
703 | void destroy_real_field_class(struct bt_object *obj) | |
704 | { | |
705 | BT_ASSERT(obj); | |
9e550e5f | 706 | BT_LIB_LOGD("Destroying real field class object: %!+F", obj); |
af90138b | 707 | finalize_field_class((void *) obj); |
939190b3 PP |
708 | g_free(obj); |
709 | } | |
710 | ||
76276a81 FD |
711 | static |
712 | struct bt_field_class *create_real_field_class(bt_trace_class *trace_class, | |
713 | enum bt_field_class_type type) | |
939190b3 PP |
714 | { |
715 | struct bt_field_class_real *real_fc = NULL; | |
716 | ||
b7396828 | 717 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
76276a81 FD |
718 | BT_LOGD("Creating default real field class object: type=%s", |
719 | bt_common_field_class_type_string(type)); | |
939190b3 PP |
720 | real_fc = g_new0(struct bt_field_class_real, 1); |
721 | if (!real_fc) { | |
a8f90e5d | 722 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class."); |
939190b3 PP |
723 | goto error; |
724 | } | |
725 | ||
76276a81 | 726 | if (init_field_class((void *) real_fc, type, destroy_real_field_class)) { |
af90138b PP |
727 | goto error; |
728 | } | |
729 | ||
9e550e5f | 730 | BT_LIB_LOGD("Created real field class object: %!+F", real_fc); |
939190b3 PP |
731 | goto end; |
732 | ||
733 | error: | |
8138bfe1 | 734 | BT_OBJECT_PUT_REF_AND_RESET(real_fc); |
939190b3 PP |
735 | |
736 | end: | |
737 | return (void *) real_fc; | |
738 | } | |
739 | ||
76276a81 FD |
740 | struct bt_field_class *bt_field_class_real_single_precision_create( |
741 | bt_trace_class *trace_class) | |
939190b3 | 742 | { |
7c7324d3 SM |
743 | BT_ASSERT_PRE_NO_ERROR(); |
744 | ||
76276a81 FD |
745 | return create_real_field_class(trace_class, |
746 | BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL); | |
939190b3 PP |
747 | } |
748 | ||
76276a81 FD |
749 | struct bt_field_class *bt_field_class_real_double_precision_create( |
750 | bt_trace_class *trace_class) | |
939190b3 | 751 | { |
7c7324d3 SM |
752 | BT_ASSERT_PRE_NO_ERROR(); |
753 | ||
76276a81 FD |
754 | return create_real_field_class(trace_class, |
755 | BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL); | |
939190b3 PP |
756 | } |
757 | ||
758 | static | |
759 | int init_named_field_classes_container( | |
760 | struct bt_field_class_named_field_class_container *fc, | |
78cf9df6 | 761 | enum bt_field_class_type type, |
02b61fe0 PP |
762 | bt_object_release_func fc_release_func, |
763 | GDestroyNotify named_fc_destroy_func) | |
939190b3 PP |
764 | { |
765 | int ret = 0; | |
766 | ||
af90138b PP |
767 | ret = init_field_class((void *) fc, type, fc_release_func); |
768 | if (ret) { | |
769 | goto end; | |
770 | } | |
771 | ||
02b61fe0 | 772 | fc->named_fcs = g_ptr_array_new_with_free_func(named_fc_destroy_func); |
939190b3 | 773 | if (!fc->named_fcs) { |
02b61fe0 | 774 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
939190b3 PP |
775 | ret = -1; |
776 | goto end; | |
777 | } | |
778 | ||
779 | fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal); | |
780 | if (!fc->name_to_index) { | |
a8f90e5d | 781 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable."); |
939190b3 PP |
782 | ret = -1; |
783 | goto end; | |
784 | } | |
785 | ||
786 | end: | |
787 | return ret; | |
788 | } | |
789 | ||
790 | static | |
791 | void finalize_named_field_class(struct bt_named_field_class *named_fc) | |
792 | { | |
793 | BT_ASSERT(named_fc); | |
9e550e5f | 794 | BT_LIB_LOGD("Finalizing named field class: " |
939190b3 PP |
795 | "addr=%p, name=\"%s\", %![fc-]+F", |
796 | named_fc, named_fc->name ? named_fc->name->str : NULL, | |
797 | named_fc->fc); | |
af90138b | 798 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes); |
939190b3 PP |
799 | |
800 | if (named_fc->name) { | |
801 | g_string_free(named_fc->name, TRUE); | |
02b61fe0 | 802 | named_fc->name = NULL; |
939190b3 PP |
803 | } |
804 | ||
9e550e5f | 805 | BT_LOGD_STR("Putting named field class's field class."); |
1248f5ea | 806 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc); |
939190b3 PP |
807 | } |
808 | ||
02b61fe0 PP |
809 | static |
810 | void destroy_named_field_class(gpointer ptr) | |
811 | { | |
af90138b PP |
812 | struct bt_named_field_class *named_fc = ptr; |
813 | ||
02b61fe0 | 814 | if (ptr) { |
560d197d | 815 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes); |
02b61fe0 PP |
816 | finalize_named_field_class(ptr); |
817 | g_free(ptr); | |
818 | } | |
819 | } | |
820 | ||
821 | static | |
8bc207af | 822 | void destroy_variant_with_selector_field_option(gpointer ptr) |
02b61fe0 | 823 | { |
8bc207af | 824 | struct bt_field_class_variant_with_selector_field_option *opt = ptr; |
02b61fe0 PP |
825 | |
826 | if (ptr) { | |
827 | finalize_named_field_class(&opt->common); | |
828 | BT_OBJECT_PUT_REF_AND_RESET(opt->range_set); | |
829 | g_free(ptr); | |
830 | } | |
831 | } | |
832 | ||
939190b3 PP |
833 | static |
834 | void finalize_named_field_classes_container( | |
835 | struct bt_field_class_named_field_class_container *fc) | |
836 | { | |
939190b3 PP |
837 | BT_ASSERT(fc); |
838 | ||
839 | if (fc->named_fcs) { | |
02b61fe0 PP |
840 | g_ptr_array_free(fc->named_fcs, TRUE); |
841 | fc->named_fcs = NULL; | |
939190b3 | 842 | |
939190b3 PP |
843 | } |
844 | ||
845 | if (fc->name_to_index) { | |
846 | g_hash_table_destroy(fc->name_to_index); | |
02b61fe0 | 847 | fc->name_to_index = NULL; |
939190b3 PP |
848 | } |
849 | } | |
850 | ||
851 | static | |
852 | void destroy_structure_field_class(struct bt_object *obj) | |
853 | { | |
854 | BT_ASSERT(obj); | |
2d95225e | 855 | BT_LIB_LOGD("Destroying structure field class object: %!+F", obj); |
af90138b | 856 | finalize_field_class((void *) obj); |
939190b3 PP |
857 | finalize_named_field_classes_container((void *) obj); |
858 | g_free(obj); | |
859 | } | |
860 | ||
b7396828 PP |
861 | struct bt_field_class *bt_field_class_structure_create( |
862 | bt_trace_class *trace_class) | |
939190b3 PP |
863 | { |
864 | int ret; | |
865 | struct bt_field_class_structure *struct_fc = NULL; | |
866 | ||
7c7324d3 | 867 | BT_ASSERT_PRE_NO_ERROR(); |
b7396828 | 868 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
9e550e5f | 869 | BT_LOGD_STR("Creating default structure field class object."); |
939190b3 PP |
870 | struct_fc = g_new0(struct bt_field_class_structure, 1); |
871 | if (!struct_fc) { | |
a8f90e5d PP |
872 | BT_LIB_LOGE_APPEND_CAUSE( |
873 | "Failed to allocate one structure field class."); | |
939190b3 PP |
874 | goto error; |
875 | } | |
876 | ||
877 | ret = init_named_field_classes_container((void *) struct_fc, | |
02b61fe0 PP |
878 | BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class, |
879 | destroy_named_field_class); | |
939190b3 | 880 | if (ret) { |
02b61fe0 | 881 | /* init_named_field_classes_container() logs errors */ |
939190b3 PP |
882 | goto error; |
883 | } | |
884 | ||
9e550e5f | 885 | BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc); |
939190b3 PP |
886 | goto end; |
887 | ||
888 | error: | |
8138bfe1 | 889 | BT_OBJECT_PUT_REF_AND_RESET(struct_fc); |
939190b3 PP |
890 | |
891 | end: | |
892 | return (void *) struct_fc; | |
893 | } | |
894 | ||
895 | static | |
02b61fe0 | 896 | int init_named_field_class(struct bt_named_field_class *named_fc, |
939190b3 PP |
897 | const char *name, struct bt_field_class *fc) |
898 | { | |
02b61fe0 | 899 | int status = BT_FUNC_STATUS_OK; |
939190b3 | 900 | |
02b61fe0 PP |
901 | BT_ASSERT(named_fc); |
902 | BT_ASSERT(name); | |
903 | BT_ASSERT(fc); | |
904 | named_fc->name = g_string_new(name); | |
905 | if (!named_fc->name) { | |
a8f90e5d | 906 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
02b61fe0 | 907 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
939190b3 PP |
908 | goto end; |
909 | } | |
910 | ||
af90138b PP |
911 | named_fc->user_attributes = bt_value_map_create(); |
912 | if (!named_fc->user_attributes) { | |
913 | BT_LIB_LOGE_APPEND_CAUSE( | |
914 | "Failed to create a map value object."); | |
915 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
916 | goto end; | |
917 | } | |
918 | ||
4b70020d | 919 | named_fc->fc = fc; |
864aa43f | 920 | bt_object_get_ref_no_null_check(named_fc->fc); |
e24f271a | 921 | |
02b61fe0 PP |
922 | end: |
923 | return status; | |
924 | } | |
925 | ||
926 | static | |
927 | struct bt_named_field_class *create_named_field_class(const char *name, | |
928 | struct bt_field_class *fc) | |
929 | { | |
930 | struct bt_named_field_class *named_fc = g_new0( | |
931 | struct bt_named_field_class, 1); | |
932 | ||
933 | if (!named_fc) { | |
934 | BT_LIB_LOGE_APPEND_CAUSE( | |
935 | "Failed to allocate a named field class."); | |
936 | goto error; | |
937 | } | |
938 | ||
939 | if (init_named_field_class(named_fc, name, fc)) { | |
940 | /* init_named_field_class() logs errors */ | |
941 | goto error; | |
942 | } | |
943 | ||
944 | goto end; | |
945 | ||
946 | error: | |
947 | destroy_named_field_class(named_fc); | |
948 | named_fc = NULL; | |
939190b3 PP |
949 | |
950 | end: | |
02b61fe0 PP |
951 | return named_fc; |
952 | } | |
953 | ||
954 | static | |
8bc207af PP |
955 | struct bt_field_class_variant_with_selector_field_option * |
956 | create_variant_with_selector_field_option( | |
02b61fe0 PP |
957 | const char *name, struct bt_field_class *fc, |
958 | const struct bt_integer_range_set *range_set) | |
959 | { | |
8bc207af PP |
960 | struct bt_field_class_variant_with_selector_field_option *opt = g_new0( |
961 | struct bt_field_class_variant_with_selector_field_option, 1); | |
02b61fe0 PP |
962 | |
963 | BT_ASSERT(range_set); | |
964 | ||
965 | if (!opt) { | |
966 | BT_LIB_LOGE_APPEND_CAUSE( | |
967 | "Failed to allocate a named field class."); | |
968 | goto error; | |
969 | } | |
970 | ||
971 | if (init_named_field_class(&opt->common, name, fc)) { | |
972 | goto error; | |
973 | } | |
974 | ||
975 | opt->range_set = range_set; | |
864aa43f | 976 | bt_object_get_ref_no_null_check(opt->range_set); |
02b61fe0 PP |
977 | bt_integer_range_set_freeze(range_set); |
978 | goto end; | |
979 | ||
980 | error: | |
8bc207af | 981 | destroy_variant_with_selector_field_option(opt); |
02b61fe0 PP |
982 | opt = NULL; |
983 | ||
984 | end: | |
985 | return opt; | |
986 | } | |
987 | ||
988 | static | |
989 | int append_named_field_class_to_container_field_class( | |
990 | struct bt_field_class_named_field_class_container *container_fc, | |
991 | struct bt_named_field_class *named_fc) | |
992 | { | |
993 | BT_ASSERT(container_fc); | |
994 | BT_ASSERT(named_fc); | |
995 | BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class"); | |
996 | BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index, | |
997 | named_fc->name->str), | |
998 | "Duplicate member/option name in structure/variant field class: " | |
999 | "%![container-fc-]+F, name=\"%s\"", container_fc, | |
1000 | named_fc->name->str); | |
af90138b PP |
1001 | |
1002 | /* | |
1003 | * Freeze the contained field class, but not the named field | |
1004 | * class itself, as it's still possible afterwards to modify | |
1005 | * properties of the member/option object. | |
1006 | */ | |
1007 | bt_field_class_freeze(named_fc->fc); | |
02b61fe0 PP |
1008 | g_ptr_array_add(container_fc->named_fcs, named_fc); |
1009 | g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, | |
1010 | GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); | |
1011 | return BT_FUNC_STATUS_OK; | |
939190b3 PP |
1012 | } |
1013 | ||
fb25b9e3 PP |
1014 | enum bt_field_class_structure_append_member_status |
1015 | bt_field_class_structure_append_member( | |
4b514ab3 PP |
1016 | struct bt_field_class *fc, const char *name, |
1017 | struct bt_field_class *member_fc) | |
939190b3 | 1018 | { |
02b61fe0 PP |
1019 | enum bt_field_class_structure_append_member_status status; |
1020 | struct bt_named_field_class *named_fc = NULL; | |
9e550e5f | 1021 | |
7c7324d3 | 1022 | BT_ASSERT_PRE_NO_ERROR(); |
939190b3 | 1023 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
909d0a89 PP |
1024 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
1025 | "Field class"); | |
02b61fe0 PP |
1026 | named_fc = create_named_field_class(name, member_fc); |
1027 | if (!named_fc) { | |
1028 | /* create_named_field_class() logs errors */ | |
1029 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1030 | goto end; | |
1031 | } | |
1032 | ||
1033 | status = append_named_field_class_to_container_field_class((void *) fc, | |
1034 | named_fc); | |
1035 | if (status == BT_FUNC_STATUS_OK) { | |
1036 | /* Moved to the container */ | |
1037 | named_fc = NULL; | |
1038 | } | |
1039 | ||
1040 | end: | |
1041 | return status; | |
939190b3 PP |
1042 | } |
1043 | ||
78cf9df6 PP |
1044 | uint64_t bt_field_class_structure_get_member_count( |
1045 | const struct bt_field_class *fc) | |
939190b3 PP |
1046 | { |
1047 | struct bt_field_class_structure *struct_fc = (void *) fc; | |
1048 | ||
fa6cfec3 PP |
1049 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1050 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
909d0a89 | 1051 | "Field class"); |
939190b3 PP |
1052 | return (uint64_t) struct_fc->common.named_fcs->len; |
1053 | } | |
1054 | ||
1055 | static | |
e24f271a PP |
1056 | struct bt_named_field_class * |
1057 | borrow_named_field_class_from_container_field_class_at_index( | |
909d0a89 | 1058 | struct bt_field_class_named_field_class_container *fc, |
e24f271a | 1059 | uint64_t index) |
939190b3 | 1060 | { |
ec4a3354 | 1061 | BT_ASSERT_DBG(fc); |
fa6cfec3 | 1062 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len); |
02b61fe0 | 1063 | return fc->named_fcs->pdata[index]; |
939190b3 PP |
1064 | } |
1065 | ||
e24f271a PP |
1066 | const struct bt_field_class_structure_member * |
1067 | bt_field_class_structure_borrow_member_by_index_const( | |
1068 | const struct bt_field_class *fc, uint64_t index) | |
939190b3 | 1069 | { |
fa6cfec3 PP |
1070 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1071 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
909d0a89 | 1072 | "Field class"); |
e24f271a PP |
1073 | return (const void *) |
1074 | borrow_named_field_class_from_container_field_class_at_index( | |
1075 | (void *) fc, index); | |
909d0a89 PP |
1076 | } |
1077 | ||
e24f271a PP |
1078 | struct bt_field_class_structure_member * |
1079 | bt_field_class_structure_borrow_member_by_index( | |
1080 | struct bt_field_class *fc, uint64_t index) | |
909d0a89 | 1081 | { |
fa6cfec3 PP |
1082 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1083 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
909d0a89 | 1084 | "Field class"); |
e24f271a PP |
1085 | return (void *) |
1086 | borrow_named_field_class_from_container_field_class_at_index( | |
1087 | (void *) fc, index); | |
9e550e5f PP |
1088 | } |
1089 | ||
939190b3 | 1090 | static |
e24f271a PP |
1091 | struct bt_named_field_class * |
1092 | borrow_named_field_class_from_container_field_class_by_name( | |
909d0a89 | 1093 | struct bt_field_class_named_field_class_container *fc, |
939190b3 PP |
1094 | const char *name) |
1095 | { | |
e24f271a | 1096 | struct bt_named_field_class *named_fc = NULL; |
939190b3 PP |
1097 | gpointer orig_key; |
1098 | gpointer value; | |
1099 | ||
ec4a3354 | 1100 | BT_ASSERT_DBG(fc); |
fa6cfec3 | 1101 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Name"); |
939190b3 PP |
1102 | if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key, |
1103 | &value)) { | |
1104 | goto end; | |
1105 | } | |
1106 | ||
02b61fe0 | 1107 | named_fc = fc->named_fcs->pdata[GPOINTER_TO_UINT(value)]; |
939190b3 PP |
1108 | |
1109 | end: | |
e24f271a | 1110 | return named_fc; |
939190b3 PP |
1111 | } |
1112 | ||
e24f271a | 1113 | const struct bt_field_class_structure_member * |
c0db9072 | 1114 | bt_field_class_structure_borrow_member_by_name_const( |
78cf9df6 | 1115 | const struct bt_field_class *fc, const char *name) |
939190b3 | 1116 | { |
fa6cfec3 PP |
1117 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1118 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
909d0a89 | 1119 | "Field class"); |
e24f271a PP |
1120 | return (const void *) |
1121 | borrow_named_field_class_from_container_field_class_by_name( | |
909d0a89 PP |
1122 | (void *) fc, name); |
1123 | } | |
1124 | ||
e24f271a | 1125 | struct bt_field_class_structure_member * |
fe65b1fe | 1126 | bt_field_class_structure_borrow_member_by_name( |
909d0a89 PP |
1127 | struct bt_field_class *fc, const char *name) |
1128 | { | |
fa6cfec3 PP |
1129 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1130 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
909d0a89 | 1131 | "Field class"); |
e24f271a PP |
1132 | return (void *) |
1133 | borrow_named_field_class_from_container_field_class_by_name( | |
78cf9df6 | 1134 | (void *) fc, name); |
9e550e5f PP |
1135 | } |
1136 | ||
e24f271a PP |
1137 | const char *bt_field_class_structure_member_get_name( |
1138 | const struct bt_field_class_structure_member *member) | |
1139 | { | |
1140 | const struct bt_named_field_class *named_fc = (const void *) member; | |
1141 | ||
fa6cfec3 | 1142 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); |
e24f271a PP |
1143 | return named_fc->name->str; |
1144 | } | |
1145 | ||
1146 | const struct bt_field_class * | |
1147 | bt_field_class_structure_member_borrow_field_class_const( | |
1148 | const struct bt_field_class_structure_member *member) | |
1149 | { | |
1150 | const struct bt_named_field_class *named_fc = (const void *) member; | |
1151 | ||
fa6cfec3 | 1152 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); |
e24f271a PP |
1153 | return named_fc->fc; |
1154 | } | |
1155 | ||
87040955 FD |
1156 | struct bt_field_class * |
1157 | bt_field_class_structure_member_borrow_field_class( | |
1158 | struct bt_field_class_structure_member *member) | |
1159 | { | |
1160 | struct bt_named_field_class *named_fc = (void *) member; | |
1161 | ||
1162 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); | |
1163 | return named_fc->fc; | |
1164 | } | |
1165 | ||
247fa9e3 PP |
1166 | static |
1167 | void destroy_option_field_class(struct bt_object *obj) | |
1168 | { | |
1169 | struct bt_field_class_option *fc = (void *) obj; | |
1170 | ||
1171 | BT_ASSERT(fc); | |
1172 | BT_LIB_LOGD("Destroying option field class object: %!+F", fc); | |
af90138b | 1173 | finalize_field_class((void *) obj); |
247fa9e3 PP |
1174 | BT_LOGD_STR("Putting content field class."); |
1175 | BT_OBJECT_PUT_REF_AND_RESET(fc->content_fc); | |
467673c1 | 1176 | |
8bc207af PP |
1177 | if (fc->common.type != BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD) { |
1178 | struct bt_field_class_option_with_selector_field *with_sel_fc = | |
467673c1 PP |
1179 | (void *) obj; |
1180 | ||
1181 | BT_LOGD_STR("Putting selector field path."); | |
1182 | BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc->selector_field_path); | |
1183 | BT_LOGD_STR("Putting selector field class."); | |
1184 | BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc->selector_fc); | |
1185 | ||
8bc207af PP |
1186 | if (fc->common.type != BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD) { |
1187 | struct bt_field_class_option_with_selector_field_integer *with_int_sel_fc = | |
467673c1 PP |
1188 | (void *) obj; |
1189 | ||
1190 | BT_LOGD_STR("Putting integer range set."); | |
1191 | BT_OBJECT_PUT_REF_AND_RESET(with_int_sel_fc->range_set); | |
1192 | } | |
1193 | } | |
1194 | ||
247fa9e3 PP |
1195 | g_free(fc); |
1196 | } | |
1197 | ||
467673c1 PP |
1198 | static |
1199 | struct bt_field_class *create_option_field_class( | |
1200 | struct bt_trace_class *trace_class, | |
1201 | enum bt_field_class_type fc_type, | |
1202 | struct bt_field_class *content_fc, | |
1203 | struct bt_field_class *selector_fc) | |
247fa9e3 PP |
1204 | { |
1205 | struct bt_field_class_option *opt_fc = NULL; | |
1206 | ||
1207 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); | |
1208 | BT_ASSERT_PRE_NON_NULL(content_fc, "Content field class"); | |
1209 | BT_LIB_LOGD("Creating option field class: " | |
467673c1 PP |
1210 | "type=%s, %![content-fc-]+F, %![sel-fc-]+F", |
1211 | bt_common_field_class_type_string(fc_type), | |
1212 | content_fc, selector_fc); | |
1213 | ||
8bc207af PP |
1214 | if (fc_type != BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD) { |
1215 | struct bt_field_class_option_with_selector_field *opt_with_sel_fc = NULL; | |
467673c1 PP |
1216 | |
1217 | BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class"); | |
1218 | ||
8bc207af | 1219 | if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD) { |
467673c1 PP |
1220 | BT_ASSERT_PRE_FC_HAS_ID(selector_fc, |
1221 | BT_FIELD_CLASS_TYPE_BOOL, | |
1222 | "Selector field class"); | |
1223 | opt_with_sel_fc = (void *) g_new0( | |
8bc207af | 1224 | struct bt_field_class_option_with_selector_field_bool, 1); |
467673c1 PP |
1225 | } else { |
1226 | BT_ASSERT_PRE_FC_IS_INT(selector_fc, | |
1227 | "Selector field class"); | |
1228 | opt_with_sel_fc = (void *) g_new0( | |
8bc207af | 1229 | struct bt_field_class_option_with_selector_field_integer, 1); |
467673c1 PP |
1230 | } |
1231 | ||
1232 | if (!opt_with_sel_fc) { | |
1233 | BT_LIB_LOGE_APPEND_CAUSE( | |
1234 | "Failed to allocate one option with selector field class."); | |
1235 | goto error; | |
1236 | } | |
1237 | ||
1238 | opt_with_sel_fc->selector_fc = selector_fc; | |
864aa43f | 1239 | bt_object_get_ref_no_null_check(opt_with_sel_fc->selector_fc); |
467673c1 PP |
1240 | opt_fc = (void *) opt_with_sel_fc; |
1241 | } else { | |
1242 | opt_fc = g_new0(struct bt_field_class_option, 1); | |
1243 | if (!opt_fc) { | |
1244 | BT_LIB_LOGE_APPEND_CAUSE( | |
1245 | "Failed to allocate one option field class."); | |
1246 | goto error; | |
1247 | } | |
247fa9e3 PP |
1248 | } |
1249 | ||
467673c1 PP |
1250 | BT_ASSERT(opt_fc); |
1251 | ||
1252 | if (init_field_class((void *) opt_fc, fc_type, | |
af90138b PP |
1253 | destroy_option_field_class)) { |
1254 | goto error; | |
1255 | } | |
1256 | ||
247fa9e3 | 1257 | opt_fc->content_fc = content_fc; |
864aa43f | 1258 | bt_object_get_ref_no_null_check(opt_fc->content_fc); |
247fa9e3 PP |
1259 | bt_field_class_freeze(opt_fc->content_fc); |
1260 | ||
1261 | if (selector_fc) { | |
247fa9e3 PP |
1262 | bt_field_class_freeze(selector_fc); |
1263 | } | |
1264 | ||
1265 | BT_LIB_LOGD("Created option field class object: " | |
1266 | "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc, selector_fc); | |
1267 | goto end; | |
1268 | ||
1269 | error: | |
1270 | BT_OBJECT_PUT_REF_AND_RESET(opt_fc); | |
1271 | ||
1272 | end: | |
1273 | return (void *) opt_fc; | |
1274 | } | |
1275 | ||
467673c1 PP |
1276 | struct bt_field_class *bt_field_class_option_without_selector_create( |
1277 | struct bt_trace_class *trace_class, | |
1278 | struct bt_field_class *content_fc) | |
1279 | { | |
7c7324d3 SM |
1280 | BT_ASSERT_PRE_NO_ERROR(); |
1281 | ||
467673c1 | 1282 | return create_option_field_class(trace_class, |
8bc207af | 1283 | BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD, |
467673c1 PP |
1284 | content_fc, NULL); |
1285 | } | |
1286 | ||
8bc207af | 1287 | struct bt_field_class *bt_field_class_option_with_selector_field_bool_create( |
467673c1 PP |
1288 | struct bt_trace_class *trace_class, |
1289 | struct bt_field_class *content_fc, | |
1290 | struct bt_field_class *selector_fc) | |
1291 | { | |
7c7324d3 | 1292 | BT_ASSERT_PRE_NO_ERROR(); |
467673c1 | 1293 | |
7c7324d3 SM |
1294 | return create_option_field_class(trace_class, |
1295 | BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD, | |
1296 | content_fc, selector_fc); | |
467673c1 PP |
1297 | } |
1298 | ||
1299 | struct bt_field_class * | |
8bc207af | 1300 | bt_field_class_option_with_selector_field_integer_unsigned_create( |
467673c1 PP |
1301 | struct bt_trace_class *trace_class, |
1302 | struct bt_field_class *content_fc, | |
1303 | struct bt_field_class *selector_fc, | |
1304 | const struct bt_integer_range_set_unsigned *u_range_set) | |
1305 | { | |
8bc207af | 1306 | struct bt_field_class_option_with_selector_field_integer *fc; |
467673c1 PP |
1307 | const struct bt_integer_range_set *range_set = |
1308 | (const void *) u_range_set; | |
1309 | ||
7c7324d3 | 1310 | BT_ASSERT_PRE_NO_ERROR(); |
467673c1 PP |
1311 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
1312 | BT_ASSERT_PRE(range_set->ranges->len > 0, | |
1313 | "Integer range set is empty: %!+R", range_set); | |
1314 | fc = (void *) create_option_field_class(trace_class, | |
8bc207af | 1315 | BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD, |
467673c1 PP |
1316 | content_fc, selector_fc); |
1317 | ||
1318 | if (!fc) { | |
1319 | goto end; | |
1320 | } | |
1321 | ||
1322 | fc->range_set = range_set; | |
864aa43f | 1323 | bt_object_get_ref_no_null_check(fc->range_set); |
467673c1 PP |
1324 | bt_integer_range_set_freeze(range_set); |
1325 | ||
1326 | end: | |
1327 | return (void *) fc; | |
1328 | } | |
1329 | ||
1330 | struct bt_field_class * | |
8bc207af | 1331 | bt_field_class_option_with_selector_field_integer_signed_create( |
467673c1 PP |
1332 | struct bt_trace_class *trace_class, |
1333 | struct bt_field_class *content_fc, | |
1334 | struct bt_field_class *selector_fc, | |
1335 | const struct bt_integer_range_set_signed *i_range_set) | |
1336 | { | |
8bc207af | 1337 | struct bt_field_class_option_with_selector_field_integer *fc; |
467673c1 PP |
1338 | const struct bt_integer_range_set *range_set = |
1339 | (const void *) i_range_set; | |
1340 | ||
7c7324d3 | 1341 | BT_ASSERT_PRE_NO_ERROR(); |
467673c1 PP |
1342 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
1343 | BT_ASSERT_PRE(range_set->ranges->len > 0, | |
1344 | "Integer range set is empty: %!+R", range_set); | |
1345 | fc = (void *) create_option_field_class(trace_class, | |
8bc207af | 1346 | BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD, |
467673c1 PP |
1347 | content_fc, selector_fc); |
1348 | ||
1349 | if (!fc) { | |
1350 | goto end; | |
1351 | } | |
1352 | ||
1353 | fc->range_set = range_set; | |
864aa43f | 1354 | bt_object_get_ref_no_null_check(fc->range_set); |
467673c1 PP |
1355 | bt_integer_range_set_freeze(range_set); |
1356 | ||
1357 | end: | |
1358 | return (void *) fc; | |
1359 | } | |
1360 | ||
247fa9e3 PP |
1361 | const struct bt_field_class *bt_field_class_option_borrow_field_class_const( |
1362 | const struct bt_field_class *fc) | |
1363 | { | |
1364 | struct bt_field_class_option *opt_fc = (void *) fc; | |
1365 | ||
1366 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
467673c1 | 1367 | BT_ASSERT_PRE_FC_IS_OPTION(fc, "Field class"); |
247fa9e3 PP |
1368 | return opt_fc->content_fc; |
1369 | } | |
1370 | ||
87040955 FD |
1371 | struct bt_field_class *bt_field_class_option_borrow_field_class( |
1372 | struct bt_field_class *fc) | |
1373 | { | |
1374 | struct bt_field_class_option *opt_fc = (void *) fc; | |
1375 | ||
1376 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
467673c1 | 1377 | BT_ASSERT_PRE_FC_IS_OPTION(fc, "Field class"); |
87040955 FD |
1378 | return opt_fc->content_fc; |
1379 | } | |
1380 | ||
247fa9e3 | 1381 | const struct bt_field_path * |
8bc207af | 1382 | bt_field_class_option_with_selector_field_borrow_selector_field_path_const( |
247fa9e3 PP |
1383 | const struct bt_field_class *fc) |
1384 | { | |
8bc207af | 1385 | const struct bt_field_class_option_with_selector_field *opt_fc = |
467673c1 | 1386 | (const void *) fc; |
247fa9e3 PP |
1387 | |
1388 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
467673c1 | 1389 | BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL(fc, "Field class"); |
247fa9e3 PP |
1390 | return opt_fc->selector_field_path; |
1391 | } | |
1392 | ||
8bc207af | 1393 | void bt_field_class_option_with_selector_field_bool_set_selector_is_reversed( |
467673c1 PP |
1394 | struct bt_field_class *fc, bt_bool sel_is_reversed) |
1395 | { | |
8bc207af | 1396 | struct bt_field_class_option_with_selector_field_bool *opt_fc = (void *) fc; |
467673c1 PP |
1397 | |
1398 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1399 | BT_ASSERT_PRE_FC_HAS_ID(fc, | |
8bc207af | 1400 | BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD, "Field class"); |
467673c1 PP |
1401 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
1402 | opt_fc->sel_is_reversed = sel_is_reversed; | |
1403 | } | |
1404 | ||
8bc207af | 1405 | bt_bool bt_field_class_option_with_selector_field_bool_selector_is_reversed( |
467673c1 PP |
1406 | const struct bt_field_class *fc) |
1407 | { | |
8bc207af | 1408 | struct bt_field_class_option_with_selector_field_bool *opt_fc = (void *) fc; |
467673c1 PP |
1409 | |
1410 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1411 | BT_ASSERT_PRE_FC_HAS_ID(fc, | |
8bc207af | 1412 | BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD, "Field class"); |
467673c1 PP |
1413 | return opt_fc->sel_is_reversed; |
1414 | } | |
1415 | ||
1416 | const struct bt_integer_range_set_unsigned * | |
8bc207af | 1417 | bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const( |
467673c1 PP |
1418 | const struct bt_field_class *fc) |
1419 | { | |
8bc207af | 1420 | struct bt_field_class_option_with_selector_field_integer *opt_fc = |
467673c1 PP |
1421 | (void *) fc; |
1422 | ||
1423 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1424 | BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc, "Field class"); | |
1425 | return (const void *) opt_fc->range_set; | |
1426 | } | |
1427 | ||
1428 | const struct bt_integer_range_set_signed * | |
8bc207af | 1429 | bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const( |
467673c1 PP |
1430 | const struct bt_field_class *fc) |
1431 | { | |
8bc207af | 1432 | struct bt_field_class_option_with_selector_field_integer *opt_fc = |
467673c1 PP |
1433 | (void *) fc; |
1434 | ||
1435 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1436 | BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc, "Field class"); | |
1437 | return (const void *) opt_fc->range_set; | |
1438 | } | |
1439 | ||
02b61fe0 PP |
1440 | static |
1441 | void finalize_variant_field_class(struct bt_field_class_variant *var_fc) | |
e24f271a | 1442 | { |
02b61fe0 PP |
1443 | BT_ASSERT(var_fc); |
1444 | BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc); | |
af90138b | 1445 | finalize_field_class((void *) var_fc); |
02b61fe0 | 1446 | finalize_named_field_classes_container((void *) var_fc); |
e24f271a PP |
1447 | } |
1448 | ||
939190b3 PP |
1449 | static |
1450 | void destroy_variant_field_class(struct bt_object *obj) | |
1451 | { | |
1452 | struct bt_field_class_variant *fc = (void *) obj; | |
1453 | ||
1454 | BT_ASSERT(fc); | |
02b61fe0 PP |
1455 | finalize_variant_field_class(fc); |
1456 | g_free(fc); | |
1457 | } | |
1458 | ||
1459 | static | |
8bc207af | 1460 | void destroy_variant_with_selector_field_field_class(struct bt_object *obj) |
02b61fe0 | 1461 | { |
8bc207af | 1462 | struct bt_field_class_variant_with_selector_field *fc = (void *) obj; |
02b61fe0 PP |
1463 | |
1464 | BT_ASSERT(fc); | |
1465 | finalize_variant_field_class(&fc->common); | |
939190b3 | 1466 | BT_LOGD_STR("Putting selector field path."); |
1248f5ea PP |
1467 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path); |
1468 | BT_LOGD_STR("Putting selector field class."); | |
1469 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc); | |
939190b3 PP |
1470 | g_free(fc); |
1471 | } | |
1472 | ||
b7396828 | 1473 | struct bt_field_class *bt_field_class_variant_create( |
02b61fe0 | 1474 | bt_trace_class *trace_class, bt_field_class *selector_fc) |
939190b3 PP |
1475 | { |
1476 | int ret; | |
1477 | struct bt_field_class_variant *var_fc = NULL; | |
8bc207af | 1478 | struct bt_field_class_variant_with_selector_field *var_with_sel_fc = NULL; |
02b61fe0 | 1479 | enum bt_field_class_type fc_type; |
939190b3 | 1480 | |
7c7324d3 | 1481 | BT_ASSERT_PRE_NO_ERROR(); |
b7396828 | 1482 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
02b61fe0 PP |
1483 | |
1484 | if (selector_fc) { | |
1485 | BT_ASSERT_PRE_FC_IS_INT(selector_fc, "Selector field class"); | |
939190b3 PP |
1486 | } |
1487 | ||
02b61fe0 PP |
1488 | BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F", |
1489 | selector_fc); | |
1490 | ||
1491 | if (selector_fc) { | |
1492 | var_with_sel_fc = g_new0( | |
8bc207af | 1493 | struct bt_field_class_variant_with_selector_field, 1); |
02b61fe0 PP |
1494 | if (!var_with_sel_fc) { |
1495 | BT_LIB_LOGE_APPEND_CAUSE( | |
1496 | "Failed to allocate one variant field class with selector."); | |
1497 | goto error; | |
1498 | } | |
1499 | ||
94847783 PP |
1500 | if (bt_field_class_type_is(selector_fc->type, |
1501 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) { | |
8bc207af | 1502 | fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD; |
02b61fe0 | 1503 | } else { |
8bc207af | 1504 | fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD; |
02b61fe0 PP |
1505 | } |
1506 | ||
1507 | ret = init_named_field_classes_container( | |
1508 | (void *) var_with_sel_fc, fc_type, | |
8bc207af PP |
1509 | destroy_variant_with_selector_field_field_class, |
1510 | destroy_variant_with_selector_field_option); | |
02b61fe0 PP |
1511 | if (ret) { |
1512 | /* init_named_field_classes_container() logs errors */ | |
1513 | goto error; | |
1514 | } | |
1515 | ||
1516 | var_with_sel_fc->selector_fc = selector_fc; | |
864aa43f | 1517 | bt_object_get_ref_no_null_check(var_with_sel_fc->selector_fc); |
02b61fe0 PP |
1518 | bt_field_class_freeze(selector_fc); |
1519 | var_fc = (void *) var_with_sel_fc; | |
ed47d321 FD |
1520 | BT_LIB_LOGD("Created default variant field class with selector object: " |
1521 | "%![var-fc-]+F, %![sel-fc-]+F", var_fc, selector_fc); | |
02b61fe0 PP |
1522 | } else { |
1523 | var_fc = g_new0(struct bt_field_class_variant, 1); | |
1524 | if (!var_fc) { | |
1525 | BT_LIB_LOGE_APPEND_CAUSE( | |
1526 | "Failed to allocate one variant field class without selector."); | |
1527 | goto error; | |
1528 | } | |
1529 | ||
1530 | ret = init_named_field_classes_container((void *) var_fc, | |
8bc207af | 1531 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD, |
02b61fe0 PP |
1532 | destroy_variant_field_class, destroy_named_field_class); |
1533 | if (ret) { | |
1534 | /* init_named_field_classes_container() logs errors */ | |
1535 | goto error; | |
1536 | } | |
ed47d321 FD |
1537 | BT_LIB_LOGD("Created default variant field class without selector object: " |
1538 | "%![var-fc-]+F", var_fc); | |
939190b3 PP |
1539 | } |
1540 | ||
02b61fe0 | 1541 | BT_ASSERT(var_fc); |
939190b3 PP |
1542 | goto end; |
1543 | ||
1544 | error: | |
8138bfe1 | 1545 | BT_OBJECT_PUT_REF_AND_RESET(var_fc); |
98b5e169 | 1546 | BT_OBJECT_PUT_REF_AND_RESET(var_with_sel_fc); |
939190b3 PP |
1547 | |
1548 | end: | |
1549 | return (void *) var_fc; | |
1550 | } | |
1551 | ||
02b61fe0 PP |
1552 | enum bt_field_class_variant_without_selector_append_option_status |
1553 | bt_field_class_variant_without_selector_append_option(struct bt_field_class *fc, | |
1554 | const char *name, struct bt_field_class *option_fc) | |
1555 | { | |
1556 | enum bt_field_class_variant_without_selector_append_option_status status; | |
1557 | struct bt_named_field_class *named_fc = NULL; | |
1558 | ||
7c7324d3 | 1559 | BT_ASSERT_PRE_NO_ERROR(); |
02b61fe0 PP |
1560 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
1561 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
1562 | BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class"); | |
1563 | BT_ASSERT_PRE_FC_HAS_ID(fc, | |
8bc207af | 1564 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD, "Field class"); |
02b61fe0 PP |
1565 | named_fc = create_named_field_class(name, option_fc); |
1566 | if (!named_fc) { | |
1567 | /* create_named_field_class() logs errors */ | |
1568 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1569 | goto end; | |
1570 | } | |
1571 | ||
1572 | status = append_named_field_class_to_container_field_class((void *) fc, | |
1573 | named_fc); | |
1574 | if (status == BT_FUNC_STATUS_OK) { | |
1575 | /* Moved to the container */ | |
1576 | named_fc = NULL; | |
1577 | } | |
1578 | ||
1579 | end: | |
1580 | if (named_fc) { | |
1581 | destroy_named_field_class(named_fc); | |
1582 | } | |
1583 | ||
1584 | return status; | |
939190b3 PP |
1585 | } |
1586 | ||
02b61fe0 PP |
1587 | static |
1588 | int ranges_overlap(GPtrArray *var_fc_opts, const struct bt_integer_range_set *range_set, | |
1589 | bool is_signed, bool *has_overlap) | |
1590 | { | |
1591 | int status = BT_FUNC_STATUS_OK; | |
1592 | struct bt_integer_range_set *full_range_set; | |
1593 | uint64_t i; | |
1594 | ||
1595 | *has_overlap = false; | |
1596 | ||
1597 | /* | |
1598 | * Build a single range set with all the ranges and test for | |
1599 | * overlaps. | |
1600 | */ | |
1601 | if (is_signed) { | |
1602 | full_range_set = (void *) bt_integer_range_set_signed_create(); | |
1603 | } else { | |
1604 | full_range_set = (void *) bt_integer_range_set_unsigned_create(); | |
1605 | } | |
1606 | ||
1607 | if (!full_range_set) { | |
1608 | BT_LOGE_STR("Failed to create a range set."); | |
1609 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1610 | goto end; | |
1611 | } | |
1612 | ||
1613 | /* Add existing option ranges */ | |
1614 | for (i = 0; i < var_fc_opts->len; i++) { | |
8bc207af | 1615 | struct bt_field_class_variant_with_selector_field_option *opt = |
02b61fe0 PP |
1616 | var_fc_opts->pdata[i]; |
1617 | uint64_t j; | |
1618 | ||
1619 | for (j = 0; j < opt->range_set->ranges->len; j++) { | |
1620 | struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
1621 | opt->range_set, j); | |
1622 | ||
1623 | if (is_signed) { | |
1624 | status = bt_integer_range_set_signed_add_range( | |
1625 | (void *) full_range_set, range->lower.i, | |
1626 | range->upper.i); | |
1627 | } else { | |
1628 | status = bt_integer_range_set_unsigned_add_range( | |
1629 | (void *) full_range_set, range->lower.u, | |
1630 | range->upper.u); | |
1631 | } | |
1632 | ||
1633 | if (status) { | |
1634 | goto end; | |
1635 | } | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | /* Add new ranges */ | |
1640 | for (i = 0; i < range_set->ranges->len; i++) { | |
1641 | struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
1642 | range_set, i); | |
1643 | ||
1644 | if (is_signed) { | |
1645 | status = bt_integer_range_set_signed_add_range( | |
1646 | (void *) full_range_set, range->lower.i, | |
1647 | range->upper.i); | |
1648 | } else { | |
1649 | status = bt_integer_range_set_unsigned_add_range( | |
1650 | (void *) full_range_set, range->lower.u, | |
1651 | range->upper.u); | |
1652 | } | |
1653 | ||
1654 | if (status) { | |
1655 | goto end; | |
1656 | } | |
1657 | } | |
1658 | ||
1659 | /* Check overlaps */ | |
1660 | if (is_signed) { | |
1661 | *has_overlap = bt_integer_range_set_signed_has_overlaps(full_range_set); | |
1662 | } else { | |
1663 | *has_overlap = bt_integer_range_set_unsigned_has_overlaps( | |
1664 | full_range_set); | |
1665 | } | |
1666 | ||
1667 | end: | |
1668 | bt_object_put_ref(full_range_set); | |
1669 | return status; | |
1670 | } | |
1671 | ||
1672 | static | |
8bc207af | 1673 | int append_option_to_variant_with_selector_field_field_class( |
02b61fe0 PP |
1674 | struct bt_field_class *fc, const char *name, |
1675 | struct bt_field_class *option_fc, | |
1676 | const struct bt_integer_range_set *range_set, | |
1677 | enum bt_field_class_type expected_type) | |
939190b3 | 1678 | { |
02b61fe0 | 1679 | int status; |
8bc207af PP |
1680 | struct bt_field_class_variant_with_selector_field *var_fc = (void *) fc; |
1681 | struct bt_field_class_variant_with_selector_field_option *opt = NULL; | |
02b61fe0 | 1682 | bool has_overlap; |
9e550e5f | 1683 | |
939190b3 | 1684 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
02b61fe0 PP |
1685 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
1686 | BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class"); | |
88efd0a7 | 1687 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
02b61fe0 PP |
1688 | BT_ASSERT_PRE_FC_HAS_ID(fc, expected_type, "Field class"); |
1689 | BT_ASSERT_PRE(range_set->ranges->len > 0, | |
88efd0a7 | 1690 | "Integer range set is empty: %!+R", range_set); |
02b61fe0 | 1691 | status = ranges_overlap(var_fc->common.common.named_fcs, range_set, |
8bc207af | 1692 | expected_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, |
02b61fe0 PP |
1693 | &has_overlap); |
1694 | if (status) { | |
1695 | /* ranges_overlap() logs errors */ | |
1696 | goto end; | |
1697 | } | |
1698 | ||
1699 | BT_ASSERT_PRE(!has_overlap, | |
88efd0a7 PP |
1700 | "Integer range set's ranges and existing ranges have an overlap: " |
1701 | "%!+R", range_set); | |
8bc207af | 1702 | opt = create_variant_with_selector_field_option(name, option_fc, range_set); |
02b61fe0 | 1703 | if (!opt) { |
8bc207af | 1704 | /* create_variant_with_selector_field_option() logs errors */ |
02b61fe0 PP |
1705 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
1706 | goto end; | |
1707 | } | |
1708 | ||
1709 | status = append_named_field_class_to_container_field_class((void *) fc, | |
1710 | &opt->common); | |
1711 | if (status == BT_FUNC_STATUS_OK) { | |
1712 | /* Moved to the container */ | |
1713 | opt = NULL; | |
1714 | } | |
1715 | ||
1716 | end: | |
1717 | if (opt) { | |
8bc207af | 1718 | destroy_variant_with_selector_field_option(opt); |
02b61fe0 PP |
1719 | } |
1720 | ||
1721 | return status; | |
1722 | } | |
1723 | ||
8bc207af PP |
1724 | enum bt_field_class_variant_with_selector_field_integer_append_option_status |
1725 | bt_field_class_variant_with_selector_field_integer_unsigned_append_option( | |
02b61fe0 PP |
1726 | struct bt_field_class *fc, const char *name, |
1727 | struct bt_field_class *option_fc, | |
1728 | const struct bt_integer_range_set_unsigned *range_set) | |
1729 | { | |
7c7324d3 SM |
1730 | BT_ASSERT_PRE_NO_ERROR(); |
1731 | ||
8bc207af | 1732 | return append_option_to_variant_with_selector_field_field_class(fc, |
02b61fe0 | 1733 | name, option_fc, (const void *) range_set, |
8bc207af | 1734 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD); |
02b61fe0 PP |
1735 | } |
1736 | ||
8bc207af PP |
1737 | enum bt_field_class_variant_with_selector_field_integer_append_option_status |
1738 | bt_field_class_variant_with_selector_field_integer_signed_append_option( | |
02b61fe0 PP |
1739 | struct bt_field_class *fc, const char *name, |
1740 | struct bt_field_class *option_fc, | |
1741 | const struct bt_integer_range_set_signed *range_set) | |
1742 | { | |
7c7324d3 SM |
1743 | BT_ASSERT_PRE_NO_ERROR(); |
1744 | ||
8bc207af | 1745 | return append_option_to_variant_with_selector_field_field_class(fc, |
02b61fe0 | 1746 | name, option_fc, (const void *) range_set, |
8bc207af | 1747 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD); |
02b61fe0 PP |
1748 | } |
1749 | ||
1750 | uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc) | |
1751 | { | |
1752 | const struct bt_field_class_variant *var_fc = (const void *) fc; | |
1753 | ||
1754 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
1755 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); | |
1756 | return (uint64_t) var_fc->common.named_fcs->len; | |
939190b3 PP |
1757 | } |
1758 | ||
e24f271a | 1759 | const struct bt_field_class_variant_option * |
c0db9072 | 1760 | bt_field_class_variant_borrow_option_by_name_const( |
78cf9df6 | 1761 | const struct bt_field_class *fc, const char *name) |
939190b3 | 1762 | { |
fa6cfec3 | 1763 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
02b61fe0 | 1764 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
e24f271a PP |
1765 | return (const void *) |
1766 | borrow_named_field_class_from_container_field_class_by_name( | |
1767 | (void *) fc, name); | |
909d0a89 PP |
1768 | } |
1769 | ||
02b61fe0 PP |
1770 | const struct bt_field_class_variant_option * |
1771 | bt_field_class_variant_borrow_option_by_index_const( | |
1772 | const struct bt_field_class *fc, uint64_t index) | |
909d0a89 | 1773 | { |
fa6cfec3 | 1774 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
02b61fe0 PP |
1775 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
1776 | return (const void *) | |
1777 | borrow_named_field_class_from_container_field_class_at_index( | |
34dd2c30 PP |
1778 | (void *) fc, index); |
1779 | } | |
1780 | ||
1781 | struct bt_field_class_variant_option * | |
1782 | bt_field_class_variant_borrow_option_by_name( | |
1783 | struct bt_field_class *fc, const char *name) | |
1784 | { | |
1785 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
1786 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); | |
1787 | return (void *) | |
1788 | borrow_named_field_class_from_container_field_class_by_name( | |
1789 | (void *) fc, name); | |
1790 | } | |
1791 | ||
1792 | struct bt_field_class_variant_option * | |
1793 | bt_field_class_variant_borrow_option_by_index( | |
1794 | struct bt_field_class *fc, uint64_t index) | |
1795 | { | |
1796 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
1797 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); | |
1798 | return (void *) | |
1799 | borrow_named_field_class_from_container_field_class_at_index( | |
02b61fe0 | 1800 | (void *) fc, index); |
9e550e5f PP |
1801 | } |
1802 | ||
8bc207af PP |
1803 | const struct bt_field_class_variant_with_selector_field_integer_unsigned_option * |
1804 | bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const( | |
02b61fe0 | 1805 | const struct bt_field_class *fc, const char *name) |
939190b3 | 1806 | { |
fa6cfec3 | 1807 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
02b61fe0 | 1808 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
8bc207af | 1809 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD, |
02b61fe0 PP |
1810 | "Field class"); |
1811 | return (const void *) | |
1812 | borrow_named_field_class_from_container_field_class_by_name( | |
1813 | (void *) fc, name); | |
939190b3 PP |
1814 | } |
1815 | ||
8bc207af PP |
1816 | const struct bt_field_class_variant_with_selector_field_integer_unsigned_option * |
1817 | bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const( | |
e24f271a | 1818 | const struct bt_field_class *fc, uint64_t index) |
939190b3 | 1819 | { |
fa6cfec3 | 1820 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
02b61fe0 | 1821 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
8bc207af | 1822 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD, |
02b61fe0 | 1823 | "Field class"); |
e24f271a PP |
1824 | return (const void *) |
1825 | borrow_named_field_class_from_container_field_class_at_index( | |
1826 | (void *) fc, index); | |
909d0a89 PP |
1827 | } |
1828 | ||
8bc207af PP |
1829 | const struct bt_field_class_variant_with_selector_field_integer_signed_option * |
1830 | bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const( | |
02b61fe0 | 1831 | const struct bt_field_class *fc, const char *name) |
909d0a89 | 1832 | { |
fa6cfec3 | 1833 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
02b61fe0 | 1834 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
8bc207af | 1835 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, |
02b61fe0 PP |
1836 | "Field class"); |
1837 | return (const void *) | |
1838 | borrow_named_field_class_from_container_field_class_by_name( | |
1839 | (void *) fc, name); | |
1840 | } | |
1841 | ||
8bc207af PP |
1842 | const struct bt_field_class_variant_with_selector_field_integer_signed_option * |
1843 | bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const( | |
02b61fe0 PP |
1844 | const struct bt_field_class *fc, uint64_t index) |
1845 | { | |
1846 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
1847 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, | |
8bc207af | 1848 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, |
02b61fe0 PP |
1849 | "Field class"); |
1850 | return (const void *) | |
e24f271a PP |
1851 | borrow_named_field_class_from_container_field_class_at_index( |
1852 | (void *) fc, index); | |
1853 | } | |
1854 | ||
1855 | const char *bt_field_class_variant_option_get_name( | |
1856 | const struct bt_field_class_variant_option *option) | |
1857 | { | |
1858 | const struct bt_named_field_class *named_fc = (const void *) option; | |
1859 | ||
fa6cfec3 | 1860 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
e24f271a PP |
1861 | return named_fc->name->str; |
1862 | } | |
1863 | ||
1864 | const struct bt_field_class * | |
1865 | bt_field_class_variant_option_borrow_field_class_const( | |
1866 | const struct bt_field_class_variant_option *option) | |
1867 | { | |
1868 | const struct bt_named_field_class *named_fc = (const void *) option; | |
1869 | ||
fa6cfec3 | 1870 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
e24f271a PP |
1871 | return named_fc->fc; |
1872 | } | |
1873 | ||
87040955 FD |
1874 | struct bt_field_class * |
1875 | bt_field_class_variant_option_borrow_field_class( | |
1876 | struct bt_field_class_variant_option *option) | |
1877 | { | |
1878 | struct bt_named_field_class *named_fc = (void *) option; | |
1879 | ||
1880 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); | |
1881 | return named_fc->fc; | |
1882 | } | |
1883 | ||
02b61fe0 | 1884 | const struct bt_integer_range_set_unsigned * |
8bc207af PP |
1885 | bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const( |
1886 | const struct bt_field_class_variant_with_selector_field_integer_unsigned_option *option) | |
e24f271a | 1887 | { |
8bc207af | 1888 | const struct bt_field_class_variant_with_selector_field_option *opt = |
02b61fe0 | 1889 | (const void *) option; |
e24f271a | 1890 | |
fa6cfec3 | 1891 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
02b61fe0 PP |
1892 | return (const void *) opt->range_set; |
1893 | } | |
1894 | ||
1895 | const struct bt_integer_range_set_signed * | |
8bc207af PP |
1896 | bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const( |
1897 | const struct bt_field_class_variant_with_selector_field_integer_signed_option *option) | |
02b61fe0 | 1898 | { |
8bc207af | 1899 | const struct bt_field_class_variant_with_selector_field_option *opt = |
02b61fe0 PP |
1900 | (const void *) option; |
1901 | ||
1902 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); | |
1903 | return (const void *) opt->range_set; | |
939190b3 PP |
1904 | } |
1905 | ||
78cf9df6 | 1906 | const struct bt_field_path * |
8bc207af | 1907 | bt_field_class_variant_with_selector_field_borrow_selector_field_path_const( |
78cf9df6 | 1908 | const struct bt_field_class *fc) |
9e550e5f | 1909 | { |
8bc207af | 1910 | const struct bt_field_class_variant_with_selector_field *var_fc = |
02b61fe0 | 1911 | (const void *) fc; |
939190b3 | 1912 | |
fa6cfec3 | 1913 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
02b61fe0 | 1914 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc, "Field class"); |
939190b3 PP |
1915 | return var_fc->selector_field_path; |
1916 | } | |
1917 | ||
1918 | static | |
af90138b | 1919 | int init_array_field_class(struct bt_field_class_array *fc, |
af0c18e3 | 1920 | enum bt_field_class_type type, bt_object_release_func release_func, |
939190b3 PP |
1921 | struct bt_field_class *element_fc) |
1922 | { | |
af90138b PP |
1923 | int ret; |
1924 | ||
939190b3 | 1925 | BT_ASSERT(element_fc); |
af90138b PP |
1926 | ret = init_field_class((void *) fc, type, release_func); |
1927 | if (ret) { | |
1928 | goto end; | |
1929 | } | |
1930 | ||
4b70020d | 1931 | fc->element_fc = element_fc; |
864aa43f | 1932 | bt_object_get_ref_no_null_check(fc->element_fc); |
939190b3 | 1933 | bt_field_class_freeze(element_fc); |
af90138b PP |
1934 | |
1935 | end: | |
1936 | return ret; | |
939190b3 PP |
1937 | } |
1938 | ||
1939 | static | |
1940 | void finalize_array_field_class(struct bt_field_class_array *array_fc) | |
1941 | { | |
1942 | BT_ASSERT(array_fc); | |
9e550e5f | 1943 | BT_LOGD_STR("Putting element field class."); |
af90138b | 1944 | finalize_field_class((void *) array_fc); |
1248f5ea | 1945 | BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc); |
939190b3 PP |
1946 | } |
1947 | ||
1948 | static | |
1949 | void destroy_static_array_field_class(struct bt_object *obj) | |
1950 | { | |
1951 | BT_ASSERT(obj); | |
9e550e5f | 1952 | BT_LIB_LOGD("Destroying static array field class object: %!+F", obj); |
939190b3 PP |
1953 | finalize_array_field_class((void *) obj); |
1954 | g_free(obj); | |
1955 | } | |
1956 | ||
78cf9df6 | 1957 | struct bt_field_class * |
60bbfc7c | 1958 | bt_field_class_array_static_create(bt_trace_class *trace_class, |
b7396828 | 1959 | struct bt_field_class *element_fc, uint64_t length) |
939190b3 | 1960 | { |
60bbfc7c | 1961 | struct bt_field_class_array_static *array_fc = NULL; |
939190b3 | 1962 | |
7c7324d3 | 1963 | BT_ASSERT_PRE_NO_ERROR(); |
b7396828 | 1964 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
9e550e5f PP |
1965 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1966 | BT_LOGD_STR("Creating default static array field class object."); | |
60bbfc7c | 1967 | array_fc = g_new0(struct bt_field_class_array_static, 1); |
939190b3 | 1968 | if (!array_fc) { |
a8f90e5d PP |
1969 | BT_LIB_LOGE_APPEND_CAUSE( |
1970 | "Failed to allocate one static array field class."); | |
939190b3 PP |
1971 | goto error; |
1972 | } | |
1973 | ||
af90138b PP |
1974 | if (init_array_field_class((void *) array_fc, |
1975 | BT_FIELD_CLASS_TYPE_STATIC_ARRAY, | |
1976 | destroy_static_array_field_class, element_fc)) { | |
1977 | goto error; | |
1978 | } | |
1979 | ||
939190b3 | 1980 | array_fc->length = length; |
9e550e5f | 1981 | BT_LIB_LOGD("Created static array field class object: %!+F", array_fc); |
939190b3 PP |
1982 | goto end; |
1983 | ||
1984 | error: | |
8138bfe1 | 1985 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
939190b3 PP |
1986 | |
1987 | end: | |
1988 | return (void *) array_fc; | |
1989 | } | |
1990 | ||
78cf9df6 PP |
1991 | const struct bt_field_class * |
1992 | bt_field_class_array_borrow_element_field_class_const( | |
1993 | const struct bt_field_class *fc) | |
939190b3 | 1994 | { |
78cf9df6 | 1995 | const struct bt_field_class_array *array_fc = (const void *) fc; |
939190b3 | 1996 | |
fa6cfec3 PP |
1997 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1998 | BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class"); | |
939190b3 PP |
1999 | return array_fc->element_fc; |
2000 | } | |
2001 | ||
909d0a89 PP |
2002 | struct bt_field_class * |
2003 | bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc) | |
2004 | { | |
2005 | struct bt_field_class_array *array_fc = (void *) fc; | |
2006 | ||
fa6cfec3 PP |
2007 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
2008 | BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class"); | |
909d0a89 PP |
2009 | return array_fc->element_fc; |
2010 | } | |
2011 | ||
60bbfc7c | 2012 | uint64_t bt_field_class_array_static_get_length(const struct bt_field_class *fc) |
9e550e5f | 2013 | { |
60bbfc7c | 2014 | const struct bt_field_class_array_static *array_fc = (const void *) fc; |
939190b3 | 2015 | |
fa6cfec3 PP |
2016 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
2017 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, | |
939190b3 PP |
2018 | "Field class"); |
2019 | return (uint64_t) array_fc->length; | |
2020 | } | |
2021 | ||
2022 | static | |
2023 | void destroy_dynamic_array_field_class(struct bt_object *obj) | |
2024 | { | |
60bbfc7c | 2025 | struct bt_field_class_array_dynamic *fc = (void *) obj; |
939190b3 PP |
2026 | |
2027 | BT_ASSERT(fc); | |
9e550e5f | 2028 | BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc); |
939190b3 PP |
2029 | finalize_array_field_class((void *) fc); |
2030 | BT_LOGD_STR("Putting length field path."); | |
1248f5ea PP |
2031 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path); |
2032 | BT_LOGD_STR("Putting length field class."); | |
2033 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc); | |
939190b3 PP |
2034 | g_free(fc); |
2035 | } | |
2036 | ||
60bbfc7c | 2037 | struct bt_field_class *bt_field_class_array_dynamic_create( |
93d7d5d1 PP |
2038 | struct bt_trace_class *trace_class, |
2039 | struct bt_field_class *element_fc, | |
2040 | struct bt_field_class *length_fc) | |
939190b3 | 2041 | { |
60bbfc7c | 2042 | struct bt_field_class_array_dynamic *array_fc = NULL; |
939190b3 | 2043 | |
7c7324d3 | 2044 | BT_ASSERT_PRE_NO_ERROR(); |
b7396828 | 2045 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
9e550e5f PP |
2046 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
2047 | BT_LOGD_STR("Creating default dynamic array field class object."); | |
60bbfc7c | 2048 | array_fc = g_new0(struct bt_field_class_array_dynamic, 1); |
939190b3 | 2049 | if (!array_fc) { |
a8f90e5d PP |
2050 | BT_LIB_LOGE_APPEND_CAUSE( |
2051 | "Failed to allocate one dynamic array field class."); | |
939190b3 PP |
2052 | goto error; |
2053 | } | |
2054 | ||
af90138b | 2055 | if (init_array_field_class((void *) array_fc, |
b8ddb4f0 PP |
2056 | length_fc ? |
2057 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD : | |
2058 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD, | |
af90138b PP |
2059 | destroy_dynamic_array_field_class, element_fc)) { |
2060 | goto error; | |
2061 | } | |
93d7d5d1 PP |
2062 | |
2063 | if (length_fc) { | |
2064 | BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, | |
2065 | "Length field class"); | |
2066 | array_fc->length_fc = length_fc; | |
864aa43f | 2067 | bt_object_get_ref_no_null_check(array_fc->length_fc); |
93d7d5d1 PP |
2068 | bt_field_class_freeze(length_fc); |
2069 | } | |
2070 | ||
9e550e5f | 2071 | BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc); |
939190b3 PP |
2072 | goto end; |
2073 | ||
2074 | error: | |
8138bfe1 | 2075 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
939190b3 PP |
2076 | |
2077 | end: | |
2078 | return (void *) array_fc; | |
2079 | } | |
2080 | ||
78cf9df6 | 2081 | const struct bt_field_path * |
b8ddb4f0 | 2082 | bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const( |
78cf9df6 | 2083 | const struct bt_field_class *fc) |
939190b3 | 2084 | { |
60bbfc7c | 2085 | const struct bt_field_class_array_dynamic *seq_fc = (const void *) fc; |
939190b3 | 2086 | |
7c7324d3 | 2087 | BT_ASSERT_PRE_NO_ERROR(); |
fa6cfec3 | 2088 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
b8ddb4f0 PP |
2089 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
2090 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, | |
939190b3 PP |
2091 | "Field class"); |
2092 | return seq_fc->length_field_path; | |
2093 | } | |
2094 | ||
2095 | static | |
2096 | void destroy_string_field_class(struct bt_object *obj) | |
2097 | { | |
2098 | BT_ASSERT(obj); | |
9e550e5f | 2099 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
af90138b | 2100 | finalize_field_class((void *) obj); |
939190b3 PP |
2101 | g_free(obj); |
2102 | } | |
2103 | ||
b7396828 | 2104 | struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class) |
939190b3 PP |
2105 | { |
2106 | struct bt_field_class_string *string_fc = NULL; | |
2107 | ||
7c7324d3 | 2108 | BT_ASSERT_PRE_NO_ERROR(); |
b7396828 | 2109 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
9e550e5f | 2110 | BT_LOGD_STR("Creating default string field class object."); |
939190b3 PP |
2111 | string_fc = g_new0(struct bt_field_class_string, 1); |
2112 | if (!string_fc) { | |
a8f90e5d PP |
2113 | BT_LIB_LOGE_APPEND_CAUSE( |
2114 | "Failed to allocate one string field class."); | |
939190b3 PP |
2115 | goto error; |
2116 | } | |
2117 | ||
af90138b PP |
2118 | if (init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING, |
2119 | destroy_string_field_class)) { | |
2120 | goto error; | |
2121 | } | |
2122 | ||
9e550e5f | 2123 | BT_LIB_LOGD("Created string field class object: %!+F", string_fc); |
939190b3 PP |
2124 | goto end; |
2125 | ||
2126 | error: | |
8138bfe1 | 2127 | BT_OBJECT_PUT_REF_AND_RESET(string_fc); |
939190b3 PP |
2128 | |
2129 | end: | |
2130 | return (void *) string_fc; | |
2131 | } | |
2132 | ||
2133 | BT_HIDDEN | |
e24f271a | 2134 | void _bt_field_class_freeze(const struct bt_field_class *c_fc) |
939190b3 | 2135 | { |
e24f271a PP |
2136 | struct bt_field_class *fc = (void *) c_fc; |
2137 | ||
939190b3 PP |
2138 | /* |
2139 | * Element/member/option field classes are frozen when added to | |
2140 | * their owner. | |
2141 | */ | |
2142 | BT_ASSERT(fc); | |
af90138b | 2143 | bt_value_freeze(fc->user_attributes); |
e24f271a PP |
2144 | fc->frozen = true; |
2145 | ||
94847783 PP |
2146 | if (fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE || |
2147 | bt_field_class_type_is(fc->type, | |
2148 | BT_FIELD_CLASS_TYPE_VARIANT)) { | |
e24f271a PP |
2149 | struct bt_field_class_named_field_class_container *container_fc = |
2150 | (void *) fc; | |
2151 | uint64_t i; | |
2152 | ||
2153 | for (i = 0; i < container_fc->named_fcs->len; i++) { | |
02b61fe0 PP |
2154 | bt_named_field_class_freeze( |
2155 | container_fc->named_fcs->pdata[i]); | |
e24f271a | 2156 | } |
e24f271a PP |
2157 | } |
2158 | } | |
2159 | ||
2160 | BT_HIDDEN | |
2161 | void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc) | |
2162 | { | |
2163 | BT_ASSERT(named_fc); | |
af90138b PP |
2164 | BT_ASSERT(named_fc->fc->frozen); |
2165 | BT_LIB_LOGD("Freezing named field class's user attributes: %!+v", | |
2166 | named_fc->user_attributes); | |
2167 | bt_value_freeze(named_fc->user_attributes); | |
e24f271a | 2168 | ((struct bt_named_field_class *) named_fc)->frozen = true; |
939190b3 PP |
2169 | } |
2170 | ||
2171 | BT_HIDDEN | |
fa6cfec3 | 2172 | void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc) |
939190b3 | 2173 | { |
78cf9df6 PP |
2174 | struct bt_field_class *fc = (void *) c_fc; |
2175 | ||
939190b3 | 2176 | BT_ASSERT(fc); |
10b7a2e4 | 2177 | BT_ASSERT_PRE(!fc->part_of_trace_class, |
939190b3 | 2178 | "Field class is already part of a trace: %!+F", fc); |
10b7a2e4 | 2179 | fc->part_of_trace_class = true; |
939190b3 | 2180 | |
94847783 PP |
2181 | if (fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE || |
2182 | bt_field_class_type_is(fc->type, | |
2183 | BT_FIELD_CLASS_TYPE_VARIANT)) { | |
939190b3 PP |
2184 | struct bt_field_class_named_field_class_container *container_fc = |
2185 | (void *) fc; | |
2186 | uint64_t i; | |
2187 | ||
2188 | for (i = 0; i < container_fc->named_fcs->len; i++) { | |
2189 | struct bt_named_field_class *named_fc = | |
02b61fe0 | 2190 | container_fc->named_fcs->pdata[i]; |
939190b3 | 2191 | |
10b7a2e4 | 2192 | bt_field_class_make_part_of_trace_class(named_fc->fc); |
939190b3 | 2193 | } |
94847783 PP |
2194 | } else if (bt_field_class_type_is(fc->type, |
2195 | BT_FIELD_CLASS_TYPE_ARRAY)) { | |
939190b3 PP |
2196 | struct bt_field_class_array *array_fc = (void *) fc; |
2197 | ||
10b7a2e4 | 2198 | bt_field_class_make_part_of_trace_class(array_fc->element_fc); |
939190b3 PP |
2199 | } |
2200 | } | |
8c6884d9 | 2201 | |
af90138b PP |
2202 | const struct bt_value *bt_field_class_borrow_user_attributes_const( |
2203 | const struct bt_field_class *fc) | |
2204 | { | |
2205 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
2206 | return fc->user_attributes; | |
2207 | } | |
2208 | ||
2209 | struct bt_value *bt_field_class_borrow_user_attributes( | |
2210 | struct bt_field_class *field_class) | |
2211 | { | |
2212 | return (void *) bt_field_class_borrow_user_attributes_const( | |
2213 | (void *) field_class); | |
2214 | } | |
2215 | ||
2216 | ||
2217 | void bt_field_class_set_user_attributes( | |
2218 | struct bt_field_class *fc, | |
2219 | const struct bt_value *user_attributes) | |
2220 | { | |
2221 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
2222 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
2223 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
2224 | "User attributes object is not a map value object."); | |
2225 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); | |
864aa43f | 2226 | bt_object_put_ref_no_null_check(fc->user_attributes); |
af90138b | 2227 | fc->user_attributes = (void *) user_attributes; |
864aa43f | 2228 | bt_object_get_ref_no_null_check(fc->user_attributes); |
af90138b PP |
2229 | } |
2230 | ||
2231 | static | |
2232 | const struct bt_value *bt_named_field_class_borrow_user_attributes_const( | |
2233 | const struct bt_named_field_class *named_fc) | |
2234 | { | |
2235 | return named_fc->user_attributes; | |
2236 | } | |
2237 | ||
2238 | static | |
2239 | void bt_named_field_class_set_user_attributes( | |
2240 | struct bt_named_field_class *named_fc, | |
2241 | const struct bt_value *user_attributes) | |
2242 | { | |
2243 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
2244 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
2245 | "User attributes object is not a map value object."); | |
2246 | BT_ASSERT_PRE_DEV_HOT(named_fc, | |
2247 | "Structure field class member or variant field class option", | |
2248 | "."); | |
864aa43f | 2249 | bt_object_put_ref_no_null_check(named_fc->user_attributes); |
af90138b | 2250 | named_fc->user_attributes = (void *) user_attributes; |
864aa43f | 2251 | bt_object_get_ref_no_null_check(named_fc->user_attributes); |
af90138b PP |
2252 | } |
2253 | ||
2254 | const struct bt_value * | |
2255 | bt_field_class_structure_member_borrow_user_attributes_const( | |
2256 | const struct bt_field_class_structure_member *member) | |
2257 | { | |
2258 | BT_ASSERT_PRE_NON_NULL(member, "Structure field class member"); | |
2259 | return bt_named_field_class_borrow_user_attributes_const( | |
2260 | (const void *) member); | |
2261 | } | |
2262 | ||
2263 | struct bt_value * | |
2264 | bt_field_class_structure_member_borrow_user_attributes( | |
2265 | struct bt_field_class_structure_member *member) | |
2266 | { | |
2267 | BT_ASSERT_PRE_NON_NULL(member, "Structure field class member"); | |
2268 | return (void *) bt_named_field_class_borrow_user_attributes_const( | |
2269 | (void *) member); | |
2270 | } | |
2271 | ||
2272 | void bt_field_class_structure_member_set_user_attributes( | |
2273 | struct bt_field_class_structure_member *member, | |
2274 | const struct bt_value *user_attributes) | |
2275 | { | |
2276 | BT_ASSERT_PRE_NON_NULL(member, "Structure field class member"); | |
2277 | bt_named_field_class_set_user_attributes((void *) member, | |
2278 | user_attributes); | |
2279 | } | |
2280 | ||
2281 | const struct bt_value *bt_field_class_variant_option_borrow_user_attributes_const( | |
2282 | const struct bt_field_class_variant_option *option) | |
2283 | { | |
2284 | BT_ASSERT_PRE_NON_NULL(option, "Variant field class option"); | |
2285 | return bt_named_field_class_borrow_user_attributes_const( | |
2286 | (const void *) option); | |
2287 | } | |
2288 | ||
2289 | struct bt_value *bt_field_class_variant_option_borrow_user_attributes( | |
2290 | struct bt_field_class_variant_option *option) | |
2291 | { | |
2292 | BT_ASSERT_PRE_NON_NULL(option, "Variant field class option"); | |
2293 | return (void *) bt_named_field_class_borrow_user_attributes_const( | |
2294 | (void *) option); | |
2295 | } | |
2296 | ||
2297 | void bt_field_class_variant_option_set_user_attributes( | |
2298 | struct bt_field_class_variant_option *option, | |
2299 | const struct bt_value *user_attributes) | |
2300 | { | |
2301 | BT_ASSERT_PRE_NON_NULL(option, "Variant field class option"); | |
2302 | bt_named_field_class_set_user_attributes((void *) option, | |
2303 | user_attributes); | |
2304 | } | |
2305 | ||
8c6884d9 PP |
2306 | void bt_field_class_get_ref(const struct bt_field_class *field_class) |
2307 | { | |
2308 | bt_object_get_ref(field_class); | |
2309 | } | |
2310 | ||
2311 | void bt_field_class_put_ref(const struct bt_field_class *field_class) | |
2312 | { | |
2313 | bt_object_put_ref(field_class); | |
2314 | } |