4dfed4929317822c26734a21906ce146b65d1f3f
[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 struct bt_field_class *bt_field_class_real_create(bt_trace_class *trace_class)
698 {
699 struct bt_field_class_real *real_fc = NULL;
700
701 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
702 BT_LOGD_STR("Creating default real field class object.");
703 real_fc = g_new0(struct bt_field_class_real, 1);
704 if (!real_fc) {
705 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
706 goto error;
707 }
708
709 if (init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL,
710 destroy_real_field_class)) {
711 goto error;
712 }
713
714 BT_LIB_LOGD("Created real field class object: %!+F", real_fc);
715 goto end;
716
717 error:
718 BT_OBJECT_PUT_REF_AND_RESET(real_fc);
719
720 end:
721 return (void *) real_fc;
722 }
723
724 bt_bool bt_field_class_real_is_single_precision(const struct bt_field_class *fc)
725 {
726 const struct bt_field_class_real *real_fc = (const void *) fc;
727
728 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
729 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class");
730 return real_fc->is_single_precision;
731 }
732
733 void bt_field_class_real_set_is_single_precision(struct bt_field_class *fc,
734 bt_bool is_single_precision)
735 {
736 struct bt_field_class_real *real_fc = (void *) fc;
737
738 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
739 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class");
740 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
741 real_fc->is_single_precision = (bool) is_single_precision;
742 BT_LIB_LOGD("Set real field class's \"is single precision\" property: "
743 "%!+F", fc);
744 }
745
746 static
747 int init_named_field_classes_container(
748 struct bt_field_class_named_field_class_container *fc,
749 enum bt_field_class_type type,
750 bt_object_release_func fc_release_func,
751 GDestroyNotify named_fc_destroy_func)
752 {
753 int ret = 0;
754
755 ret = init_field_class((void *) fc, type, fc_release_func);
756 if (ret) {
757 goto end;
758 }
759
760 fc->named_fcs = g_ptr_array_new_with_free_func(named_fc_destroy_func);
761 if (!fc->named_fcs) {
762 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
763 ret = -1;
764 goto end;
765 }
766
767 fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal);
768 if (!fc->name_to_index) {
769 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
770 ret = -1;
771 goto end;
772 }
773
774 end:
775 return ret;
776 }
777
778 static
779 void finalize_named_field_class(struct bt_named_field_class *named_fc)
780 {
781 BT_ASSERT(named_fc);
782 BT_LIB_LOGD("Finalizing named field class: "
783 "addr=%p, name=\"%s\", %![fc-]+F",
784 named_fc, named_fc->name ? named_fc->name->str : NULL,
785 named_fc->fc);
786 BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes);
787
788 if (named_fc->name) {
789 g_string_free(named_fc->name, TRUE);
790 named_fc->name = NULL;
791 }
792
793 BT_LOGD_STR("Putting named field class's field class.");
794 BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc);
795 }
796
797 static
798 void destroy_named_field_class(gpointer ptr)
799 {
800 struct bt_named_field_class *named_fc = ptr;
801
802 if (ptr) {
803 BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes);
804 finalize_named_field_class(ptr);
805 g_free(ptr);
806 }
807 }
808
809 static
810 void destroy_variant_with_selector_option(gpointer ptr)
811 {
812 struct bt_field_class_variant_with_selector_option *opt = ptr;
813
814 if (ptr) {
815 finalize_named_field_class(&opt->common);
816 BT_OBJECT_PUT_REF_AND_RESET(opt->range_set);
817 g_free(ptr);
818 }
819 }
820
821 static
822 void finalize_named_field_classes_container(
823 struct bt_field_class_named_field_class_container *fc)
824 {
825 BT_ASSERT(fc);
826
827 if (fc->named_fcs) {
828 g_ptr_array_free(fc->named_fcs, TRUE);
829 fc->named_fcs = NULL;
830
831 }
832
833 if (fc->name_to_index) {
834 g_hash_table_destroy(fc->name_to_index);
835 fc->name_to_index = NULL;
836 }
837 }
838
839 static
840 void destroy_structure_field_class(struct bt_object *obj)
841 {
842 BT_ASSERT(obj);
843 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj);
844 finalize_field_class((void *) obj);
845 finalize_named_field_classes_container((void *) obj);
846 g_free(obj);
847 }
848
849 struct bt_field_class *bt_field_class_structure_create(
850 bt_trace_class *trace_class)
851 {
852 int ret;
853 struct bt_field_class_structure *struct_fc = NULL;
854
855 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
856 BT_LOGD_STR("Creating default structure field class object.");
857 struct_fc = g_new0(struct bt_field_class_structure, 1);
858 if (!struct_fc) {
859 BT_LIB_LOGE_APPEND_CAUSE(
860 "Failed to allocate one structure field class.");
861 goto error;
862 }
863
864 ret = init_named_field_classes_container((void *) struct_fc,
865 BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class,
866 destroy_named_field_class);
867 if (ret) {
868 /* init_named_field_classes_container() logs errors */
869 goto error;
870 }
871
872 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc);
873 goto end;
874
875 error:
876 BT_OBJECT_PUT_REF_AND_RESET(struct_fc);
877
878 end:
879 return (void *) struct_fc;
880 }
881
882 static
883 int init_named_field_class(struct bt_named_field_class *named_fc,
884 const char *name, struct bt_field_class *fc)
885 {
886 int status = BT_FUNC_STATUS_OK;
887
888 BT_ASSERT(named_fc);
889 BT_ASSERT(name);
890 BT_ASSERT(fc);
891 named_fc->name = g_string_new(name);
892 if (!named_fc->name) {
893 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
894 status = BT_FUNC_STATUS_MEMORY_ERROR;
895 goto end;
896 }
897
898 named_fc->user_attributes = bt_value_map_create();
899 if (!named_fc->user_attributes) {
900 BT_LIB_LOGE_APPEND_CAUSE(
901 "Failed to create a map value object.");
902 status = BT_FUNC_STATUS_MEMORY_ERROR;
903 goto end;
904 }
905
906 named_fc->fc = fc;
907 bt_object_get_no_null_check(named_fc->fc);
908
909 end:
910 return status;
911 }
912
913 static
914 struct bt_named_field_class *create_named_field_class(const char *name,
915 struct bt_field_class *fc)
916 {
917 struct bt_named_field_class *named_fc = g_new0(
918 struct bt_named_field_class, 1);
919
920 if (!named_fc) {
921 BT_LIB_LOGE_APPEND_CAUSE(
922 "Failed to allocate a named field class.");
923 goto error;
924 }
925
926 if (init_named_field_class(named_fc, name, fc)) {
927 /* init_named_field_class() logs errors */
928 goto error;
929 }
930
931 goto end;
932
933 error:
934 destroy_named_field_class(named_fc);
935 named_fc = NULL;
936
937 end:
938 return named_fc;
939 }
940
941 static
942 struct bt_field_class_variant_with_selector_option *
943 create_variant_with_selector_option(
944 const char *name, struct bt_field_class *fc,
945 const struct bt_integer_range_set *range_set)
946 {
947 struct bt_field_class_variant_with_selector_option *opt = g_new0(
948 struct bt_field_class_variant_with_selector_option, 1);
949
950 BT_ASSERT(range_set);
951
952 if (!opt) {
953 BT_LIB_LOGE_APPEND_CAUSE(
954 "Failed to allocate a named field class.");
955 goto error;
956 }
957
958 if (init_named_field_class(&opt->common, name, fc)) {
959 goto error;
960 }
961
962 opt->range_set = range_set;
963 bt_object_get_no_null_check(opt->range_set);
964 bt_integer_range_set_freeze(range_set);
965 goto end;
966
967 error:
968 destroy_variant_with_selector_option(opt);
969 opt = NULL;
970
971 end:
972 return opt;
973 }
974
975 static
976 int append_named_field_class_to_container_field_class(
977 struct bt_field_class_named_field_class_container *container_fc,
978 struct bt_named_field_class *named_fc)
979 {
980 BT_ASSERT(container_fc);
981 BT_ASSERT(named_fc);
982 BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class");
983 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index,
984 named_fc->name->str),
985 "Duplicate member/option name in structure/variant field class: "
986 "%![container-fc-]+F, name=\"%s\"", container_fc,
987 named_fc->name->str);
988
989 /*
990 * Freeze the contained field class, but not the named field
991 * class itself, as it's still possible afterwards to modify
992 * properties of the member/option object.
993 */
994 bt_field_class_freeze(named_fc->fc);
995 g_ptr_array_add(container_fc->named_fcs, named_fc);
996 g_hash_table_insert(container_fc->name_to_index, named_fc->name->str,
997 GUINT_TO_POINTER(container_fc->named_fcs->len - 1));
998 return BT_FUNC_STATUS_OK;
999 }
1000
1001 enum bt_field_class_structure_append_member_status
1002 bt_field_class_structure_append_member(
1003 struct bt_field_class *fc, const char *name,
1004 struct bt_field_class *member_fc)
1005 {
1006 enum bt_field_class_structure_append_member_status status;
1007 struct bt_named_field_class *named_fc = NULL;
1008
1009 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1010 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1011 "Field class");
1012 named_fc = create_named_field_class(name, member_fc);
1013 if (!named_fc) {
1014 /* create_named_field_class() logs errors */
1015 status = BT_FUNC_STATUS_MEMORY_ERROR;
1016 goto end;
1017 }
1018
1019 status = append_named_field_class_to_container_field_class((void *) fc,
1020 named_fc);
1021 if (status == BT_FUNC_STATUS_OK) {
1022 /* Moved to the container */
1023 named_fc = NULL;
1024 }
1025
1026 end:
1027 return status;
1028 }
1029
1030 uint64_t bt_field_class_structure_get_member_count(
1031 const struct bt_field_class *fc)
1032 {
1033 struct bt_field_class_structure *struct_fc = (void *) fc;
1034
1035 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1036 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1037 "Field class");
1038 return (uint64_t) struct_fc->common.named_fcs->len;
1039 }
1040
1041 static
1042 struct bt_named_field_class *
1043 borrow_named_field_class_from_container_field_class_at_index(
1044 struct bt_field_class_named_field_class_container *fc,
1045 uint64_t index)
1046 {
1047 BT_ASSERT(fc);
1048 BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len);
1049 return fc->named_fcs->pdata[index];
1050 }
1051
1052 const struct bt_field_class_structure_member *
1053 bt_field_class_structure_borrow_member_by_index_const(
1054 const struct bt_field_class *fc, uint64_t index)
1055 {
1056 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1057 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1058 "Field class");
1059 return (const void *)
1060 borrow_named_field_class_from_container_field_class_at_index(
1061 (void *) fc, index);
1062 }
1063
1064 struct bt_field_class_structure_member *
1065 bt_field_class_structure_borrow_member_by_index(
1066 struct bt_field_class *fc, uint64_t index)
1067 {
1068 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1069 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1070 "Field class");
1071 return (void *)
1072 borrow_named_field_class_from_container_field_class_at_index(
1073 (void *) fc, index);
1074 }
1075
1076 static
1077 struct bt_named_field_class *
1078 borrow_named_field_class_from_container_field_class_by_name(
1079 struct bt_field_class_named_field_class_container *fc,
1080 const char *name)
1081 {
1082 struct bt_named_field_class *named_fc = NULL;
1083 gpointer orig_key;
1084 gpointer value;
1085
1086 BT_ASSERT(fc);
1087 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
1088 if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key,
1089 &value)) {
1090 goto end;
1091 }
1092
1093 named_fc = fc->named_fcs->pdata[GPOINTER_TO_UINT(value)];
1094
1095 end:
1096 return named_fc;
1097 }
1098
1099 const struct bt_field_class_structure_member *
1100 bt_field_class_structure_borrow_member_by_name_const(
1101 const struct bt_field_class *fc, const char *name)
1102 {
1103 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1104 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1105 "Field class");
1106 return (const void *)
1107 borrow_named_field_class_from_container_field_class_by_name(
1108 (void *) fc, name);
1109 }
1110
1111 struct bt_field_class_structure_member *
1112 bt_field_class_structure_borrow_member_by_name(
1113 struct bt_field_class *fc, const char *name)
1114 {
1115 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1116 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1117 "Field class");
1118 return (void *)
1119 borrow_named_field_class_from_container_field_class_by_name(
1120 (void *) fc, name);
1121 }
1122
1123 const char *bt_field_class_structure_member_get_name(
1124 const struct bt_field_class_structure_member *member)
1125 {
1126 const struct bt_named_field_class *named_fc = (const void *) member;
1127
1128 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1129 return named_fc->name->str;
1130 }
1131
1132 const struct bt_field_class *
1133 bt_field_class_structure_member_borrow_field_class_const(
1134 const struct bt_field_class_structure_member *member)
1135 {
1136 const struct bt_named_field_class *named_fc = (const void *) member;
1137
1138 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1139 return named_fc->fc;
1140 }
1141
1142 struct bt_field_class *
1143 bt_field_class_structure_member_borrow_field_class(
1144 struct bt_field_class_structure_member *member)
1145 {
1146 struct bt_named_field_class *named_fc = (void *) member;
1147
1148 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1149 return named_fc->fc;
1150 }
1151
1152 static
1153 void destroy_option_field_class(struct bt_object *obj)
1154 {
1155 struct bt_field_class_option *fc = (void *) obj;
1156
1157 BT_ASSERT(fc);
1158 BT_LIB_LOGD("Destroying option field class object: %!+F", fc);
1159 finalize_field_class((void *) obj);
1160 BT_LOGD_STR("Putting content field class.");
1161 BT_OBJECT_PUT_REF_AND_RESET(fc->content_fc);
1162 BT_LOGD_STR("Putting selector field path.");
1163 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path);
1164 BT_LOGD_STR("Putting selector field class.");
1165 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc);
1166 g_free(fc);
1167 }
1168
1169 struct bt_field_class *bt_field_class_option_create(bt_trace_class *trace_class,
1170 bt_field_class *content_fc, bt_field_class *selector_fc)
1171 {
1172 struct bt_field_class_option *opt_fc = NULL;
1173
1174 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1175 BT_ASSERT_PRE_NON_NULL(content_fc, "Content field class");
1176 BT_LIB_LOGD("Creating option field class: "
1177 "%![content-fc-]+F, %![sel-fc-]+F", content_fc, selector_fc);
1178 opt_fc = g_new0(struct bt_field_class_option, 1);
1179 if (!opt_fc) {
1180 BT_LIB_LOGE_APPEND_CAUSE(
1181 "Failed to allocate one option field class.");
1182 goto error;
1183 }
1184
1185 if (init_field_class((void *) opt_fc, BT_FIELD_CLASS_TYPE_OPTION,
1186 destroy_option_field_class)) {
1187 goto error;
1188 }
1189
1190 opt_fc->content_fc = content_fc;
1191 bt_object_get_no_null_check(opt_fc->content_fc);
1192 bt_field_class_freeze(opt_fc->content_fc);
1193
1194 if (selector_fc) {
1195 BT_ASSERT_PRE_FC_HAS_ID(selector_fc, BT_FIELD_CLASS_TYPE_BOOL,
1196 "Selector field class");
1197 opt_fc->selector_fc = selector_fc;
1198 bt_object_get_no_null_check(opt_fc->selector_fc);
1199 bt_field_class_freeze(selector_fc);
1200 }
1201
1202 BT_LIB_LOGD("Created option field class object: "
1203 "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc, selector_fc);
1204 goto end;
1205
1206 error:
1207 BT_OBJECT_PUT_REF_AND_RESET(opt_fc);
1208
1209 end:
1210 return (void *) opt_fc;
1211 }
1212
1213 const struct bt_field_class *bt_field_class_option_borrow_field_class_const(
1214 const struct bt_field_class *fc)
1215 {
1216 struct bt_field_class_option *opt_fc = (void *) fc;
1217
1218 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1219 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_OPTION,
1220 "Field class");
1221 return opt_fc->content_fc;
1222 }
1223
1224 struct bt_field_class *bt_field_class_option_borrow_field_class(
1225 struct bt_field_class *fc)
1226 {
1227 struct bt_field_class_option *opt_fc = (void *) fc;
1228
1229 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1230 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_OPTION,
1231 "Field class");
1232 return opt_fc->content_fc;
1233 }
1234
1235 const struct bt_field_path *
1236 bt_field_class_option_borrow_selector_field_path_const(
1237 const struct bt_field_class *fc)
1238 {
1239 struct bt_field_class_option *opt_fc = (void *) fc;
1240
1241 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1242 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_OPTION,
1243 "Field class");
1244 return opt_fc->selector_field_path;
1245 }
1246
1247 static
1248 void finalize_variant_field_class(struct bt_field_class_variant *var_fc)
1249 {
1250 BT_ASSERT(var_fc);
1251 BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc);
1252 finalize_field_class((void *) var_fc);
1253 finalize_named_field_classes_container((void *) var_fc);
1254 }
1255
1256 static
1257 void destroy_variant_field_class(struct bt_object *obj)
1258 {
1259 struct bt_field_class_variant *fc = (void *) obj;
1260
1261 BT_ASSERT(fc);
1262 finalize_variant_field_class(fc);
1263 g_free(fc);
1264 }
1265
1266 static
1267 void destroy_variant_with_selector_field_class(struct bt_object *obj)
1268 {
1269 struct bt_field_class_variant_with_selector *fc = (void *) obj;
1270
1271 BT_ASSERT(fc);
1272 finalize_variant_field_class(&fc->common);
1273 BT_LOGD_STR("Putting selector field path.");
1274 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path);
1275 BT_LOGD_STR("Putting selector field class.");
1276 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc);
1277 g_free(fc);
1278 }
1279
1280 struct bt_field_class *bt_field_class_variant_create(
1281 bt_trace_class *trace_class, bt_field_class *selector_fc)
1282 {
1283 int ret;
1284 struct bt_field_class_variant *var_fc = NULL;
1285 struct bt_field_class_variant_with_selector *var_with_sel_fc = NULL;
1286 enum bt_field_class_type fc_type;
1287
1288 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1289
1290 if (selector_fc) {
1291 BT_ASSERT_PRE_FC_IS_INT(selector_fc, "Selector field class");
1292 }
1293
1294 BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F",
1295 selector_fc);
1296
1297 if (selector_fc) {
1298 var_with_sel_fc = g_new0(
1299 struct bt_field_class_variant_with_selector, 1);
1300 if (!var_with_sel_fc) {
1301 BT_LIB_LOGE_APPEND_CAUSE(
1302 "Failed to allocate one variant field class with selector.");
1303 goto error;
1304 }
1305
1306 if (selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1307 selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) {
1308 fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR;
1309 } else {
1310 fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR;
1311 }
1312
1313 ret = init_named_field_classes_container(
1314 (void *) var_with_sel_fc, fc_type,
1315 destroy_variant_with_selector_field_class,
1316 destroy_variant_with_selector_option);
1317 if (ret) {
1318 /* init_named_field_classes_container() logs errors */
1319 goto error;
1320 }
1321
1322 var_with_sel_fc->selector_fc = selector_fc;
1323 bt_object_get_no_null_check(var_with_sel_fc->selector_fc);
1324 bt_field_class_freeze(selector_fc);
1325 var_fc = (void *) var_with_sel_fc;
1326 } else {
1327 var_fc = g_new0(struct bt_field_class_variant, 1);
1328 if (!var_fc) {
1329 BT_LIB_LOGE_APPEND_CAUSE(
1330 "Failed to allocate one variant field class without selector.");
1331 goto error;
1332 }
1333
1334 ret = init_named_field_classes_container((void *) var_fc,
1335 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR,
1336 destroy_variant_field_class, destroy_named_field_class);
1337 if (ret) {
1338 /* init_named_field_classes_container() logs errors */
1339 goto error;
1340 }
1341 }
1342
1343 BT_ASSERT(var_fc);
1344 BT_LIB_LOGD("Created default variant field class with selector object: "
1345 "%![var-fc-]+F, %![sel-fc-]+F", var_fc, selector_fc);
1346 goto end;
1347
1348 error:
1349 BT_OBJECT_PUT_REF_AND_RESET(var_fc);
1350
1351 end:
1352 return (void *) var_fc;
1353 }
1354
1355 enum bt_field_class_variant_without_selector_append_option_status
1356 bt_field_class_variant_without_selector_append_option(struct bt_field_class *fc,
1357 const char *name, struct bt_field_class *option_fc)
1358 {
1359 enum bt_field_class_variant_without_selector_append_option_status status;
1360 struct bt_named_field_class *named_fc = NULL;
1361
1362 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1363 BT_ASSERT_PRE_NON_NULL(name, "Name");
1364 BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class");
1365 BT_ASSERT_PRE_FC_HAS_ID(fc,
1366 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR, "Field class");
1367 named_fc = create_named_field_class(name, option_fc);
1368 if (!named_fc) {
1369 /* create_named_field_class() logs errors */
1370 status = BT_FUNC_STATUS_MEMORY_ERROR;
1371 goto end;
1372 }
1373
1374 status = append_named_field_class_to_container_field_class((void *) fc,
1375 named_fc);
1376 if (status == BT_FUNC_STATUS_OK) {
1377 /* Moved to the container */
1378 named_fc = NULL;
1379 }
1380
1381 end:
1382 if (named_fc) {
1383 destroy_named_field_class(named_fc);
1384 }
1385
1386 return status;
1387 }
1388
1389 static
1390 int ranges_overlap(GPtrArray *var_fc_opts, const struct bt_integer_range_set *range_set,
1391 bool is_signed, bool *has_overlap)
1392 {
1393 int status = BT_FUNC_STATUS_OK;
1394 struct bt_integer_range_set *full_range_set;
1395 uint64_t i;
1396
1397 *has_overlap = false;
1398
1399 /*
1400 * Build a single range set with all the ranges and test for
1401 * overlaps.
1402 */
1403 if (is_signed) {
1404 full_range_set = (void *) bt_integer_range_set_signed_create();
1405 } else {
1406 full_range_set = (void *) bt_integer_range_set_unsigned_create();
1407 }
1408
1409 if (!full_range_set) {
1410 BT_LOGE_STR("Failed to create a range set.");
1411 status = BT_FUNC_STATUS_MEMORY_ERROR;
1412 goto end;
1413 }
1414
1415 /* Add existing option ranges */
1416 for (i = 0; i < var_fc_opts->len; i++) {
1417 struct bt_field_class_variant_with_selector_option *opt =
1418 var_fc_opts->pdata[i];
1419 uint64_t j;
1420
1421 for (j = 0; j < opt->range_set->ranges->len; j++) {
1422 struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1423 opt->range_set, j);
1424
1425 if (is_signed) {
1426 status = bt_integer_range_set_signed_add_range(
1427 (void *) full_range_set, range->lower.i,
1428 range->upper.i);
1429 } else {
1430 status = bt_integer_range_set_unsigned_add_range(
1431 (void *) full_range_set, range->lower.u,
1432 range->upper.u);
1433 }
1434
1435 if (status) {
1436 goto end;
1437 }
1438 }
1439 }
1440
1441 /* Add new ranges */
1442 for (i = 0; i < range_set->ranges->len; i++) {
1443 struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1444 range_set, i);
1445
1446 if (is_signed) {
1447 status = bt_integer_range_set_signed_add_range(
1448 (void *) full_range_set, range->lower.i,
1449 range->upper.i);
1450 } else {
1451 status = bt_integer_range_set_unsigned_add_range(
1452 (void *) full_range_set, range->lower.u,
1453 range->upper.u);
1454 }
1455
1456 if (status) {
1457 goto end;
1458 }
1459 }
1460
1461 /* Check overlaps */
1462 if (is_signed) {
1463 *has_overlap = bt_integer_range_set_signed_has_overlaps(full_range_set);
1464 } else {
1465 *has_overlap = bt_integer_range_set_unsigned_has_overlaps(
1466 full_range_set);
1467 }
1468
1469 end:
1470 bt_object_put_ref(full_range_set);
1471 return status;
1472 }
1473
1474 static
1475 int append_option_to_variant_with_selector_field_class(
1476 struct bt_field_class *fc, const char *name,
1477 struct bt_field_class *option_fc,
1478 const struct bt_integer_range_set *range_set,
1479 enum bt_field_class_type expected_type)
1480 {
1481 int status;
1482 struct bt_field_class_variant_with_selector *var_fc = (void *) fc;
1483 struct bt_field_class_variant_with_selector_option *opt = NULL;
1484 bool has_overlap;
1485
1486 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1487 BT_ASSERT_PRE_NON_NULL(name, "Name");
1488 BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class");
1489 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
1490 BT_ASSERT_PRE_FC_HAS_ID(fc, expected_type, "Field class");
1491 BT_ASSERT_PRE(range_set->ranges->len > 0,
1492 "Integer range set is empty: %!+R", range_set);
1493 status = ranges_overlap(var_fc->common.common.named_fcs, range_set,
1494 expected_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR,
1495 &has_overlap);
1496 if (status) {
1497 /* ranges_overlap() logs errors */
1498 goto end;
1499 }
1500
1501 BT_ASSERT_PRE(!has_overlap,
1502 "Integer range set's ranges and existing ranges have an overlap: "
1503 "%!+R", range_set);
1504 opt = create_variant_with_selector_option(name, option_fc, range_set);
1505 if (!opt) {
1506 /* create_variant_with_selector_option() logs errors */
1507 status = BT_FUNC_STATUS_MEMORY_ERROR;
1508 goto end;
1509 }
1510
1511 status = append_named_field_class_to_container_field_class((void *) fc,
1512 &opt->common);
1513 if (status == BT_FUNC_STATUS_OK) {
1514 /* Moved to the container */
1515 opt = NULL;
1516 }
1517
1518 end:
1519 if (opt) {
1520 destroy_variant_with_selector_option(opt);
1521 }
1522
1523 return status;
1524 }
1525
1526 enum bt_field_class_variant_with_selector_append_option_status
1527 bt_field_class_variant_with_selector_unsigned_append_option(
1528 struct bt_field_class *fc, const char *name,
1529 struct bt_field_class *option_fc,
1530 const struct bt_integer_range_set_unsigned *range_set)
1531 {
1532 return append_option_to_variant_with_selector_field_class(fc,
1533 name, option_fc, (const void *) range_set,
1534 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR);
1535 }
1536
1537 enum bt_field_class_variant_with_selector_append_option_status
1538 bt_field_class_variant_with_selector_signed_append_option(
1539 struct bt_field_class *fc, const char *name,
1540 struct bt_field_class *option_fc,
1541 const struct bt_integer_range_set_signed *range_set)
1542 {
1543 return append_option_to_variant_with_selector_field_class(fc,
1544 name, option_fc, (const void *) range_set,
1545 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR);
1546 }
1547
1548 uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc)
1549 {
1550 const struct bt_field_class_variant *var_fc = (const void *) fc;
1551
1552 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1553 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1554 return (uint64_t) var_fc->common.named_fcs->len;
1555 }
1556
1557 const struct bt_field_class_variant_option *
1558 bt_field_class_variant_borrow_option_by_name_const(
1559 const struct bt_field_class *fc, const char *name)
1560 {
1561 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1562 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1563 return (const void *)
1564 borrow_named_field_class_from_container_field_class_by_name(
1565 (void *) fc, name);
1566 }
1567
1568 const struct bt_field_class_variant_option *
1569 bt_field_class_variant_borrow_option_by_index_const(
1570 const struct bt_field_class *fc, uint64_t index)
1571 {
1572 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1573 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1574 return (const void *)
1575 borrow_named_field_class_from_container_field_class_at_index(
1576 (void *) fc, index);
1577 }
1578
1579 struct bt_field_class_variant_option *
1580 bt_field_class_variant_borrow_option_by_name(
1581 struct bt_field_class *fc, const char *name)
1582 {
1583 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1584 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1585 return (void *)
1586 borrow_named_field_class_from_container_field_class_by_name(
1587 (void *) fc, name);
1588 }
1589
1590 struct bt_field_class_variant_option *
1591 bt_field_class_variant_borrow_option_by_index(
1592 struct bt_field_class *fc, uint64_t index)
1593 {
1594 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1595 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1596 return (void *)
1597 borrow_named_field_class_from_container_field_class_at_index(
1598 (void *) fc, index);
1599 }
1600
1601 const struct bt_field_class_variant_with_selector_unsigned_option *
1602 bt_field_class_variant_with_selector_unsigned_borrow_option_by_name_const(
1603 const struct bt_field_class *fc, const char *name)
1604 {
1605 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1606 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
1607 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR,
1608 "Field class");
1609 return (const void *)
1610 borrow_named_field_class_from_container_field_class_by_name(
1611 (void *) fc, name);
1612 }
1613
1614 const struct bt_field_class_variant_with_selector_unsigned_option *
1615 bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const(
1616 const struct bt_field_class *fc, uint64_t index)
1617 {
1618 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1619 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
1620 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR,
1621 "Field class");
1622 return (const void *)
1623 borrow_named_field_class_from_container_field_class_at_index(
1624 (void *) fc, index);
1625 }
1626
1627 const struct bt_field_class_variant_with_selector_signed_option *
1628 bt_field_class_variant_with_selector_signed_borrow_option_by_name_const(
1629 const struct bt_field_class *fc, const char *name)
1630 {
1631 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1632 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
1633 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR,
1634 "Field class");
1635 return (const void *)
1636 borrow_named_field_class_from_container_field_class_by_name(
1637 (void *) fc, name);
1638 }
1639
1640 const struct bt_field_class_variant_with_selector_signed_option *
1641 bt_field_class_variant_with_selector_signed_borrow_option_by_index_const(
1642 const struct bt_field_class *fc, uint64_t index)
1643 {
1644 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1645 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
1646 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR,
1647 "Field class");
1648 return (const void *)
1649 borrow_named_field_class_from_container_field_class_at_index(
1650 (void *) fc, index);
1651 }
1652
1653 const char *bt_field_class_variant_option_get_name(
1654 const struct bt_field_class_variant_option *option)
1655 {
1656 const struct bt_named_field_class *named_fc = (const void *) option;
1657
1658 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1659 return named_fc->name->str;
1660 }
1661
1662 const struct bt_field_class *
1663 bt_field_class_variant_option_borrow_field_class_const(
1664 const struct bt_field_class_variant_option *option)
1665 {
1666 const struct bt_named_field_class *named_fc = (const void *) option;
1667
1668 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1669 return named_fc->fc;
1670 }
1671
1672 struct bt_field_class *
1673 bt_field_class_variant_option_borrow_field_class(
1674 struct bt_field_class_variant_option *option)
1675 {
1676 struct bt_named_field_class *named_fc = (void *) option;
1677
1678 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1679 return named_fc->fc;
1680 }
1681
1682 const struct bt_integer_range_set_unsigned *
1683 bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const(
1684 const struct bt_field_class_variant_with_selector_unsigned_option *option)
1685 {
1686 const struct bt_field_class_variant_with_selector_option *opt =
1687 (const void *) option;
1688
1689 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1690 return (const void *) opt->range_set;
1691 }
1692
1693 const struct bt_integer_range_set_signed *
1694 bt_field_class_variant_with_selector_signed_option_borrow_ranges_const(
1695 const struct bt_field_class_variant_with_selector_signed_option *option)
1696 {
1697 const struct bt_field_class_variant_with_selector_option *opt =
1698 (const void *) option;
1699
1700 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1701 return (const void *) opt->range_set;
1702 }
1703
1704 const struct bt_field_path *
1705 bt_field_class_variant_with_selector_borrow_selector_field_path_const(
1706 const struct bt_field_class *fc)
1707 {
1708 const struct bt_field_class_variant_with_selector *var_fc =
1709 (const void *) fc;
1710
1711 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1712 BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc, "Field class");
1713 return var_fc->selector_field_path;
1714 }
1715
1716 static
1717 int init_array_field_class(struct bt_field_class_array *fc,
1718 enum bt_field_class_type type, bt_object_release_func release_func,
1719 struct bt_field_class *element_fc)
1720 {
1721 int ret;
1722
1723 BT_ASSERT(element_fc);
1724 ret = init_field_class((void *) fc, type, release_func);
1725 if (ret) {
1726 goto end;
1727 }
1728
1729 fc->element_fc = element_fc;
1730 bt_object_get_no_null_check(fc->element_fc);
1731 bt_field_class_freeze(element_fc);
1732
1733 end:
1734 return ret;
1735 }
1736
1737 static
1738 void finalize_array_field_class(struct bt_field_class_array *array_fc)
1739 {
1740 BT_ASSERT(array_fc);
1741 BT_LOGD_STR("Putting element field class.");
1742 finalize_field_class((void *) array_fc);
1743 BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc);
1744 }
1745
1746 static
1747 void destroy_static_array_field_class(struct bt_object *obj)
1748 {
1749 BT_ASSERT(obj);
1750 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj);
1751 finalize_array_field_class((void *) obj);
1752 g_free(obj);
1753 }
1754
1755 struct bt_field_class *
1756 bt_field_class_array_static_create(bt_trace_class *trace_class,
1757 struct bt_field_class *element_fc, uint64_t length)
1758 {
1759 struct bt_field_class_array_static *array_fc = NULL;
1760
1761 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1762 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1763 BT_LOGD_STR("Creating default static array field class object.");
1764 array_fc = g_new0(struct bt_field_class_array_static, 1);
1765 if (!array_fc) {
1766 BT_LIB_LOGE_APPEND_CAUSE(
1767 "Failed to allocate one static array field class.");
1768 goto error;
1769 }
1770
1771 if (init_array_field_class((void *) array_fc,
1772 BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
1773 destroy_static_array_field_class, element_fc)) {
1774 goto error;
1775 }
1776
1777 array_fc->length = length;
1778 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc);
1779 goto end;
1780
1781 error:
1782 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
1783
1784 end:
1785 return (void *) array_fc;
1786 }
1787
1788 const struct bt_field_class *
1789 bt_field_class_array_borrow_element_field_class_const(
1790 const struct bt_field_class *fc)
1791 {
1792 const struct bt_field_class_array *array_fc = (const void *) fc;
1793
1794 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1795 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
1796 return array_fc->element_fc;
1797 }
1798
1799 struct bt_field_class *
1800 bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc)
1801 {
1802 struct bt_field_class_array *array_fc = (void *) fc;
1803
1804 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1805 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
1806 return array_fc->element_fc;
1807 }
1808
1809 uint64_t bt_field_class_array_static_get_length(const struct bt_field_class *fc)
1810 {
1811 const struct bt_field_class_array_static *array_fc = (const void *) fc;
1812
1813 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1814 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
1815 "Field class");
1816 return (uint64_t) array_fc->length;
1817 }
1818
1819 static
1820 void destroy_dynamic_array_field_class(struct bt_object *obj)
1821 {
1822 struct bt_field_class_array_dynamic *fc = (void *) obj;
1823
1824 BT_ASSERT(fc);
1825 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc);
1826 finalize_array_field_class((void *) fc);
1827 BT_LOGD_STR("Putting length field path.");
1828 BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path);
1829 BT_LOGD_STR("Putting length field class.");
1830 BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc);
1831 g_free(fc);
1832 }
1833
1834 struct bt_field_class *bt_field_class_array_dynamic_create(
1835 struct bt_trace_class *trace_class,
1836 struct bt_field_class *element_fc,
1837 struct bt_field_class *length_fc)
1838 {
1839 struct bt_field_class_array_dynamic *array_fc = NULL;
1840
1841 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1842 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1843 BT_LOGD_STR("Creating default dynamic array field class object.");
1844 array_fc = g_new0(struct bt_field_class_array_dynamic, 1);
1845 if (!array_fc) {
1846 BT_LIB_LOGE_APPEND_CAUSE(
1847 "Failed to allocate one dynamic array field class.");
1848 goto error;
1849 }
1850
1851 if (init_array_field_class((void *) array_fc,
1852 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
1853 destroy_dynamic_array_field_class, element_fc)) {
1854 goto error;
1855 }
1856
1857 if (length_fc) {
1858 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc,
1859 "Length field class");
1860 array_fc->length_fc = length_fc;
1861 bt_object_get_no_null_check(array_fc->length_fc);
1862 bt_field_class_freeze(length_fc);
1863 }
1864
1865 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc);
1866 goto end;
1867
1868 error:
1869 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
1870
1871 end:
1872 return (void *) array_fc;
1873 }
1874
1875 const struct bt_field_path *
1876 bt_field_class_array_dynamic_borrow_length_field_path_const(
1877 const struct bt_field_class *fc)
1878 {
1879 const struct bt_field_class_array_dynamic *seq_fc = (const void *) fc;
1880
1881 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1882 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
1883 "Field class");
1884 return seq_fc->length_field_path;
1885 }
1886
1887 static
1888 void destroy_string_field_class(struct bt_object *obj)
1889 {
1890 BT_ASSERT(obj);
1891 BT_LIB_LOGD("Destroying string field class object: %!+F", obj);
1892 finalize_field_class((void *) obj);
1893 g_free(obj);
1894 }
1895
1896 struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class)
1897 {
1898 struct bt_field_class_string *string_fc = NULL;
1899
1900 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1901 BT_LOGD_STR("Creating default string field class object.");
1902 string_fc = g_new0(struct bt_field_class_string, 1);
1903 if (!string_fc) {
1904 BT_LIB_LOGE_APPEND_CAUSE(
1905 "Failed to allocate one string field class.");
1906 goto error;
1907 }
1908
1909 if (init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING,
1910 destroy_string_field_class)) {
1911 goto error;
1912 }
1913
1914 BT_LIB_LOGD("Created string field class object: %!+F", string_fc);
1915 goto end;
1916
1917 error:
1918 BT_OBJECT_PUT_REF_AND_RESET(string_fc);
1919
1920 end:
1921 return (void *) string_fc;
1922 }
1923
1924 BT_HIDDEN
1925 void _bt_field_class_freeze(const struct bt_field_class *c_fc)
1926 {
1927 struct bt_field_class *fc = (void *) c_fc;
1928
1929 /*
1930 * Element/member/option field classes are frozen when added to
1931 * their owner.
1932 */
1933 BT_ASSERT(fc);
1934 bt_value_freeze(fc->user_attributes);
1935 fc->frozen = true;
1936
1937 switch (fc->type) {
1938 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1939 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
1940 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
1941 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
1942 {
1943 struct bt_field_class_named_field_class_container *container_fc =
1944 (void *) fc;
1945 uint64_t i;
1946
1947 for (i = 0; i < container_fc->named_fcs->len; i++) {
1948 bt_named_field_class_freeze(
1949 container_fc->named_fcs->pdata[i]);
1950 }
1951
1952 break;
1953 }
1954 default:
1955 break;
1956 }
1957 }
1958
1959 BT_HIDDEN
1960 void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc)
1961 {
1962 BT_ASSERT(named_fc);
1963 BT_ASSERT(named_fc->fc->frozen);
1964 BT_LIB_LOGD("Freezing named field class's user attributes: %!+v",
1965 named_fc->user_attributes);
1966 bt_value_freeze(named_fc->user_attributes);
1967 ((struct bt_named_field_class *) named_fc)->frozen = true;
1968 }
1969
1970 BT_HIDDEN
1971 void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc)
1972 {
1973 struct bt_field_class *fc = (void *) c_fc;
1974
1975 BT_ASSERT(fc);
1976 BT_ASSERT_PRE(!fc->part_of_trace_class,
1977 "Field class is already part of a trace: %!+F", fc);
1978 fc->part_of_trace_class = true;
1979
1980 switch (fc->type) {
1981 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1982 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
1983 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
1984 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
1985 {
1986 struct bt_field_class_named_field_class_container *container_fc =
1987 (void *) fc;
1988 uint64_t i;
1989
1990 for (i = 0; i < container_fc->named_fcs->len; i++) {
1991 struct bt_named_field_class *named_fc =
1992 container_fc->named_fcs->pdata[i];
1993
1994 bt_field_class_make_part_of_trace_class(named_fc->fc);
1995 }
1996
1997 break;
1998 }
1999 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
2000 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
2001 {
2002 struct bt_field_class_array *array_fc = (void *) fc;
2003
2004 bt_field_class_make_part_of_trace_class(array_fc->element_fc);
2005 break;
2006 }
2007 default:
2008 break;
2009 }
2010 }
2011
2012 const struct bt_value *bt_field_class_borrow_user_attributes_const(
2013 const struct bt_field_class *fc)
2014 {
2015 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
2016 return fc->user_attributes;
2017 }
2018
2019 struct bt_value *bt_field_class_borrow_user_attributes(
2020 struct bt_field_class *field_class)
2021 {
2022 return (void *) bt_field_class_borrow_user_attributes_const(
2023 (void *) field_class);
2024 }
2025
2026
2027 void bt_field_class_set_user_attributes(
2028 struct bt_field_class *fc,
2029 const struct bt_value *user_attributes)
2030 {
2031 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
2032 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
2033 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
2034 "User attributes object is not a map value object.");
2035 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
2036 bt_object_put_no_null_check(fc->user_attributes);
2037 fc->user_attributes = (void *) user_attributes;
2038 bt_object_get_no_null_check(fc->user_attributes);
2039 }
2040
2041 static
2042 const struct bt_value *bt_named_field_class_borrow_user_attributes_const(
2043 const struct bt_named_field_class *named_fc)
2044 {
2045 return named_fc->user_attributes;
2046 }
2047
2048 static
2049 void bt_named_field_class_set_user_attributes(
2050 struct bt_named_field_class *named_fc,
2051 const struct bt_value *user_attributes)
2052 {
2053 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
2054 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
2055 "User attributes object is not a map value object.");
2056 BT_ASSERT_PRE_DEV_HOT(named_fc,
2057 "Structure field class member or variant field class option",
2058 ".");
2059 bt_object_put_no_null_check(named_fc->user_attributes);
2060 named_fc->user_attributes = (void *) user_attributes;
2061 bt_object_get_no_null_check(named_fc->user_attributes);
2062 }
2063
2064 const struct bt_value *
2065 bt_field_class_structure_member_borrow_user_attributes_const(
2066 const struct bt_field_class_structure_member *member)
2067 {
2068 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2069 return bt_named_field_class_borrow_user_attributes_const(
2070 (const void *) member);
2071 }
2072
2073 struct bt_value *
2074 bt_field_class_structure_member_borrow_user_attributes(
2075 struct bt_field_class_structure_member *member)
2076 {
2077 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2078 return (void *) bt_named_field_class_borrow_user_attributes_const(
2079 (void *) member);
2080 }
2081
2082 void bt_field_class_structure_member_set_user_attributes(
2083 struct bt_field_class_structure_member *member,
2084 const struct bt_value *user_attributes)
2085 {
2086 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2087 bt_named_field_class_set_user_attributes((void *) member,
2088 user_attributes);
2089 }
2090
2091 const struct bt_value *bt_field_class_variant_option_borrow_user_attributes_const(
2092 const struct bt_field_class_variant_option *option)
2093 {
2094 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2095 return bt_named_field_class_borrow_user_attributes_const(
2096 (const void *) option);
2097 }
2098
2099 struct bt_value *bt_field_class_variant_option_borrow_user_attributes(
2100 struct bt_field_class_variant_option *option)
2101 {
2102 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2103 return (void *) bt_named_field_class_borrow_user_attributes_const(
2104 (void *) option);
2105 }
2106
2107 void bt_field_class_variant_option_set_user_attributes(
2108 struct bt_field_class_variant_option *option,
2109 const struct bt_value *user_attributes)
2110 {
2111 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2112 bt_named_field_class_set_user_attributes((void *) option,
2113 user_attributes);
2114 }
2115
2116 void bt_field_class_get_ref(const struct bt_field_class *field_class)
2117 {
2118 bt_object_get_ref(field_class);
2119 }
2120
2121 void bt_field_class_put_ref(const struct bt_field_class *field_class)
2122 {
2123 bt_object_put_ref(field_class);
2124 }
This page took 0.233987 seconds and 3 git commands to generate.