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