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