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