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