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