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