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