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