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