Commit | Line | Data |
---|---|---|
5cd6d0e5 | 1 | /* |
5cd6d0e5 PP |
2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | #define BT_LOG_TAG "FIELD-CLASSES" | |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
28 | #include <babeltrace/assert-pre-internal.h> | |
e5be10ef | 29 | #include <babeltrace/trace-ir/private-field-classes.h> |
5cd6d0e5 PP |
30 | #include <babeltrace/trace-ir/field-classes-internal.h> |
31 | #include <babeltrace/trace-ir/field-path-internal.h> | |
32 | #include <babeltrace/trace-ir/fields-internal.h> | |
e5be10ef | 33 | #include <babeltrace/trace-ir/private-fields.h> |
5cd6d0e5 PP |
34 | #include <babeltrace/trace-ir/fields.h> |
35 | #include <babeltrace/trace-ir/utils-internal.h> | |
65300d60 | 36 | #include <babeltrace/object.h> |
5cd6d0e5 PP |
37 | #include <babeltrace/trace-ir/clock-class.h> |
38 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
39 | #include <babeltrace/object-internal.h> | |
65300d60 | 40 | #include <babeltrace/object.h> |
5cd6d0e5 PP |
41 | #include <babeltrace/compiler-internal.h> |
42 | #include <babeltrace/endian-internal.h> | |
43 | #include <babeltrace/assert-internal.h> | |
44 | #include <babeltrace/compat/glib-internal.h> | |
45 | #include <float.h> | |
46 | #include <inttypes.h> | |
47 | #include <stdlib.h> | |
48 | ||
864cad70 | 49 | enum bt_field_class_type bt_field_class_get_type(struct bt_field_class *fc) |
5cd6d0e5 PP |
50 | { |
51 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 52 | return fc->type; |
5cd6d0e5 PP |
53 | } |
54 | ||
55 | static | |
864cad70 | 56 | void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, |
5cd6d0e5 PP |
57 | bt_object_release_func release_func) |
58 | { | |
59 | BT_ASSERT(fc); | |
864cad70 | 60 | BT_ASSERT(bt_field_class_has_known_type(fc)); |
5cd6d0e5 PP |
61 | BT_ASSERT(release_func); |
62 | bt_object_init_shared(&fc->base, release_func); | |
864cad70 | 63 | fc->type = type; |
5cd6d0e5 PP |
64 | } |
65 | ||
66 | static | |
864cad70 PP |
67 | void init_integer_field_class(struct bt_field_class_integer *fc, |
68 | enum bt_field_class_type type, | |
5cd6d0e5 PP |
69 | bt_object_release_func release_func) |
70 | { | |
864cad70 | 71 | init_field_class((void *) fc, type, release_func); |
5cd6d0e5 PP |
72 | fc->range = 64; |
73 | fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; | |
74 | } | |
75 | ||
76 | static | |
77 | void destroy_integer_field_class(struct bt_object *obj) | |
78 | { | |
79 | BT_ASSERT(obj); | |
e5be10ef | 80 | BT_LIB_LOGD("Destroying integer field class object: %!+F", obj); |
5cd6d0e5 PP |
81 | g_free(obj); |
82 | } | |
83 | ||
84 | static inline | |
864cad70 | 85 | struct bt_field_class *create_integer_field_class(enum bt_field_class_type type) |
5cd6d0e5 PP |
86 | { |
87 | struct bt_field_class_integer *int_fc = NULL; | |
88 | ||
e5be10ef | 89 | BT_LOGD("Creating default integer field class object: type=%s", |
864cad70 | 90 | bt_common_field_class_type_string(type)); |
5cd6d0e5 PP |
91 | int_fc = g_new0(struct bt_field_class_integer, 1); |
92 | if (!int_fc) { | |
e5be10ef | 93 | BT_LOGE_STR("Failed to allocate one integer field class."); |
5cd6d0e5 PP |
94 | goto error; |
95 | } | |
96 | ||
864cad70 | 97 | init_integer_field_class(int_fc, type, destroy_integer_field_class); |
e5be10ef | 98 | BT_LIB_LOGD("Created integer field class object: %!+F", int_fc); |
5cd6d0e5 PP |
99 | goto end; |
100 | ||
101 | error: | |
65300d60 | 102 | BT_OBJECT_PUT_REF_AND_RESET(int_fc); |
5cd6d0e5 PP |
103 | |
104 | end: | |
105 | return (void *) int_fc; | |
106 | } | |
107 | ||
e5be10ef PP |
108 | struct bt_private_field_class * |
109 | bt_private_field_class_unsigned_integer_create(void) | |
5cd6d0e5 | 110 | { |
e5be10ef PP |
111 | return (void *) create_integer_field_class( |
112 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER); | |
5cd6d0e5 PP |
113 | } |
114 | ||
e5be10ef | 115 | struct bt_private_field_class *bt_private_field_class_signed_integer_create(void) |
5cd6d0e5 | 116 | { |
e5be10ef PP |
117 | return (void *) create_integer_field_class( |
118 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); | |
5cd6d0e5 PP |
119 | } |
120 | ||
121 | uint64_t bt_field_class_integer_get_field_value_range( | |
122 | struct bt_field_class *fc) | |
123 | { | |
124 | struct bt_field_class_integer *int_fc = (void *) fc; | |
125 | ||
126 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
127 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
128 | return int_fc->range; | |
129 | } | |
130 | ||
131 | BT_ASSERT_PRE_FUNC | |
132 | static | |
133 | bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc, | |
134 | uint64_t size) | |
135 | { | |
136 | // TODO | |
137 | return true; | |
138 | } | |
139 | ||
140e6d94 | 140 | void bt_private_field_class_integer_set_field_value_range( |
e5be10ef | 141 | struct bt_private_field_class *priv_fc, uint64_t size) |
5cd6d0e5 | 142 | { |
e5be10ef | 143 | struct bt_field_class *fc = (void *) priv_fc; |
5cd6d0e5 PP |
144 | struct bt_field_class_integer *int_fc = (void *) fc; |
145 | ||
146 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
147 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
148 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); | |
149 | BT_ASSERT_PRE(size <= 64, | |
e5be10ef | 150 | "Unsupported size for integer field class's field value range " |
5cd6d0e5 | 151 | "(maximum is 64): size=%" PRIu64, size); |
864cad70 PP |
152 | BT_ASSERT_PRE( |
153 | int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || | |
154 | int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || | |
5cd6d0e5 | 155 | size_is_valid_for_enumeration_field_class(fc, size), |
e5be10ef | 156 | "Invalid field value range for enumeration field class: " |
5cd6d0e5 PP |
157 | "at least one of the current mapping ranges contains values " |
158 | "which are outside this range: %!+F, size=%" PRIu64, fc, size); | |
159 | int_fc->range = size; | |
e5be10ef | 160 | BT_LIB_LOGV("Set integer field class's field value range: %!+F", fc); |
5cd6d0e5 PP |
161 | } |
162 | ||
163 | enum bt_field_class_integer_preferred_display_base | |
164 | bt_field_class_integer_get_preferred_display_base(struct bt_field_class *fc) | |
165 | { | |
166 | struct bt_field_class_integer *int_fc = (void *) fc; | |
167 | ||
168 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
169 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
170 | return int_fc->base; | |
171 | } | |
172 | ||
140e6d94 | 173 | void bt_private_field_class_integer_set_preferred_display_base( |
e5be10ef | 174 | struct bt_private_field_class *priv_fc, |
5cd6d0e5 PP |
175 | enum bt_field_class_integer_preferred_display_base base) |
176 | { | |
e5be10ef | 177 | struct bt_field_class *fc = (void *) priv_fc; |
5cd6d0e5 PP |
178 | struct bt_field_class_integer *int_fc = (void *) fc; |
179 | ||
180 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
181 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
182 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); | |
183 | int_fc->base = base; | |
e5be10ef | 184 | BT_LIB_LOGV("Set integer field class's preferred display base: %!+F", fc); |
5cd6d0e5 PP |
185 | } |
186 | ||
187 | static | |
188 | void finalize_enumeration_field_class_mapping( | |
189 | struct bt_field_class_enumeration_mapping *mapping) | |
190 | { | |
191 | BT_ASSERT(mapping); | |
192 | ||
193 | if (mapping->label) { | |
194 | g_string_free(mapping->label, TRUE); | |
195 | } | |
196 | ||
197 | if (mapping->ranges) { | |
198 | g_array_free(mapping->ranges, TRUE); | |
199 | } | |
200 | } | |
201 | ||
202 | static | |
203 | void destroy_enumeration_field_class(struct bt_object *obj) | |
204 | { | |
205 | struct bt_field_class_enumeration *fc = (void *) obj; | |
206 | ||
207 | BT_ASSERT(fc); | |
e5be10ef | 208 | BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc); |
5cd6d0e5 PP |
209 | |
210 | if (fc->mappings) { | |
211 | uint64_t i; | |
212 | ||
213 | for (i = 0; i < fc->mappings->len; i++) { | |
214 | finalize_enumeration_field_class_mapping( | |
215 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i)); | |
216 | } | |
217 | ||
218 | g_array_free(fc->mappings, TRUE); | |
219 | } | |
220 | ||
221 | if (fc->label_buf) { | |
222 | g_ptr_array_free(fc->label_buf, TRUE); | |
223 | } | |
224 | ||
225 | g_free(fc); | |
226 | } | |
227 | ||
228 | static | |
864cad70 | 229 | struct bt_field_class *create_enumeration_field_class(enum bt_field_class_type type) |
5cd6d0e5 PP |
230 | { |
231 | struct bt_field_class_enumeration *enum_fc = NULL; | |
232 | ||
e5be10ef | 233 | BT_LOGD("Creating default enumeration field class object: type=%s", |
864cad70 | 234 | bt_common_field_class_type_string(type)); |
5cd6d0e5 PP |
235 | enum_fc = g_new0(struct bt_field_class_enumeration, 1); |
236 | if (!enum_fc) { | |
e5be10ef | 237 | BT_LOGE_STR("Failed to allocate one enumeration field class."); |
5cd6d0e5 PP |
238 | goto error; |
239 | } | |
240 | ||
864cad70 | 241 | init_integer_field_class((void *) enum_fc, type, |
5cd6d0e5 PP |
242 | destroy_enumeration_field_class); |
243 | enum_fc->mappings = g_array_new(FALSE, TRUE, | |
244 | sizeof(struct bt_field_class_enumeration_mapping)); | |
245 | if (!enum_fc->mappings) { | |
246 | BT_LOGE_STR("Failed to allocate a GArray."); | |
247 | goto error; | |
248 | } | |
249 | ||
250 | enum_fc->label_buf = g_ptr_array_new(); | |
251 | if (!enum_fc->label_buf) { | |
252 | BT_LOGE_STR("Failed to allocate a GArray."); | |
253 | goto error; | |
254 | } | |
255 | ||
e5be10ef | 256 | BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc); |
5cd6d0e5 PP |
257 | goto end; |
258 | ||
259 | error: | |
65300d60 | 260 | BT_OBJECT_PUT_REF_AND_RESET(enum_fc); |
5cd6d0e5 PP |
261 | |
262 | end: | |
263 | return (void *) enum_fc; | |
264 | } | |
265 | ||
e5be10ef PP |
266 | struct bt_private_field_class * |
267 | bt_private_field_class_unsigned_enumeration_create(void) | |
5cd6d0e5 | 268 | { |
e5be10ef | 269 | return (void *) create_enumeration_field_class( |
864cad70 | 270 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
5cd6d0e5 PP |
271 | } |
272 | ||
e5be10ef PP |
273 | struct bt_private_field_class * |
274 | bt_private_field_class_signed_enumeration_create(void) | |
5cd6d0e5 | 275 | { |
e5be10ef | 276 | return (void *) create_enumeration_field_class( |
864cad70 | 277 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
5cd6d0e5 PP |
278 | } |
279 | ||
280 | uint64_t bt_field_class_enumeration_get_mapping_count(struct bt_field_class *fc) | |
281 | { | |
282 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
283 | ||
284 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
285 | BT_ASSERT_PRE_FC_IS_ENUM(fc, "Field class"); | |
286 | return (uint64_t) enum_fc->mappings->len; | |
287 | } | |
288 | ||
289 | void bt_field_class_unsigned_enumeration_borrow_mapping_by_index( | |
290 | struct bt_field_class *fc, uint64_t index, | |
291 | const char **name, | |
292 | struct bt_field_class_unsigned_enumeration_mapping_ranges **ranges) | |
293 | { | |
294 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
295 | struct bt_field_class_enumeration_mapping *mapping; | |
296 | ||
297 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
298 | BT_ASSERT_PRE_NON_NULL(name, "Name (output)"); | |
299 | BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)"); | |
300 | BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len); | |
864cad70 | 301 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 PP |
302 | "Field class"); |
303 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); | |
304 | *name = mapping->label->str; | |
305 | *ranges = (void *) mapping; | |
306 | } | |
307 | ||
308 | void bt_field_class_signed_enumeration_borrow_mapping_by_index( | |
309 | struct bt_field_class *fc, uint64_t index, | |
310 | const char **name, | |
311 | struct bt_field_class_signed_enumeration_mapping_ranges **ranges) | |
312 | { | |
313 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
314 | struct bt_field_class_enumeration_mapping *mapping; | |
315 | ||
316 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
317 | BT_ASSERT_PRE_NON_NULL(name, "Name (output)"); | |
318 | BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)"); | |
319 | BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len); | |
864cad70 | 320 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 PP |
321 | "Field class"); |
322 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); | |
323 | *name = mapping->label->str; | |
324 | *ranges = (void *) mapping; | |
325 | } | |
326 | ||
327 | static inline | |
328 | uint64_t get_enumeration_field_class_mapping_range_count( | |
329 | struct bt_field_class_enumeration_mapping *mapping) | |
330 | { | |
331 | BT_ASSERT_PRE_NON_NULL(mapping, "Ranges"); | |
332 | return (uint64_t) mapping->ranges->len; | |
333 | } | |
334 | ||
335 | uint64_t bt_field_class_unsigned_enumeration_mapping_ranges_get_range_count( | |
336 | struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges) | |
337 | { | |
338 | return get_enumeration_field_class_mapping_range_count((void *) ranges); | |
339 | } | |
340 | ||
341 | uint64_t bt_field_class_signed_enumeration_mapping_ranges_get_range_count( | |
342 | struct bt_field_class_signed_enumeration_mapping_ranges *ranges) | |
343 | { | |
344 | return get_enumeration_field_class_mapping_range_count((void *) ranges); | |
345 | } | |
346 | ||
347 | static inline | |
348 | void get_enumeration_field_class_mapping_range_at_index( | |
349 | struct bt_field_class_enumeration_mapping *mapping, | |
350 | uint64_t index, uint64_t *lower, uint64_t *upper) | |
351 | { | |
352 | struct bt_field_class_enumeration_mapping_range *range; | |
353 | ||
354 | BT_ASSERT_PRE_NON_NULL(mapping, "Ranges"); | |
355 | BT_ASSERT_PRE_NON_NULL(lower, "Range's lower (output)"); | |
356 | BT_ASSERT_PRE_NON_NULL(upper, "Range's upper (output)"); | |
357 | BT_ASSERT_PRE_VALID_INDEX(index, mapping->ranges->len); | |
358 | range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, index); | |
359 | *lower = range->lower.u; | |
360 | *upper = range->upper.u; | |
361 | } | |
362 | ||
363 | void bt_field_class_unsigned_enumeration_mapping_ranges_get_range_by_index( | |
364 | struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges, | |
365 | uint64_t index, uint64_t *lower, uint64_t *upper) | |
366 | { | |
367 | get_enumeration_field_class_mapping_range_at_index((void *) ranges, | |
368 | index, lower, upper); | |
369 | } | |
370 | ||
371 | void bt_field_class_signed_enumeration_mapping_ranges_get_range_by_index( | |
372 | struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges, | |
373 | uint64_t index, int64_t *lower, int64_t *upper) | |
374 | { | |
375 | get_enumeration_field_class_mapping_range_at_index((void *) ranges, | |
376 | index, (uint64_t *) lower, (uint64_t *) upper); | |
377 | } | |
378 | ||
379 | ||
380 | ||
381 | int bt_field_class_unsigned_enumeration_get_mapping_labels_by_value( | |
382 | struct bt_field_class *fc, uint64_t value, | |
383 | bt_field_class_enumeration_mapping_label_array *label_array, | |
384 | uint64_t *count) | |
385 | { | |
386 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
387 | uint64_t i; | |
388 | ||
389 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
390 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
391 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); | |
864cad70 | 392 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 PP |
393 | "Field class"); |
394 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
395 | ||
396 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
397 | uint64_t j; | |
398 | struct bt_field_class_enumeration_mapping *mapping = | |
399 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); | |
400 | ||
401 | for (j = 0; j < mapping->ranges->len; j++) { | |
402 | struct bt_field_class_enumeration_mapping_range *range = | |
403 | BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX( | |
404 | mapping, j); | |
405 | ||
406 | if (value >= range->lower.u && | |
407 | value <= range->upper.u) { | |
408 | g_ptr_array_add(enum_fc->label_buf, | |
409 | mapping->label->str); | |
410 | break; | |
411 | } | |
412 | } | |
413 | } | |
414 | ||
415 | *label_array = (void *) enum_fc->label_buf->pdata; | |
416 | *count = (uint64_t) enum_fc->label_buf->len; | |
417 | return 0; | |
418 | } | |
419 | ||
420 | int bt_field_class_signed_enumeration_get_mapping_labels_by_value( | |
421 | struct bt_field_class *fc, int64_t value, | |
422 | bt_field_class_enumeration_mapping_label_array *label_array, | |
423 | uint64_t *count) | |
424 | { | |
425 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
426 | uint64_t i; | |
427 | ||
428 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
429 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
430 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); | |
864cad70 | 431 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 PP |
432 | "Field class"); |
433 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
434 | ||
435 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
436 | uint64_t j; | |
437 | struct bt_field_class_enumeration_mapping *mapping = | |
438 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); | |
439 | ||
440 | for (j = 0; j < mapping->ranges->len; j++) { | |
441 | struct bt_field_class_enumeration_mapping_range *range = | |
442 | BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX( | |
443 | mapping, j); | |
444 | ||
445 | if (value >= range->lower.i && | |
446 | value <= range->upper.i) { | |
447 | g_ptr_array_add(enum_fc->label_buf, | |
448 | mapping->label->str); | |
449 | break; | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | *label_array = (void *) enum_fc->label_buf->pdata; | |
455 | *count = (uint64_t) enum_fc->label_buf->len; | |
456 | return 0; | |
457 | } | |
458 | ||
459 | static inline | |
460 | int add_mapping_to_enumeration_field_class(struct bt_field_class *fc, | |
461 | const char *label, uint64_t lower, uint64_t upper) | |
462 | { | |
463 | int ret = 0; | |
464 | uint64_t i; | |
465 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
466 | struct bt_field_class_enumeration_mapping *mapping = NULL; | |
467 | struct bt_field_class_enumeration_mapping_range *range; | |
468 | ||
469 | BT_ASSERT(fc); | |
470 | BT_ASSERT_PRE_NON_NULL(label, "Label"); | |
471 | ||
472 | /* Find existing mapping identified by this label */ | |
473 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
474 | struct bt_field_class_enumeration_mapping *mapping_candidate = | |
475 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); | |
476 | ||
477 | if (strcmp(mapping_candidate->label->str, label) == 0) { | |
478 | mapping = mapping_candidate; | |
479 | break; | |
480 | } | |
481 | } | |
482 | ||
483 | if (!mapping) { | |
484 | /* Create new mapping for this label */ | |
485 | g_array_set_size(enum_fc->mappings, enum_fc->mappings->len + 1); | |
486 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, | |
487 | enum_fc->mappings->len - 1); | |
488 | mapping->ranges = g_array_new(FALSE, TRUE, | |
489 | sizeof(struct bt_field_class_enumeration_mapping_range)); | |
490 | if (!mapping->ranges) { | |
491 | finalize_enumeration_field_class_mapping(mapping); | |
492 | g_array_set_size(enum_fc->mappings, | |
493 | enum_fc->mappings->len - 1); | |
494 | ret = -1; | |
495 | goto end; | |
496 | } | |
497 | ||
498 | mapping->label = g_string_new(label); | |
499 | if (!mapping->label) { | |
500 | finalize_enumeration_field_class_mapping(mapping); | |
501 | g_array_set_size(enum_fc->mappings, | |
502 | enum_fc->mappings->len - 1); | |
503 | ret = -1; | |
504 | goto end; | |
505 | } | |
506 | } | |
507 | ||
508 | /* Add range */ | |
509 | BT_ASSERT(mapping); | |
510 | g_array_set_size(mapping->ranges, mapping->ranges->len + 1); | |
511 | range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, | |
512 | mapping->ranges->len - 1); | |
513 | range->lower.u = lower; | |
514 | range->upper.u = upper; | |
e5be10ef | 515 | BT_LIB_LOGV("Added mapping to enumeration field class: " |
5cd6d0e5 PP |
516 | "%![fc-]+F, label=\"%s\", lower-unsigned=%" PRIu64 ", " |
517 | "upper-unsigned=%" PRIu64, fc, label, lower, upper); | |
518 | ||
519 | end: | |
520 | return ret; | |
521 | } | |
522 | ||
e5be10ef PP |
523 | int bt_private_field_class_unsigned_enumeration_map_range( |
524 | struct bt_private_field_class *priv_fc, const char *label, | |
5cd6d0e5 PP |
525 | uint64_t range_lower, uint64_t range_upper) |
526 | { | |
e5be10ef | 527 | struct bt_field_class *fc = (void *) priv_fc; |
5cd6d0e5 PP |
528 | struct bt_field_class_enumeration *enum_fc = (void *) fc; |
529 | ||
530 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 531 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 PP |
532 | "Field class"); |
533 | BT_ASSERT_PRE(range_lower <= range_upper, | |
534 | "Range's upper bound is less than lower bound: " | |
535 | "upper=%" PRIu64 ", lower=%" PRIu64, | |
536 | range_lower, range_upper); | |
537 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range, | |
538 | range_lower), | |
e5be10ef | 539 | "Range's lower bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
540 | "%![fc-]+F, lower=%" PRIu64, fc, range_lower); |
541 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range, | |
542 | range_upper), | |
e5be10ef | 543 | "Range's upper bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
544 | "%![fc-]+F, upper=%" PRIu64, fc, range_upper); |
545 | return add_mapping_to_enumeration_field_class(fc, label, range_lower, | |
546 | range_upper); | |
547 | } | |
548 | ||
e5be10ef PP |
549 | int bt_private_field_class_signed_enumeration_map_range( |
550 | struct bt_private_field_class *priv_fc, const char *label, | |
5cd6d0e5 PP |
551 | int64_t range_lower, int64_t range_upper) |
552 | { | |
e5be10ef | 553 | struct bt_field_class *fc = (void *) priv_fc; |
5cd6d0e5 PP |
554 | struct bt_field_class_enumeration *enum_fc = (void *) fc; |
555 | ||
556 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 557 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 PP |
558 | "Field class"); |
559 | BT_ASSERT_PRE(range_lower <= range_upper, | |
560 | "Range's upper bound is less than lower bound: " | |
561 | "upper=%" PRId64 ", lower=%" PRId64, | |
562 | range_lower, range_upper); | |
563 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range, | |
564 | range_lower), | |
e5be10ef | 565 | "Range's lower bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
566 | "%![fc-]+F, lower=%" PRId64, fc, range_lower); |
567 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range, | |
568 | range_upper), | |
e5be10ef | 569 | "Range's upper bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
570 | "%![fc-]+F, upper=%" PRId64, fc, range_upper); |
571 | return add_mapping_to_enumeration_field_class(fc, label, range_lower, | |
572 | range_upper); | |
573 | } | |
574 | ||
575 | static | |
576 | void destroy_real_field_class(struct bt_object *obj) | |
577 | { | |
578 | BT_ASSERT(obj); | |
e5be10ef | 579 | BT_LIB_LOGD("Destroying real field class object: %!+F", obj); |
5cd6d0e5 PP |
580 | g_free(obj); |
581 | } | |
582 | ||
e5be10ef | 583 | struct bt_private_field_class *bt_private_field_class_real_create(void) |
5cd6d0e5 PP |
584 | { |
585 | struct bt_field_class_real *real_fc = NULL; | |
586 | ||
e5be10ef | 587 | BT_LOGD_STR("Creating default real field class object."); |
5cd6d0e5 PP |
588 | real_fc = g_new0(struct bt_field_class_real, 1); |
589 | if (!real_fc) { | |
e5be10ef | 590 | BT_LOGE_STR("Failed to allocate one real field class."); |
5cd6d0e5 PP |
591 | goto error; |
592 | } | |
593 | ||
864cad70 | 594 | init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL, |
5cd6d0e5 | 595 | destroy_real_field_class); |
e5be10ef | 596 | BT_LIB_LOGD("Created real field class object: %!+F", real_fc); |
5cd6d0e5 PP |
597 | goto end; |
598 | ||
599 | error: | |
65300d60 | 600 | BT_OBJECT_PUT_REF_AND_RESET(real_fc); |
5cd6d0e5 PP |
601 | |
602 | end: | |
603 | return (void *) real_fc; | |
604 | } | |
605 | ||
606 | bt_bool bt_field_class_real_is_single_precision(struct bt_field_class *fc) | |
607 | { | |
608 | struct bt_field_class_real *real_fc = (void *) fc; | |
609 | ||
610 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 611 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
5cd6d0e5 PP |
612 | return real_fc->is_single_precision; |
613 | } | |
614 | ||
140e6d94 | 615 | void bt_private_field_class_real_set_is_single_precision( |
e5be10ef | 616 | struct bt_private_field_class *priv_fc, |
5cd6d0e5 PP |
617 | bt_bool is_single_precision) |
618 | { | |
e5be10ef | 619 | struct bt_field_class *fc = (void *) priv_fc; |
5cd6d0e5 PP |
620 | struct bt_field_class_real *real_fc = (void *) fc; |
621 | ||
622 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 623 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
5cd6d0e5 PP |
624 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); |
625 | real_fc->is_single_precision = (bool) is_single_precision; | |
e5be10ef | 626 | BT_LIB_LOGV("Set real field class's \"is single precision\" property: " |
5cd6d0e5 | 627 | "%!+F", fc); |
5cd6d0e5 PP |
628 | } |
629 | ||
630 | static | |
631 | int init_named_field_classes_container( | |
632 | struct bt_field_class_named_field_class_container *fc, | |
864cad70 | 633 | enum bt_field_class_type type, bt_object_release_func release_func) |
5cd6d0e5 PP |
634 | { |
635 | int ret = 0; | |
636 | ||
864cad70 | 637 | init_field_class((void *) fc, type, release_func); |
5cd6d0e5 PP |
638 | fc->named_fcs = g_array_new(FALSE, TRUE, |
639 | sizeof(struct bt_named_field_class)); | |
640 | if (!fc->named_fcs) { | |
641 | BT_LOGE_STR("Failed to allocate a GArray."); | |
642 | ret = -1; | |
643 | goto end; | |
644 | } | |
645 | ||
646 | fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal); | |
647 | if (!fc->name_to_index) { | |
648 | BT_LOGE_STR("Failed to allocate a GHashTable."); | |
649 | ret = -1; | |
650 | goto end; | |
651 | } | |
652 | ||
653 | end: | |
654 | return ret; | |
655 | } | |
656 | ||
657 | static | |
658 | void finalize_named_field_class(struct bt_named_field_class *named_fc) | |
659 | { | |
660 | BT_ASSERT(named_fc); | |
e5be10ef | 661 | BT_LIB_LOGD("Finalizing named field class: " |
5cd6d0e5 PP |
662 | "addr=%p, name=\"%s\", %![fc-]+F", |
663 | named_fc, named_fc->name ? named_fc->name->str : NULL, | |
664 | named_fc->fc); | |
665 | ||
666 | if (named_fc->name) { | |
667 | g_string_free(named_fc->name, TRUE); | |
668 | } | |
669 | ||
e5be10ef | 670 | BT_LOGD_STR("Putting named field class's field class."); |
65300d60 | 671 | bt_object_put_ref(named_fc->fc); |
5cd6d0e5 PP |
672 | } |
673 | ||
674 | static | |
675 | void finalize_named_field_classes_container( | |
676 | struct bt_field_class_named_field_class_container *fc) | |
677 | { | |
678 | uint64_t i; | |
679 | ||
680 | BT_ASSERT(fc); | |
681 | ||
682 | if (fc->named_fcs) { | |
683 | for (i = 0; i < fc->named_fcs->len; i++) { | |
684 | finalize_named_field_class( | |
685 | &g_array_index(fc->named_fcs, | |
686 | struct bt_named_field_class, i)); | |
687 | } | |
688 | ||
689 | g_array_free(fc->named_fcs, TRUE); | |
690 | } | |
691 | ||
692 | if (fc->name_to_index) { | |
693 | g_hash_table_destroy(fc->name_to_index); | |
694 | } | |
695 | } | |
696 | ||
697 | static | |
698 | void destroy_structure_field_class(struct bt_object *obj) | |
699 | { | |
700 | BT_ASSERT(obj); | |
e5be10ef | 701 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
5cd6d0e5 PP |
702 | finalize_named_field_classes_container((void *) obj); |
703 | g_free(obj); | |
704 | } | |
705 | ||
e5be10ef | 706 | struct bt_private_field_class *bt_private_field_class_structure_create(void) |
5cd6d0e5 PP |
707 | { |
708 | int ret; | |
709 | struct bt_field_class_structure *struct_fc = NULL; | |
710 | ||
e5be10ef | 711 | BT_LOGD_STR("Creating default structure field class object."); |
5cd6d0e5 PP |
712 | struct_fc = g_new0(struct bt_field_class_structure, 1); |
713 | if (!struct_fc) { | |
e5be10ef | 714 | BT_LOGE_STR("Failed to allocate one structure field class."); |
5cd6d0e5 PP |
715 | goto error; |
716 | } | |
717 | ||
718 | ret = init_named_field_classes_container((void *) struct_fc, | |
864cad70 | 719 | BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class); |
5cd6d0e5 PP |
720 | if (ret) { |
721 | goto error; | |
722 | } | |
723 | ||
e5be10ef | 724 | BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc); |
5cd6d0e5 PP |
725 | goto end; |
726 | ||
727 | error: | |
65300d60 | 728 | BT_OBJECT_PUT_REF_AND_RESET(struct_fc); |
5cd6d0e5 PP |
729 | |
730 | end: | |
731 | return (void *) struct_fc; | |
732 | } | |
733 | ||
734 | static | |
735 | int append_named_field_class_to_container_field_class( | |
736 | struct bt_field_class_named_field_class_container *container_fc, | |
737 | const char *name, struct bt_field_class *fc) | |
738 | { | |
739 | int ret = 0; | |
740 | struct bt_named_field_class *named_fc; | |
741 | GString *name_str; | |
742 | ||
743 | BT_ASSERT(container_fc); | |
744 | BT_ASSERT_PRE_FC_HOT(container_fc, "Field class"); | |
745 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
746 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
747 | BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index, | |
748 | name), | |
e5be10ef | 749 | "Duplicate member/option name in structure/variant field class: " |
5cd6d0e5 PP |
750 | "%![container-fc-]+F, name=\"%s\"", container_fc, name); |
751 | name_str = g_string_new(name); | |
752 | if (!name_str) { | |
753 | BT_LOGE_STR("Failed to allocate a GString."); | |
754 | ret = -1; | |
755 | goto end; | |
756 | } | |
757 | ||
758 | g_array_set_size(container_fc->named_fcs, | |
759 | container_fc->named_fcs->len + 1); | |
760 | named_fc = &g_array_index(container_fc->named_fcs, | |
761 | struct bt_named_field_class, container_fc->named_fcs->len - 1); | |
762 | named_fc->name = name_str; | |
65300d60 | 763 | named_fc->fc = bt_object_get_ref(fc); |
5cd6d0e5 PP |
764 | g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, |
765 | GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); | |
766 | bt_field_class_freeze(fc); | |
767 | ||
768 | end: | |
769 | return ret; | |
770 | } | |
771 | ||
28e6ca8b | 772 | int bt_private_field_class_structure_append_member( |
e5be10ef PP |
773 | struct bt_private_field_class *priv_fc, |
774 | const char *name, struct bt_private_field_class *member_fc) | |
5cd6d0e5 | 775 | { |
e5be10ef PP |
776 | struct bt_field_class *fc = (void *) priv_fc; |
777 | ||
5cd6d0e5 | 778 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 | 779 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
5cd6d0e5 | 780 | return append_named_field_class_to_container_field_class((void *) fc, |
e5be10ef | 781 | name, (void *) member_fc); |
5cd6d0e5 PP |
782 | } |
783 | ||
784 | uint64_t bt_field_class_structure_get_member_count(struct bt_field_class *fc) | |
785 | { | |
786 | struct bt_field_class_structure *struct_fc = (void *) fc; | |
787 | ||
788 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 789 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
5cd6d0e5 PP |
790 | return (uint64_t) struct_fc->common.named_fcs->len; |
791 | } | |
792 | ||
793 | static | |
794 | void borrow_named_field_class_from_container_field_class_at_index( | |
795 | struct bt_field_class_named_field_class_container *fc, | |
796 | uint64_t index, const char **name, | |
797 | struct bt_field_class **out_fc) | |
798 | { | |
799 | struct bt_named_field_class *named_fc; | |
800 | ||
801 | BT_ASSERT(fc); | |
802 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
803 | BT_ASSERT_PRE_NON_NULL(out_fc, "Field class (output)"); | |
804 | BT_ASSERT_PRE_VALID_INDEX(index, fc->named_fcs->len); | |
805 | named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, index); | |
806 | *name = named_fc->name->str; | |
807 | *out_fc = named_fc->fc; | |
808 | } | |
809 | ||
810 | void bt_field_class_structure_borrow_member_by_index( | |
811 | struct bt_field_class *fc, uint64_t index, | |
812 | const char **name, struct bt_field_class **out_fc) | |
813 | { | |
814 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 815 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
5cd6d0e5 PP |
816 | borrow_named_field_class_from_container_field_class_at_index((void *) fc, |
817 | index, name, out_fc); | |
818 | } | |
819 | ||
28e6ca8b | 820 | void bt_private_field_class_structure_borrow_member_by_index( |
e5be10ef PP |
821 | struct bt_private_field_class *fc, uint64_t index, |
822 | const char **name, struct bt_private_field_class **out_fc) | |
823 | { | |
824 | bt_field_class_structure_borrow_member_by_index((void *) fc, | |
825 | index, name, (void *) out_fc); | |
826 | } | |
827 | ||
5cd6d0e5 PP |
828 | static |
829 | struct bt_field_class *borrow_field_class_from_container_field_class_by_name( | |
830 | struct bt_field_class_named_field_class_container *fc, | |
831 | const char *name) | |
832 | { | |
833 | struct bt_field_class *ret_fc = NULL; | |
834 | struct bt_named_field_class *named_fc; | |
835 | gpointer orig_key; | |
836 | gpointer value; | |
837 | ||
838 | BT_ASSERT(fc); | |
839 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
840 | if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key, | |
841 | &value)) { | |
842 | goto end; | |
843 | } | |
844 | ||
845 | named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, | |
846 | GPOINTER_TO_UINT(value)); | |
847 | ret_fc = named_fc->fc; | |
848 | ||
849 | end: | |
850 | return ret_fc; | |
851 | } | |
852 | ||
853 | struct bt_field_class *bt_field_class_structure_borrow_member_field_class_by_name( | |
854 | struct bt_field_class *fc, const char *name) | |
855 | { | |
856 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 857 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
5cd6d0e5 PP |
858 | return borrow_field_class_from_container_field_class_by_name((void *) fc, |
859 | name); | |
860 | } | |
861 | ||
e5be10ef | 862 | struct bt_private_field_class * |
28e6ca8b | 863 | bt_private_field_class_structure_borrow_member_field_class_by_name( |
e5be10ef PP |
864 | struct bt_private_field_class *fc, const char *name) |
865 | { | |
866 | return (void *) bt_field_class_structure_borrow_member_field_class_by_name( | |
867 | (void *) fc, name); | |
868 | } | |
869 | ||
5cd6d0e5 PP |
870 | static |
871 | void destroy_variant_field_class(struct bt_object *obj) | |
872 | { | |
873 | struct bt_field_class_variant *fc = (void *) obj; | |
874 | ||
875 | BT_ASSERT(fc); | |
e5be10ef | 876 | BT_LIB_LOGD("Destroying variant field class object: %!+F", fc); |
5cd6d0e5 PP |
877 | finalize_named_field_classes_container((void *) fc); |
878 | BT_LOGD_STR("Putting selector field path."); | |
65300d60 | 879 | bt_object_put_ref(fc->selector_field_path); |
5cd6d0e5 PP |
880 | g_free(fc); |
881 | } | |
882 | ||
e5be10ef | 883 | struct bt_private_field_class *bt_private_field_class_variant_create(void) |
5cd6d0e5 PP |
884 | { |
885 | int ret; | |
886 | struct bt_field_class_variant *var_fc = NULL; | |
887 | ||
e5be10ef | 888 | BT_LOGD_STR("Creating default variant field class object."); |
5cd6d0e5 PP |
889 | var_fc = g_new0(struct bt_field_class_variant, 1); |
890 | if (!var_fc) { | |
e5be10ef | 891 | BT_LOGE_STR("Failed to allocate one variant field class."); |
5cd6d0e5 PP |
892 | goto error; |
893 | } | |
894 | ||
895 | ret = init_named_field_classes_container((void *) var_fc, | |
864cad70 | 896 | BT_FIELD_CLASS_TYPE_VARIANT, destroy_variant_field_class); |
5cd6d0e5 PP |
897 | if (ret) { |
898 | goto error; | |
899 | } | |
900 | ||
e5be10ef | 901 | BT_LIB_LOGD("Created variant field class object: %!+F", var_fc); |
5cd6d0e5 PP |
902 | goto end; |
903 | ||
904 | error: | |
65300d60 | 905 | BT_OBJECT_PUT_REF_AND_RESET(var_fc); |
5cd6d0e5 PP |
906 | |
907 | end: | |
908 | return (void *) var_fc; | |
909 | } | |
910 | ||
28e6ca8b | 911 | int bt_private_field_class_variant_set_selector_field_class( |
e5be10ef PP |
912 | struct bt_private_field_class *priv_fc, |
913 | struct bt_private_field_class *selector_fc) | |
5cd6d0e5 | 914 | { |
e5be10ef | 915 | struct bt_field_class *fc = (void *) priv_fc; |
5cd6d0e5 PP |
916 | struct bt_field_class_variant *var_fc = (void *) fc; |
917 | ||
e5be10ef PP |
918 | BT_ASSERT_PRE_NON_NULL(fc, "Variant field class"); |
919 | BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class"); | |
864cad70 | 920 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
e5be10ef PP |
921 | BT_ASSERT_PRE_FC_IS_ENUM(selector_fc, "Selector field class"); |
922 | BT_ASSERT_PRE_FC_HOT(fc, "Variant field class"); | |
923 | var_fc->selector_fc = (void *) selector_fc; | |
924 | bt_field_class_freeze((void *) selector_fc); | |
5cd6d0e5 PP |
925 | return 0; |
926 | } | |
927 | ||
e5be10ef PP |
928 | int bt_private_field_class_variant_append_private_option( |
929 | struct bt_private_field_class *priv_fc, | |
930 | const char *name, struct bt_private_field_class *option_fc) | |
5cd6d0e5 | 931 | { |
e5be10ef PP |
932 | struct bt_field_class *fc = (void *) priv_fc; |
933 | ||
5cd6d0e5 | 934 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 | 935 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
5cd6d0e5 | 936 | return append_named_field_class_to_container_field_class((void *) fc, |
e5be10ef | 937 | name, (void *) option_fc); |
5cd6d0e5 PP |
938 | } |
939 | ||
940 | struct bt_field_class *bt_field_class_variant_borrow_option_field_class_by_name( | |
941 | struct bt_field_class *fc, const char *name) | |
942 | { | |
943 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 944 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
5cd6d0e5 PP |
945 | return borrow_field_class_from_container_field_class_by_name((void *) fc, |
946 | name); | |
947 | } | |
948 | ||
e5be10ef | 949 | struct bt_private_field_class * |
28e6ca8b | 950 | bt_private_field_class_variant_borrow_option_field_class_by_name( |
e5be10ef PP |
951 | struct bt_private_field_class *fc, const char *name) |
952 | { | |
953 | return (void *) bt_field_class_variant_borrow_option_field_class_by_name( | |
954 | (void *) fc, name); | |
955 | } | |
956 | ||
5cd6d0e5 PP |
957 | uint64_t bt_field_class_variant_get_option_count(struct bt_field_class *fc) |
958 | { | |
959 | struct bt_field_class_variant *var_fc = (void *) fc; | |
960 | ||
961 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 962 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
5cd6d0e5 PP |
963 | return (uint64_t) var_fc->common.named_fcs->len; |
964 | } | |
965 | ||
966 | void bt_field_class_variant_borrow_option_by_index( | |
967 | struct bt_field_class *fc, uint64_t index, | |
968 | const char **name, struct bt_field_class **out_fc) | |
969 | { | |
970 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 971 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
5cd6d0e5 PP |
972 | borrow_named_field_class_from_container_field_class_at_index((void *) fc, |
973 | index, name, out_fc); | |
974 | } | |
975 | ||
28e6ca8b | 976 | void bt_private_field_class_variant_borrow_option_by_index( |
e5be10ef PP |
977 | struct bt_private_field_class *fc, uint64_t index, |
978 | const char **name, struct bt_private_field_class **out_fc) | |
979 | { | |
980 | bt_field_class_variant_borrow_option_by_index((void *) fc, | |
981 | index, name, (void *) out_fc); | |
982 | } | |
983 | ||
5cd6d0e5 PP |
984 | struct bt_field_path *bt_field_class_variant_borrow_selector_field_path( |
985 | struct bt_field_class *fc) | |
986 | { | |
987 | struct bt_field_class_variant *var_fc = (void *) fc; | |
988 | ||
989 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 990 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, |
5cd6d0e5 PP |
991 | "Field class"); |
992 | return var_fc->selector_field_path; | |
993 | } | |
994 | ||
995 | static | |
996 | void init_array_field_class(struct bt_field_class_array *fc, | |
864cad70 | 997 | enum bt_field_class_type type, bt_object_release_func release_func, |
5cd6d0e5 PP |
998 | struct bt_field_class *element_fc) |
999 | { | |
1000 | BT_ASSERT(element_fc); | |
864cad70 | 1001 | init_field_class((void *) fc, type, release_func); |
65300d60 | 1002 | fc->element_fc = bt_object_get_ref(element_fc); |
5cd6d0e5 PP |
1003 | bt_field_class_freeze(element_fc); |
1004 | } | |
1005 | ||
1006 | static | |
1007 | void finalize_array_field_class(struct bt_field_class_array *array_fc) | |
1008 | { | |
1009 | BT_ASSERT(array_fc); | |
e5be10ef | 1010 | BT_LOGD_STR("Putting element field class."); |
65300d60 | 1011 | bt_object_put_ref(array_fc->element_fc); |
5cd6d0e5 PP |
1012 | } |
1013 | ||
1014 | static | |
1015 | void destroy_static_array_field_class(struct bt_object *obj) | |
1016 | { | |
1017 | BT_ASSERT(obj); | |
e5be10ef | 1018 | BT_LIB_LOGD("Destroying static array field class object: %!+F", obj); |
5cd6d0e5 PP |
1019 | finalize_array_field_class((void *) obj); |
1020 | g_free(obj); | |
1021 | } | |
1022 | ||
e5be10ef PP |
1023 | struct bt_private_field_class * |
1024 | bt_private_field_class_static_array_create( | |
1025 | struct bt_private_field_class *priv_element_fc, | |
1026 | uint64_t length) | |
5cd6d0e5 | 1027 | { |
e5be10ef | 1028 | struct bt_field_class *element_fc = (void *) priv_element_fc; |
5cd6d0e5 PP |
1029 | struct bt_field_class_static_array *array_fc = NULL; |
1030 | ||
e5be10ef PP |
1031 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1032 | BT_LOGD_STR("Creating default static array field class object."); | |
5cd6d0e5 PP |
1033 | array_fc = g_new0(struct bt_field_class_static_array, 1); |
1034 | if (!array_fc) { | |
e5be10ef | 1035 | BT_LOGE_STR("Failed to allocate one static array field class."); |
5cd6d0e5 PP |
1036 | goto error; |
1037 | } | |
1038 | ||
864cad70 | 1039 | init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
5cd6d0e5 PP |
1040 | destroy_static_array_field_class, element_fc); |
1041 | array_fc->length = length; | |
e5be10ef | 1042 | BT_LIB_LOGD("Created static array field class object: %!+F", array_fc); |
5cd6d0e5 PP |
1043 | goto end; |
1044 | ||
1045 | error: | |
65300d60 | 1046 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
5cd6d0e5 PP |
1047 | |
1048 | end: | |
1049 | return (void *) array_fc; | |
1050 | } | |
1051 | ||
1052 | struct bt_field_class *bt_field_class_array_borrow_element_field_class( | |
1053 | struct bt_field_class *fc) | |
1054 | { | |
1055 | struct bt_field_class_array *array_fc = (void *) fc; | |
1056 | ||
1057 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1058 | BT_ASSERT_PRE_FC_IS_ARRAY(fc, "Field class"); | |
1059 | return array_fc->element_fc; | |
1060 | } | |
1061 | ||
e5be10ef PP |
1062 | struct bt_private_field_class * |
1063 | bt_private_field_class_array_borrow_element_field_class( | |
1064 | struct bt_private_field_class *fc) | |
1065 | { | |
1066 | return (void *) bt_field_class_array_borrow_element_field_class( | |
1067 | (void *) fc); | |
1068 | } | |
1069 | ||
5cd6d0e5 PP |
1070 | uint64_t bt_field_class_static_array_get_length(struct bt_field_class *fc) |
1071 | { | |
1072 | struct bt_field_class_static_array *array_fc = (void *) fc; | |
1073 | ||
1074 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 1075 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
5cd6d0e5 PP |
1076 | "Field class"); |
1077 | return (uint64_t) array_fc->length; | |
1078 | } | |
1079 | ||
1080 | static | |
1081 | void destroy_dynamic_array_field_class(struct bt_object *obj) | |
1082 | { | |
1083 | struct bt_field_class_dynamic_array *fc = (void *) obj; | |
1084 | ||
1085 | BT_ASSERT(fc); | |
e5be10ef | 1086 | BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc); |
5cd6d0e5 PP |
1087 | finalize_array_field_class((void *) fc); |
1088 | BT_LOGD_STR("Putting length field path."); | |
65300d60 | 1089 | bt_object_put_ref(fc->length_field_path); |
5cd6d0e5 PP |
1090 | g_free(fc); |
1091 | } | |
1092 | ||
e5be10ef PP |
1093 | struct bt_private_field_class *bt_private_field_class_dynamic_array_create( |
1094 | struct bt_private_field_class *priv_element_fc) | |
5cd6d0e5 | 1095 | { |
e5be10ef | 1096 | struct bt_field_class *element_fc = (void *) priv_element_fc; |
5cd6d0e5 PP |
1097 | struct bt_field_class_dynamic_array *array_fc = NULL; |
1098 | ||
e5be10ef PP |
1099 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1100 | BT_LOGD_STR("Creating default dynamic array field class object."); | |
5cd6d0e5 PP |
1101 | array_fc = g_new0(struct bt_field_class_dynamic_array, 1); |
1102 | if (!array_fc) { | |
e5be10ef | 1103 | BT_LOGE_STR("Failed to allocate one dynamic array field class."); |
5cd6d0e5 PP |
1104 | goto error; |
1105 | } | |
1106 | ||
864cad70 | 1107 | init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
5cd6d0e5 | 1108 | destroy_dynamic_array_field_class, element_fc); |
e5be10ef | 1109 | BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc); |
5cd6d0e5 PP |
1110 | goto end; |
1111 | ||
1112 | error: | |
65300d60 | 1113 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
5cd6d0e5 PP |
1114 | |
1115 | end: | |
1116 | return (void *) array_fc; | |
1117 | } | |
1118 | ||
28e6ca8b | 1119 | int bt_private_field_class_dynamic_array_set_length_field_class( |
e5be10ef PP |
1120 | struct bt_private_field_class *priv_fc, |
1121 | struct bt_private_field_class *priv_length_fc) | |
5cd6d0e5 | 1122 | { |
e5be10ef PP |
1123 | struct bt_field_class *fc = (void *) priv_fc; |
1124 | struct bt_field_class *length_fc = (void *) priv_length_fc; | |
5cd6d0e5 PP |
1125 | struct bt_field_class_dynamic_array *array_fc = (void *) fc; |
1126 | ||
e5be10ef PP |
1127 | BT_ASSERT_PRE_NON_NULL(fc, "Dynamic array field class"); |
1128 | BT_ASSERT_PRE_NON_NULL(length_fc, "Length field class"); | |
864cad70 | 1129 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
5cd6d0e5 | 1130 | "Field class"); |
e5be10ef PP |
1131 | BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, "Length field class"); |
1132 | BT_ASSERT_PRE_FC_HOT(fc, "Dynamic array field class"); | |
5cd6d0e5 PP |
1133 | array_fc->length_fc = length_fc; |
1134 | bt_field_class_freeze(length_fc); | |
1135 | return 0; | |
1136 | } | |
1137 | ||
1138 | struct bt_field_path *bt_field_class_dynamic_array_borrow_length_field_path( | |
1139 | struct bt_field_class *fc) | |
1140 | { | |
1141 | struct bt_field_class_dynamic_array *seq_fc = (void *) fc; | |
1142 | ||
1143 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 1144 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
5cd6d0e5 PP |
1145 | "Field class"); |
1146 | return seq_fc->length_field_path; | |
1147 | } | |
1148 | ||
1149 | static | |
1150 | void destroy_string_field_class(struct bt_object *obj) | |
1151 | { | |
1152 | BT_ASSERT(obj); | |
e5be10ef | 1153 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
5cd6d0e5 PP |
1154 | g_free(obj); |
1155 | } | |
1156 | ||
e5be10ef | 1157 | struct bt_private_field_class *bt_private_field_class_string_create(void) |
5cd6d0e5 PP |
1158 | { |
1159 | struct bt_field_class_string *string_fc = NULL; | |
1160 | ||
e5be10ef | 1161 | BT_LOGD_STR("Creating default string field class object."); |
5cd6d0e5 PP |
1162 | string_fc = g_new0(struct bt_field_class_string, 1); |
1163 | if (!string_fc) { | |
e5be10ef | 1164 | BT_LOGE_STR("Failed to allocate one string field class."); |
5cd6d0e5 PP |
1165 | goto error; |
1166 | } | |
1167 | ||
864cad70 | 1168 | init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING, |
5cd6d0e5 | 1169 | destroy_string_field_class); |
e5be10ef | 1170 | BT_LIB_LOGD("Created string field class object: %!+F", string_fc); |
5cd6d0e5 PP |
1171 | goto end; |
1172 | ||
1173 | error: | |
65300d60 | 1174 | BT_OBJECT_PUT_REF_AND_RESET(string_fc); |
5cd6d0e5 PP |
1175 | |
1176 | end: | |
1177 | return (void *) string_fc; | |
1178 | } | |
1179 | ||
1180 | BT_HIDDEN | |
1181 | void _bt_field_class_freeze(struct bt_field_class *fc) | |
1182 | { | |
1183 | /* | |
1184 | * Element/member/option field classes are frozen when added to | |
1185 | * their owner. | |
1186 | */ | |
1187 | BT_ASSERT(fc); | |
1188 | fc->frozen = true; | |
1189 | } | |
1190 | ||
1191 | BT_HIDDEN | |
1192 | void _bt_field_class_make_part_of_trace(struct bt_field_class *fc) | |
1193 | { | |
1194 | BT_ASSERT(fc); | |
1195 | BT_ASSERT_PRE(!fc->part_of_trace, | |
1196 | "Field class is already part of a trace: %!+F", fc); | |
1197 | fc->part_of_trace = true; | |
1198 | ||
864cad70 PP |
1199 | switch (fc->type) { |
1200 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1201 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
5cd6d0e5 PP |
1202 | { |
1203 | struct bt_field_class_named_field_class_container *container_fc = | |
1204 | (void *) fc; | |
1205 | uint64_t i; | |
1206 | ||
1207 | for (i = 0; i < container_fc->named_fcs->len; i++) { | |
1208 | struct bt_named_field_class *named_fc = | |
1209 | BT_FIELD_CLASS_NAMED_FC_AT_INDEX( | |
1210 | container_fc, i); | |
1211 | ||
1212 | bt_field_class_make_part_of_trace(named_fc->fc); | |
1213 | } | |
1214 | ||
1215 | break; | |
1216 | } | |
864cad70 PP |
1217 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
1218 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
5cd6d0e5 PP |
1219 | { |
1220 | struct bt_field_class_array *array_fc = (void *) fc; | |
1221 | ||
1222 | bt_field_class_make_part_of_trace(array_fc->element_fc); | |
1223 | break; | |
1224 | } | |
1225 | default: | |
1226 | break; | |
1227 | } | |
1228 | } |