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