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