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