bt2: add integer range set support
[babeltrace.git] / src / lib / trace-ir / field-class.c
CommitLineData
5cd6d0e5 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5cd6d0e5
PP
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5cd6d0e5
PP
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/FIELD-CLASS"
c2d9d9cf 25#include "lib/logging.h"
5cd6d0e5 26
578e048b 27#include "lib/assert-pre.h"
3fadfbc0
MJ
28#include <babeltrace2/trace-ir/field-class.h>
29#include <babeltrace2/trace-ir/field-class-const.h>
3fadfbc0
MJ
30#include <babeltrace2/trace-ir/field-const.h>
31#include <babeltrace2/trace-ir/field.h>
3fadfbc0 32#include <babeltrace2/trace-ir/clock-class.h>
578e048b
MJ
33#include "lib/object.h"
34#include "compat/compiler.h"
35#include "compat/endian.h"
36#include "common/assert.h"
37#include "compat/glib.h"
5cd6d0e5
PP
38#include <float.h>
39#include <inttypes.h>
40#include <stdlib.h>
41
578e048b
MJ
42#include "clock-class.h"
43#include "field-class.h"
44#include "field.h"
45#include "field-path.h"
46#include "utils.h"
d24d5663 47#include "lib/func-status.h"
578e048b 48
40f4ba76
PP
49enum bt_field_class_type bt_field_class_get_type(
50 const struct bt_field_class *fc)
5cd6d0e5 51{
bdb288b3 52 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
864cad70 53 return fc->type;
5cd6d0e5
PP
54}
55
56static
864cad70 57void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type,
5cd6d0e5
PP
58 bt_object_release_func release_func)
59{
60 BT_ASSERT(fc);
864cad70 61 BT_ASSERT(bt_field_class_has_known_type(fc));
5cd6d0e5
PP
62 BT_ASSERT(release_func);
63 bt_object_init_shared(&fc->base, release_func);
864cad70 64 fc->type = type;
5cd6d0e5
PP
65}
66
67static
864cad70
PP
68void init_integer_field_class(struct bt_field_class_integer *fc,
69 enum bt_field_class_type type,
5cd6d0e5
PP
70 bt_object_release_func release_func)
71{
864cad70 72 init_field_class((void *) fc, type, release_func);
5cd6d0e5
PP
73 fc->range = 64;
74 fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL;
75}
76
77static
78void destroy_integer_field_class(struct bt_object *obj)
79{
80 BT_ASSERT(obj);
e5be10ef 81 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj);
5cd6d0e5
PP
82 g_free(obj);
83}
84
85static inline
1122a43a
PP
86struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class,
87 enum bt_field_class_type type)
5cd6d0e5
PP
88{
89 struct bt_field_class_integer *int_fc = NULL;
90
1122a43a 91 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 92 BT_LOGD("Creating default integer field class object: type=%s",
864cad70 93 bt_common_field_class_type_string(type));
5cd6d0e5
PP
94 int_fc = g_new0(struct bt_field_class_integer, 1);
95 if (!int_fc) {
870631a2
PP
96 BT_LIB_LOGE_APPEND_CAUSE(
97 "Failed to allocate one integer field class.");
5cd6d0e5
PP
98 goto error;
99 }
100
864cad70 101 init_integer_field_class(int_fc, type, destroy_integer_field_class);
e5be10ef 102 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc);
5cd6d0e5
PP
103 goto end;
104
105error:
65300d60 106 BT_OBJECT_PUT_REF_AND_RESET(int_fc);
5cd6d0e5
PP
107
108end:
109 return (void *) int_fc;
110}
111
1122a43a
PP
112struct bt_field_class *bt_field_class_unsigned_integer_create(
113 bt_trace_class *trace_class)
5cd6d0e5 114{
1122a43a 115 return create_integer_field_class(trace_class,
e5be10ef 116 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
5cd6d0e5
PP
117}
118
1122a43a
PP
119struct bt_field_class *bt_field_class_signed_integer_create(
120 bt_trace_class *trace_class)
5cd6d0e5 121{
1122a43a 122 return create_integer_field_class(trace_class,
e5be10ef 123 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER);
5cd6d0e5
PP
124}
125
126uint64_t bt_field_class_integer_get_field_value_range(
40f4ba76 127 const struct bt_field_class *fc)
5cd6d0e5 128{
40f4ba76 129 const struct bt_field_class_integer *int_fc = (const void *) fc;
5cd6d0e5 130
bdb288b3
PP
131 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
132 BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class");
5cd6d0e5
PP
133 return int_fc->range;
134}
135
136BT_ASSERT_PRE_FUNC
137static
138bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc,
139 uint64_t size)
140{
141 // TODO
142 return true;
143}
144
40f4ba76
PP
145void bt_field_class_integer_set_field_value_range(
146 struct bt_field_class *fc, uint64_t size)
5cd6d0e5
PP
147{
148 struct bt_field_class_integer *int_fc = (void *) fc;
149
150 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
151 BT_ASSERT_PRE_FC_IS_INT(fc, "Field class");
bdb288b3 152 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
5cd6d0e5 153 BT_ASSERT_PRE(size <= 64,
e5be10ef 154 "Unsupported size for integer field class's field value range "
5cd6d0e5 155 "(maximum is 64): size=%" PRIu64, size);
864cad70
PP
156 BT_ASSERT_PRE(
157 int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
158 int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
5cd6d0e5 159 size_is_valid_for_enumeration_field_class(fc, size),
e5be10ef 160 "Invalid field value range for enumeration field class: "
5cd6d0e5
PP
161 "at least one of the current mapping ranges contains values "
162 "which are outside this range: %!+F, size=%" PRIu64, fc, size);
163 int_fc->range = size;
3f7d4d90 164 BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc);
5cd6d0e5
PP
165}
166
167enum bt_field_class_integer_preferred_display_base
40f4ba76 168bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc)
5cd6d0e5 169{
40f4ba76 170 const struct bt_field_class_integer *int_fc = (const void *) fc;
5cd6d0e5 171
bdb288b3
PP
172 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
173 BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class");
5cd6d0e5
PP
174 return int_fc->base;
175}
176
40f4ba76
PP
177void bt_field_class_integer_set_preferred_display_base(
178 struct bt_field_class *fc,
5cd6d0e5
PP
179 enum bt_field_class_integer_preferred_display_base base)
180{
181 struct bt_field_class_integer *int_fc = (void *) fc;
182
183 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
184 BT_ASSERT_PRE_FC_IS_INT(fc, "Field class");
bdb288b3 185 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
5cd6d0e5 186 int_fc->base = base;
3f7d4d90 187 BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc);
5cd6d0e5
PP
188}
189
190static
191void finalize_enumeration_field_class_mapping(
192 struct bt_field_class_enumeration_mapping *mapping)
193{
194 BT_ASSERT(mapping);
195
196 if (mapping->label) {
197 g_string_free(mapping->label, TRUE);
198 }
199
200 if (mapping->ranges) {
201 g_array_free(mapping->ranges, TRUE);
202 }
203}
204
205static
206void destroy_enumeration_field_class(struct bt_object *obj)
207{
208 struct bt_field_class_enumeration *fc = (void *) obj;
209
210 BT_ASSERT(fc);
e5be10ef 211 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc);
5cd6d0e5
PP
212
213 if (fc->mappings) {
214 uint64_t i;
215
216 for (i = 0; i < fc->mappings->len; i++) {
217 finalize_enumeration_field_class_mapping(
218 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i));
219 }
220
221 g_array_free(fc->mappings, TRUE);
238b7404 222 fc->mappings = NULL;
5cd6d0e5
PP
223 }
224
225 if (fc->label_buf) {
226 g_ptr_array_free(fc->label_buf, TRUE);
238b7404 227 fc->label_buf = NULL;
5cd6d0e5
PP
228 }
229
230 g_free(fc);
231}
232
233static
40f4ba76 234struct bt_field_class *create_enumeration_field_class(
1122a43a 235 bt_trace_class *trace_class, enum bt_field_class_type type)
5cd6d0e5
PP
236{
237 struct bt_field_class_enumeration *enum_fc = NULL;
238
1122a43a 239 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 240 BT_LOGD("Creating default enumeration field class object: type=%s",
864cad70 241 bt_common_field_class_type_string(type));
5cd6d0e5
PP
242 enum_fc = g_new0(struct bt_field_class_enumeration, 1);
243 if (!enum_fc) {
870631a2
PP
244 BT_LIB_LOGE_APPEND_CAUSE(
245 "Failed to allocate one enumeration field class.");
5cd6d0e5
PP
246 goto error;
247 }
248
864cad70 249 init_integer_field_class((void *) enum_fc, type,
5cd6d0e5
PP
250 destroy_enumeration_field_class);
251 enum_fc->mappings = g_array_new(FALSE, TRUE,
252 sizeof(struct bt_field_class_enumeration_mapping));
253 if (!enum_fc->mappings) {
870631a2 254 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
5cd6d0e5
PP
255 goto error;
256 }
257
258 enum_fc->label_buf = g_ptr_array_new();
259 if (!enum_fc->label_buf) {
870631a2 260 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
5cd6d0e5
PP
261 goto error;
262 }
263
e5be10ef 264 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc);
5cd6d0e5
PP
265 goto end;
266
267error:
65300d60 268 BT_OBJECT_PUT_REF_AND_RESET(enum_fc);
5cd6d0e5
PP
269
270end:
271 return (void *) enum_fc;
272}
273
1122a43a
PP
274struct bt_field_class *bt_field_class_unsigned_enumeration_create(
275 bt_trace_class *trace_class)
5cd6d0e5 276{
1122a43a 277 return create_enumeration_field_class(trace_class,
864cad70 278 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
5cd6d0e5
PP
279}
280
1122a43a
PP
281struct bt_field_class *bt_field_class_signed_enumeration_create(
282 bt_trace_class *trace_class)
5cd6d0e5 283{
1122a43a 284 return create_enumeration_field_class(trace_class,
864cad70 285 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
5cd6d0e5
PP
286}
287
40f4ba76
PP
288uint64_t bt_field_class_enumeration_get_mapping_count(
289 const struct bt_field_class *fc)
5cd6d0e5 290{
40f4ba76 291 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5 292
bdb288b3
PP
293 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
294 BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc, "Field class");
5cd6d0e5
PP
295 return (uint64_t) enum_fc->mappings->len;
296}
297
8f3ccfbc
PP
298const struct bt_field_class_unsigned_enumeration_mapping *
299bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const(
300 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 301{
40f4ba76 302 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5 303
bdb288b3
PP
304 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
305 BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len);
306 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5 307 "Field class");
8f3ccfbc 308 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
5cd6d0e5
PP
309}
310
8f3ccfbc
PP
311const struct bt_field_class_signed_enumeration_mapping *
312bt_field_class_signed_enumeration_borrow_mapping_by_index_const(
313 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 314{
40f4ba76 315 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5 316
bdb288b3
PP
317 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
318 BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len);
319 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5 320 "Field class");
8f3ccfbc 321 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
5cd6d0e5
PP
322}
323
8f3ccfbc 324const char *bt_field_class_enumeration_mapping_get_label(
40f4ba76 325 const struct bt_field_class_enumeration_mapping *mapping)
5cd6d0e5 326{
bdb288b3 327 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
8f3ccfbc 328 return mapping->label->str;
5cd6d0e5
PP
329}
330
8f3ccfbc
PP
331uint64_t bt_field_class_enumeration_mapping_get_range_count(
332 const struct bt_field_class_enumeration_mapping *mapping)
5cd6d0e5 333{
bdb288b3 334 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
8f3ccfbc 335 return (uint64_t) mapping->ranges->len;
5cd6d0e5
PP
336}
337
338static inline
339void get_enumeration_field_class_mapping_range_at_index(
40f4ba76 340 const struct bt_field_class_enumeration_mapping *mapping,
5cd6d0e5
PP
341 uint64_t index, uint64_t *lower, uint64_t *upper)
342{
40f4ba76 343 const struct bt_field_class_enumeration_mapping_range *range;
5cd6d0e5 344
bdb288b3
PP
345 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Ranges");
346 BT_ASSERT_PRE_DEV_NON_NULL(lower, "Range's lower (output)");
347 BT_ASSERT_PRE_DEV_NON_NULL(upper, "Range's upper (output)");
348 BT_ASSERT_PRE_DEV_VALID_INDEX(index, mapping->ranges->len);
5cd6d0e5
PP
349 range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, index);
350 *lower = range->lower.u;
351 *upper = range->upper.u;
352}
353
8f3ccfbc
PP
354void bt_field_class_unsigned_enumeration_mapping_get_range_by_index(
355 const struct bt_field_class_unsigned_enumeration_mapping *ranges,
5cd6d0e5
PP
356 uint64_t index, uint64_t *lower, uint64_t *upper)
357{
40f4ba76
PP
358 get_enumeration_field_class_mapping_range_at_index(
359 (const void *) ranges, index, lower, upper);
5cd6d0e5
PP
360}
361
8f3ccfbc
PP
362void bt_field_class_signed_enumeration_mapping_get_range_by_index(
363 const struct bt_field_class_signed_enumeration_mapping *ranges,
5cd6d0e5
PP
364 uint64_t index, int64_t *lower, int64_t *upper)
365{
40f4ba76
PP
366 get_enumeration_field_class_mapping_range_at_index(
367 (const void *) ranges, index,
368 (uint64_t *) lower, (uint64_t *) upper);
5cd6d0e5
PP
369}
370
185ecf64
PP
371enum bt_field_class_enumeration_get_mapping_labels_for_value_status
372bt_field_class_unsigned_enumeration_get_mapping_labels_for_value(
40f4ba76 373 const struct bt_field_class *fc, uint64_t value,
5cd6d0e5
PP
374 bt_field_class_enumeration_mapping_label_array *label_array,
375 uint64_t *count)
376{
40f4ba76 377 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
378 uint64_t i;
379
bdb288b3
PP
380 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
381 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
382 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
383 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5
PP
384 "Field class");
385 g_ptr_array_set_size(enum_fc->label_buf, 0);
386
387 for (i = 0; i < enum_fc->mappings->len; i++) {
388 uint64_t j;
40f4ba76 389 const struct bt_field_class_enumeration_mapping *mapping =
5cd6d0e5
PP
390 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
391
392 for (j = 0; j < mapping->ranges->len; j++) {
40f4ba76 393 const struct bt_field_class_enumeration_mapping_range *range =
5cd6d0e5
PP
394 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
395 mapping, j);
396
397 if (value >= range->lower.u &&
398 value <= range->upper.u) {
399 g_ptr_array_add(enum_fc->label_buf,
400 mapping->label->str);
401 break;
402 }
403 }
404 }
405
406 *label_array = (void *) enum_fc->label_buf->pdata;
407 *count = (uint64_t) enum_fc->label_buf->len;
d24d5663 408 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
409}
410
185ecf64
PP
411enum bt_field_class_enumeration_get_mapping_labels_for_value_status
412bt_field_class_signed_enumeration_get_mapping_labels_for_value(
40f4ba76 413 const struct bt_field_class *fc, int64_t value,
5cd6d0e5
PP
414 bt_field_class_enumeration_mapping_label_array *label_array,
415 uint64_t *count)
416{
40f4ba76 417 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
418 uint64_t i;
419
bdb288b3
PP
420 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
421 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
422 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
423 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5
PP
424 "Field class");
425 g_ptr_array_set_size(enum_fc->label_buf, 0);
426
427 for (i = 0; i < enum_fc->mappings->len; i++) {
428 uint64_t j;
40f4ba76 429 const struct bt_field_class_enumeration_mapping *mapping =
5cd6d0e5
PP
430 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
431
432 for (j = 0; j < mapping->ranges->len; j++) {
40f4ba76 433 const struct bt_field_class_enumeration_mapping_range *range =
5cd6d0e5
PP
434 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
435 mapping, j);
436
437 if (value >= range->lower.i &&
438 value <= range->upper.i) {
439 g_ptr_array_add(enum_fc->label_buf,
440 mapping->label->str);
441 break;
442 }
443 }
444 }
445
446 *label_array = (void *) enum_fc->label_buf->pdata;
447 *count = (uint64_t) enum_fc->label_buf->len;
d24d5663 448 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
449}
450
451static inline
d24d5663
PP
452enum bt_field_class_enumeration_map_range_status
453add_mapping_to_enumeration_field_class(
4295b9e0 454 struct bt_field_class *fc,
5cd6d0e5
PP
455 const char *label, uint64_t lower, uint64_t upper)
456{
d24d5663 457 int ret = BT_FUNC_STATUS_OK;
5cd6d0e5
PP
458 uint64_t i;
459 struct bt_field_class_enumeration *enum_fc = (void *) fc;
460 struct bt_field_class_enumeration_mapping *mapping = NULL;
461 struct bt_field_class_enumeration_mapping_range *range;
462
463 BT_ASSERT(fc);
464 BT_ASSERT_PRE_NON_NULL(label, "Label");
465
466 /* Find existing mapping identified by this label */
467 for (i = 0; i < enum_fc->mappings->len; i++) {
468 struct bt_field_class_enumeration_mapping *mapping_candidate =
469 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
470
471 if (strcmp(mapping_candidate->label->str, label) == 0) {
472 mapping = mapping_candidate;
473 break;
474 }
475 }
476
477 if (!mapping) {
478 /* Create new mapping for this label */
479 g_array_set_size(enum_fc->mappings, enum_fc->mappings->len + 1);
480 mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc,
481 enum_fc->mappings->len - 1);
482 mapping->ranges = g_array_new(FALSE, TRUE,
483 sizeof(struct bt_field_class_enumeration_mapping_range));
484 if (!mapping->ranges) {
485 finalize_enumeration_field_class_mapping(mapping);
486 g_array_set_size(enum_fc->mappings,
487 enum_fc->mappings->len - 1);
d24d5663 488 ret = BT_FUNC_STATUS_MEMORY_ERROR;
5cd6d0e5
PP
489 goto end;
490 }
491
492 mapping->label = g_string_new(label);
493 if (!mapping->label) {
494 finalize_enumeration_field_class_mapping(mapping);
495 g_array_set_size(enum_fc->mappings,
496 enum_fc->mappings->len - 1);
d24d5663 497 ret = BT_FUNC_STATUS_MEMORY_ERROR;
5cd6d0e5
PP
498 goto end;
499 }
500 }
501
502 /* Add range */
503 BT_ASSERT(mapping);
504 g_array_set_size(mapping->ranges, mapping->ranges->len + 1);
505 range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping,
506 mapping->ranges->len - 1);
507 range->lower.u = lower;
508 range->upper.u = upper;
3f7d4d90 509 BT_LIB_LOGD("Added mapping to enumeration field class: "
5cd6d0e5
PP
510 "%![fc-]+F, label=\"%s\", lower-unsigned=%" PRIu64 ", "
511 "upper-unsigned=%" PRIu64, fc, label, lower, upper);
512
513end:
514 return ret;
515}
516
bdb288b3
PP
517enum bt_field_class_enumeration_map_range_status
518bt_field_class_unsigned_enumeration_map_range(
40f4ba76 519 struct bt_field_class *fc, const char *label,
5cd6d0e5
PP
520 uint64_t range_lower, uint64_t range_upper)
521{
522 struct bt_field_class_enumeration *enum_fc = (void *) fc;
523
524 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 525 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5
PP
526 "Field class");
527 BT_ASSERT_PRE(range_lower <= range_upper,
528 "Range's upper bound is less than lower bound: "
529 "upper=%" PRIu64 ", lower=%" PRIu64,
530 range_lower, range_upper);
531 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range,
532 range_lower),
e5be10ef 533 "Range's lower bound is outside the enumeration field class's value range: "
5cd6d0e5
PP
534 "%![fc-]+F, lower=%" PRIu64, fc, range_lower);
535 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range,
536 range_upper),
e5be10ef 537 "Range's upper bound is outside the enumeration field class's value range: "
5cd6d0e5
PP
538 "%![fc-]+F, upper=%" PRIu64, fc, range_upper);
539 return add_mapping_to_enumeration_field_class(fc, label, range_lower,
540 range_upper);
541}
542
bdb288b3
PP
543enum bt_field_class_enumeration_map_range_status
544bt_field_class_signed_enumeration_map_range(
40f4ba76 545 struct bt_field_class *fc, const char *label,
5cd6d0e5
PP
546 int64_t range_lower, int64_t range_upper)
547{
548 struct bt_field_class_enumeration *enum_fc = (void *) fc;
549
550 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 551 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5
PP
552 "Field class");
553 BT_ASSERT_PRE(range_lower <= range_upper,
554 "Range's upper bound is less than lower bound: "
555 "upper=%" PRId64 ", lower=%" PRId64,
556 range_lower, range_upper);
557 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range,
558 range_lower),
e5be10ef 559 "Range's lower bound is outside the enumeration field class's value range: "
5cd6d0e5
PP
560 "%![fc-]+F, lower=%" PRId64, fc, range_lower);
561 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range,
562 range_upper),
e5be10ef 563 "Range's upper bound is outside the enumeration field class's value range: "
5cd6d0e5
PP
564 "%![fc-]+F, upper=%" PRId64, fc, range_upper);
565 return add_mapping_to_enumeration_field_class(fc, label, range_lower,
566 range_upper);
567}
568
569static
570void destroy_real_field_class(struct bt_object *obj)
571{
572 BT_ASSERT(obj);
e5be10ef 573 BT_LIB_LOGD("Destroying real field class object: %!+F", obj);
5cd6d0e5
PP
574 g_free(obj);
575}
576
1122a43a 577struct bt_field_class *bt_field_class_real_create(bt_trace_class *trace_class)
5cd6d0e5
PP
578{
579 struct bt_field_class_real *real_fc = NULL;
580
1122a43a 581 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 582 BT_LOGD_STR("Creating default real field class object.");
5cd6d0e5
PP
583 real_fc = g_new0(struct bt_field_class_real, 1);
584 if (!real_fc) {
870631a2 585 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
5cd6d0e5
PP
586 goto error;
587 }
588
864cad70 589 init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL,
5cd6d0e5 590 destroy_real_field_class);
e5be10ef 591 BT_LIB_LOGD("Created real field class object: %!+F", real_fc);
5cd6d0e5
PP
592 goto end;
593
594error:
65300d60 595 BT_OBJECT_PUT_REF_AND_RESET(real_fc);
5cd6d0e5
PP
596
597end:
598 return (void *) real_fc;
599}
600
40f4ba76 601bt_bool bt_field_class_real_is_single_precision(const struct bt_field_class *fc)
5cd6d0e5 602{
40f4ba76 603 const struct bt_field_class_real *real_fc = (const void *) fc;
5cd6d0e5 604
bdb288b3
PP
605 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
606 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class");
5cd6d0e5
PP
607 return real_fc->is_single_precision;
608}
609
40f4ba76 610void bt_field_class_real_set_is_single_precision(struct bt_field_class *fc,
5cd6d0e5
PP
611 bt_bool is_single_precision)
612{
613 struct bt_field_class_real *real_fc = (void *) fc;
614
615 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 616 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class");
bdb288b3 617 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
5cd6d0e5 618 real_fc->is_single_precision = (bool) is_single_precision;
3f7d4d90 619 BT_LIB_LOGD("Set real field class's \"is single precision\" property: "
5cd6d0e5 620 "%!+F", fc);
5cd6d0e5
PP
621}
622
623static
624int init_named_field_classes_container(
625 struct bt_field_class_named_field_class_container *fc,
40f4ba76
PP
626 enum bt_field_class_type type,
627 bt_object_release_func release_func)
5cd6d0e5
PP
628{
629 int ret = 0;
630
864cad70 631 init_field_class((void *) fc, type, release_func);
5cd6d0e5
PP
632 fc->named_fcs = g_array_new(FALSE, TRUE,
633 sizeof(struct bt_named_field_class));
634 if (!fc->named_fcs) {
870631a2 635 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
5cd6d0e5
PP
636 ret = -1;
637 goto end;
638 }
639
640 fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal);
641 if (!fc->name_to_index) {
870631a2 642 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
5cd6d0e5
PP
643 ret = -1;
644 goto end;
645 }
646
647end:
648 return ret;
649}
650
651static
652void finalize_named_field_class(struct bt_named_field_class *named_fc)
653{
654 BT_ASSERT(named_fc);
e5be10ef 655 BT_LIB_LOGD("Finalizing named field class: "
5cd6d0e5
PP
656 "addr=%p, name=\"%s\", %![fc-]+F",
657 named_fc, named_fc->name ? named_fc->name->str : NULL,
658 named_fc->fc);
659
660 if (named_fc->name) {
661 g_string_free(named_fc->name, TRUE);
662 }
663
e5be10ef 664 BT_LOGD_STR("Putting named field class's field class.");
238b7404 665 BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc);
5cd6d0e5
PP
666}
667
668static
669void finalize_named_field_classes_container(
670 struct bt_field_class_named_field_class_container *fc)
671{
672 uint64_t i;
673
674 BT_ASSERT(fc);
675
676 if (fc->named_fcs) {
677 for (i = 0; i < fc->named_fcs->len; i++) {
678 finalize_named_field_class(
679 &g_array_index(fc->named_fcs,
680 struct bt_named_field_class, i));
681 }
682
683 g_array_free(fc->named_fcs, TRUE);
684 }
685
686 if (fc->name_to_index) {
687 g_hash_table_destroy(fc->name_to_index);
688 }
689}
690
691static
692void destroy_structure_field_class(struct bt_object *obj)
693{
694 BT_ASSERT(obj);
1998d1ab 695 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj);
5cd6d0e5
PP
696 finalize_named_field_classes_container((void *) obj);
697 g_free(obj);
698}
699
1122a43a
PP
700struct bt_field_class *bt_field_class_structure_create(
701 bt_trace_class *trace_class)
5cd6d0e5
PP
702{
703 int ret;
704 struct bt_field_class_structure *struct_fc = NULL;
705
1122a43a 706 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 707 BT_LOGD_STR("Creating default structure field class object.");
5cd6d0e5
PP
708 struct_fc = g_new0(struct bt_field_class_structure, 1);
709 if (!struct_fc) {
870631a2
PP
710 BT_LIB_LOGE_APPEND_CAUSE(
711 "Failed to allocate one structure field class.");
5cd6d0e5
PP
712 goto error;
713 }
714
715 ret = init_named_field_classes_container((void *) struct_fc,
864cad70 716 BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class);
5cd6d0e5
PP
717 if (ret) {
718 goto error;
719 }
720
e5be10ef 721 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc);
5cd6d0e5
PP
722 goto end;
723
724error:
65300d60 725 BT_OBJECT_PUT_REF_AND_RESET(struct_fc);
5cd6d0e5
PP
726
727end:
728 return (void *) struct_fc;
729}
730
731static
d24d5663 732int append_named_field_class_to_container_field_class(
5cd6d0e5
PP
733 struct bt_field_class_named_field_class_container *container_fc,
734 const char *name, struct bt_field_class *fc)
735{
d24d5663 736 int ret = BT_FUNC_STATUS_OK;
5cd6d0e5
PP
737 struct bt_named_field_class *named_fc;
738 GString *name_str;
739
740 BT_ASSERT(container_fc);
bdb288b3 741 BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class");
5cd6d0e5
PP
742 BT_ASSERT_PRE_NON_NULL(name, "Name");
743 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
744 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index,
745 name),
e5be10ef 746 "Duplicate member/option name in structure/variant field class: "
5cd6d0e5
PP
747 "%![container-fc-]+F, name=\"%s\"", container_fc, name);
748 name_str = g_string_new(name);
749 if (!name_str) {
870631a2 750 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
d24d5663 751 ret = BT_FUNC_STATUS_MEMORY_ERROR;
5cd6d0e5
PP
752 goto end;
753 }
754
755 g_array_set_size(container_fc->named_fcs,
756 container_fc->named_fcs->len + 1);
757 named_fc = &g_array_index(container_fc->named_fcs,
758 struct bt_named_field_class, container_fc->named_fcs->len - 1);
759 named_fc->name = name_str;
398454ed
PP
760 named_fc->fc = fc;
761 bt_object_get_no_null_check(fc);
5cd6d0e5
PP
762 g_hash_table_insert(container_fc->name_to_index, named_fc->name->str,
763 GUINT_TO_POINTER(container_fc->named_fcs->len - 1));
1e6fd1d7
PP
764
765 /*
766 * Freeze the field class, but not the named field class (the
767 * user can still modify it, if possible, until the container
768 * itself is frozen).
769 */
5cd6d0e5
PP
770 bt_field_class_freeze(fc);
771
772end:
773 return ret;
774}
775
d24d5663
PP
776enum bt_field_class_structure_append_member_status
777bt_field_class_structure_append_member(
4295b9e0
PP
778 struct bt_field_class *fc, const char *name,
779 struct bt_field_class *member_fc)
5cd6d0e5 780{
e5be10ef 781
5cd6d0e5 782 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
740faaf4
PP
783 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
784 "Field class");
5cd6d0e5 785 return append_named_field_class_to_container_field_class((void *) fc,
40f4ba76 786 name, member_fc);
5cd6d0e5
PP
787}
788
40f4ba76
PP
789uint64_t bt_field_class_structure_get_member_count(
790 const struct bt_field_class *fc)
5cd6d0e5
PP
791{
792 struct bt_field_class_structure *struct_fc = (void *) fc;
793
bdb288b3
PP
794 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
795 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 796 "Field class");
5cd6d0e5
PP
797 return (uint64_t) struct_fc->common.named_fcs->len;
798}
799
800static
1e6fd1d7
PP
801struct bt_named_field_class *
802borrow_named_field_class_from_container_field_class_at_index(
740faaf4 803 struct bt_field_class_named_field_class_container *fc,
1e6fd1d7 804 uint64_t index)
5cd6d0e5 805{
5cd6d0e5 806 BT_ASSERT(fc);
bdb288b3 807 BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len);
1e6fd1d7 808 return BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, index);
5cd6d0e5
PP
809}
810
1e6fd1d7
PP
811const struct bt_field_class_structure_member *
812bt_field_class_structure_borrow_member_by_index_const(
813 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 814{
bdb288b3
PP
815 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
816 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 817 "Field class");
1e6fd1d7
PP
818 return (const void *)
819 borrow_named_field_class_from_container_field_class_at_index(
820 (void *) fc, index);
740faaf4
PP
821}
822
1e6fd1d7
PP
823struct bt_field_class_structure_member *
824bt_field_class_structure_borrow_member_by_index(
825 struct bt_field_class *fc, uint64_t index)
740faaf4 826{
bdb288b3
PP
827 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
828 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 829 "Field class");
1e6fd1d7
PP
830 return (void *)
831 borrow_named_field_class_from_container_field_class_at_index(
832 (void *) fc, index);
e5be10ef
PP
833}
834
5cd6d0e5 835static
1e6fd1d7
PP
836struct bt_named_field_class *
837borrow_named_field_class_from_container_field_class_by_name(
740faaf4 838 struct bt_field_class_named_field_class_container *fc,
5cd6d0e5
PP
839 const char *name)
840{
1e6fd1d7 841 struct bt_named_field_class *named_fc = NULL;
5cd6d0e5
PP
842 gpointer orig_key;
843 gpointer value;
844
845 BT_ASSERT(fc);
bdb288b3 846 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
5cd6d0e5
PP
847 if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key,
848 &value)) {
849 goto end;
850 }
851
852 named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc,
853 GPOINTER_TO_UINT(value));
5cd6d0e5
PP
854
855end:
1e6fd1d7 856 return named_fc;
5cd6d0e5
PP
857}
858
1e6fd1d7 859const struct bt_field_class_structure_member *
7c06e353 860bt_field_class_structure_borrow_member_by_name_const(
40f4ba76 861 const struct bt_field_class *fc, const char *name)
5cd6d0e5 862{
bdb288b3
PP
863 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
864 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 865 "Field class");
1e6fd1d7
PP
866 return (const void *)
867 borrow_named_field_class_from_container_field_class_by_name(
740faaf4
PP
868 (void *) fc, name);
869}
870
1e6fd1d7 871struct bt_field_class_structure_member *
fffedc87 872bt_field_class_structure_borrow_member_by_name(
740faaf4
PP
873 struct bt_field_class *fc, const char *name)
874{
bdb288b3
PP
875 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
876 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 877 "Field class");
1e6fd1d7
PP
878 return (void *)
879 borrow_named_field_class_from_container_field_class_by_name(
40f4ba76 880 (void *) fc, name);
e5be10ef
PP
881}
882
1e6fd1d7
PP
883const char *bt_field_class_structure_member_get_name(
884 const struct bt_field_class_structure_member *member)
885{
886 const struct bt_named_field_class *named_fc = (const void *) member;
887
bdb288b3 888 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1e6fd1d7
PP
889 return named_fc->name->str;
890}
891
892const struct bt_field_class *
893bt_field_class_structure_member_borrow_field_class_const(
894 const struct bt_field_class_structure_member *member)
895{
896 const struct bt_named_field_class *named_fc = (const void *) member;
897
bdb288b3 898 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1e6fd1d7
PP
899 return named_fc->fc;
900}
901
902struct bt_field_class *
903bt_field_class_structure_member_borrow_field_class(
904 struct bt_field_class_structure_member *member)
905{
906 struct bt_named_field_class *named_fc = (void *) member;
907
bdb288b3 908 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1e6fd1d7
PP
909 return named_fc->fc;
910}
911
5cd6d0e5
PP
912static
913void destroy_variant_field_class(struct bt_object *obj)
914{
915 struct bt_field_class_variant *fc = (void *) obj;
916
917 BT_ASSERT(fc);
e5be10ef 918 BT_LIB_LOGD("Destroying variant field class object: %!+F", fc);
5cd6d0e5
PP
919 finalize_named_field_classes_container((void *) fc);
920 BT_LOGD_STR("Putting selector field path.");
238b7404
PP
921 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path);
922 BT_LOGD_STR("Putting selector field class.");
923 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc);
5cd6d0e5
PP
924 g_free(fc);
925}
926
1122a43a
PP
927struct bt_field_class *bt_field_class_variant_create(
928 bt_trace_class *trace_class)
5cd6d0e5
PP
929{
930 int ret;
931 struct bt_field_class_variant *var_fc = NULL;
932
1122a43a 933 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 934 BT_LOGD_STR("Creating default variant field class object.");
5cd6d0e5
PP
935 var_fc = g_new0(struct bt_field_class_variant, 1);
936 if (!var_fc) {
870631a2
PP
937 BT_LIB_LOGE_APPEND_CAUSE(
938 "Failed to allocate one variant field class.");
5cd6d0e5
PP
939 goto error;
940 }
941
942 ret = init_named_field_classes_container((void *) var_fc,
864cad70 943 BT_FIELD_CLASS_TYPE_VARIANT, destroy_variant_field_class);
5cd6d0e5
PP
944 if (ret) {
945 goto error;
946 }
947
e5be10ef 948 BT_LIB_LOGD("Created variant field class object: %!+F", var_fc);
5cd6d0e5
PP
949 goto end;
950
951error:
65300d60 952 BT_OBJECT_PUT_REF_AND_RESET(var_fc);
5cd6d0e5
PP
953
954end:
955 return (void *) var_fc;
956}
957
d24d5663
PP
958enum bt_field_class_variant_set_selector_field_class_status
959bt_field_class_variant_set_selector_field_class(
40f4ba76
PP
960 struct bt_field_class *fc,
961 struct bt_field_class *selector_fc)
5cd6d0e5
PP
962{
963 struct bt_field_class_variant *var_fc = (void *) fc;
964
e5be10ef
PP
965 BT_ASSERT_PRE_NON_NULL(fc, "Variant field class");
966 BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class");
864cad70 967 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
e5be10ef 968 BT_ASSERT_PRE_FC_IS_ENUM(selector_fc, "Selector field class");
bdb288b3 969 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Variant field class");
40f4ba76 970 var_fc->selector_fc = selector_fc;
398454ed 971 bt_object_get_no_null_check(selector_fc);
40f4ba76 972 bt_field_class_freeze(selector_fc);
d24d5663 973 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
974}
975
d24d5663
PP
976enum bt_field_class_variant_append_option_status
977bt_field_class_variant_append_option(
40f4ba76
PP
978 struct bt_field_class *fc,
979 const char *name, struct bt_field_class *option_fc)
5cd6d0e5 980{
e5be10ef 981
5cd6d0e5 982 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 983 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
5cd6d0e5 984 return append_named_field_class_to_container_field_class((void *) fc,
40f4ba76 985 name, option_fc);
5cd6d0e5
PP
986}
987
1e6fd1d7 988const struct bt_field_class_variant_option *
7c06e353 989bt_field_class_variant_borrow_option_by_name_const(
40f4ba76 990 const struct bt_field_class *fc, const char *name)
5cd6d0e5 991{
bdb288b3
PP
992 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
993 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT,
994 "Field class");
1e6fd1d7
PP
995 return (const void *)
996 borrow_named_field_class_from_container_field_class_by_name(
997 (void *) fc, name);
740faaf4
PP
998}
999
1e6fd1d7 1000struct bt_field_class_variant_option *
7c06e353 1001bt_field_class_variant_borrow_option_by_name(
740faaf4
PP
1002 struct bt_field_class *fc, const char *name)
1003{
bdb288b3
PP
1004 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1005 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
1e6fd1d7
PP
1006 return (void *)
1007 borrow_named_field_class_from_container_field_class_by_name(
1008 (void *) fc, name);
e5be10ef
PP
1009}
1010
40f4ba76 1011uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc)
5cd6d0e5 1012{
40f4ba76 1013 const struct bt_field_class_variant *var_fc = (const void *) fc;
5cd6d0e5 1014
bdb288b3
PP
1015 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1016 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
5cd6d0e5
PP
1017 return (uint64_t) var_fc->common.named_fcs->len;
1018}
1019
1e6fd1d7
PP
1020const struct bt_field_class_variant_option *
1021bt_field_class_variant_borrow_option_by_index_const(
1022 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 1023{
bdb288b3
PP
1024 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1025 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
1e6fd1d7
PP
1026 return (const void *)
1027 borrow_named_field_class_from_container_field_class_at_index(
1028 (void *) fc, index);
740faaf4
PP
1029}
1030
1e6fd1d7
PP
1031struct bt_field_class_variant_option *
1032bt_field_class_variant_borrow_option_by_index(
1033 struct bt_field_class *fc, uint64_t index)
740faaf4 1034{
bdb288b3
PP
1035 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1036 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
1e6fd1d7
PP
1037 return (void *)
1038 borrow_named_field_class_from_container_field_class_at_index(
1039 (void *) fc, index);
1040}
1041
1042const char *bt_field_class_variant_option_get_name(
1043 const struct bt_field_class_variant_option *option)
1044{
1045 const struct bt_named_field_class *named_fc = (const void *) option;
1046
bdb288b3 1047 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1e6fd1d7
PP
1048 return named_fc->name->str;
1049}
1050
1051const struct bt_field_class *
1052bt_field_class_variant_option_borrow_field_class_const(
1053 const struct bt_field_class_variant_option *option)
1054{
1055 const struct bt_named_field_class *named_fc = (const void *) option;
1056
bdb288b3 1057 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1e6fd1d7
PP
1058 return named_fc->fc;
1059}
1060
1061struct bt_field_class *
1062bt_field_class_variant_option_borrow_field_class(
1063 struct bt_field_class_variant_option *option)
1064{
1065 struct bt_named_field_class *named_fc = (void *) option;
1066
bdb288b3 1067 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1e6fd1d7 1068 return named_fc->fc;
5cd6d0e5
PP
1069}
1070
40f4ba76
PP
1071const struct bt_field_path *
1072bt_field_class_variant_borrow_selector_field_path_const(
1073 const struct bt_field_class *fc)
e5be10ef 1074{
40f4ba76 1075 const struct bt_field_class_variant *var_fc = (const void *) fc;
5cd6d0e5 1076
bdb288b3
PP
1077 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1078 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT,
5cd6d0e5
PP
1079 "Field class");
1080 return var_fc->selector_field_path;
1081}
1082
1083static
1084void init_array_field_class(struct bt_field_class_array *fc,
864cad70 1085 enum bt_field_class_type type, bt_object_release_func release_func,
5cd6d0e5
PP
1086 struct bt_field_class *element_fc)
1087{
1088 BT_ASSERT(element_fc);
864cad70 1089 init_field_class((void *) fc, type, release_func);
398454ed
PP
1090 fc->element_fc = element_fc;
1091 bt_object_get_no_null_check(element_fc);
5cd6d0e5
PP
1092 bt_field_class_freeze(element_fc);
1093}
1094
1095static
1096void finalize_array_field_class(struct bt_field_class_array *array_fc)
1097{
1098 BT_ASSERT(array_fc);
e5be10ef 1099 BT_LOGD_STR("Putting element field class.");
238b7404 1100 BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc);
5cd6d0e5
PP
1101}
1102
1103static
1104void destroy_static_array_field_class(struct bt_object *obj)
1105{
1106 BT_ASSERT(obj);
e5be10ef 1107 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj);
5cd6d0e5
PP
1108 finalize_array_field_class((void *) obj);
1109 g_free(obj);
1110}
1111
40f4ba76 1112struct bt_field_class *
1122a43a
PP
1113bt_field_class_static_array_create(bt_trace_class *trace_class,
1114 struct bt_field_class *element_fc, uint64_t length)
5cd6d0e5
PP
1115{
1116 struct bt_field_class_static_array *array_fc = NULL;
1117
1122a43a 1118 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef
PP
1119 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1120 BT_LOGD_STR("Creating default static array field class object.");
5cd6d0e5
PP
1121 array_fc = g_new0(struct bt_field_class_static_array, 1);
1122 if (!array_fc) {
870631a2
PP
1123 BT_LIB_LOGE_APPEND_CAUSE(
1124 "Failed to allocate one static array field class.");
5cd6d0e5
PP
1125 goto error;
1126 }
1127
864cad70 1128 init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
5cd6d0e5
PP
1129 destroy_static_array_field_class, element_fc);
1130 array_fc->length = length;
e5be10ef 1131 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc);
5cd6d0e5
PP
1132 goto end;
1133
1134error:
65300d60 1135 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
5cd6d0e5
PP
1136
1137end:
1138 return (void *) array_fc;
1139}
1140
40f4ba76
PP
1141const struct bt_field_class *
1142bt_field_class_array_borrow_element_field_class_const(
1143 const struct bt_field_class *fc)
5cd6d0e5 1144{
40f4ba76 1145 const struct bt_field_class_array *array_fc = (const void *) fc;
5cd6d0e5 1146
bdb288b3
PP
1147 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1148 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
5cd6d0e5
PP
1149 return array_fc->element_fc;
1150}
1151
740faaf4
PP
1152struct bt_field_class *
1153bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc)
1154{
1155 struct bt_field_class_array *array_fc = (void *) fc;
1156
bdb288b3
PP
1157 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1158 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
740faaf4
PP
1159 return array_fc->element_fc;
1160}
1161
40f4ba76 1162uint64_t bt_field_class_static_array_get_length(const struct bt_field_class *fc)
e5be10ef 1163{
40f4ba76 1164 const struct bt_field_class_static_array *array_fc = (const void *) fc;
5cd6d0e5 1165
bdb288b3
PP
1166 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1167 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
5cd6d0e5
PP
1168 "Field class");
1169 return (uint64_t) array_fc->length;
1170}
1171
1172static
1173void destroy_dynamic_array_field_class(struct bt_object *obj)
1174{
1175 struct bt_field_class_dynamic_array *fc = (void *) obj;
1176
1177 BT_ASSERT(fc);
e5be10ef 1178 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc);
5cd6d0e5
PP
1179 finalize_array_field_class((void *) fc);
1180 BT_LOGD_STR("Putting length field path.");
238b7404
PP
1181 BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path);
1182 BT_LOGD_STR("Putting length field class.");
1183 BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc);
5cd6d0e5
PP
1184 g_free(fc);
1185}
1186
40f4ba76 1187struct bt_field_class *bt_field_class_dynamic_array_create(
1122a43a 1188 bt_trace_class *trace_class,
40f4ba76 1189 struct bt_field_class *element_fc)
5cd6d0e5
PP
1190{
1191 struct bt_field_class_dynamic_array *array_fc = NULL;
1192
1122a43a 1193 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef
PP
1194 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1195 BT_LOGD_STR("Creating default dynamic array field class object.");
5cd6d0e5
PP
1196 array_fc = g_new0(struct bt_field_class_dynamic_array, 1);
1197 if (!array_fc) {
870631a2
PP
1198 BT_LIB_LOGE_APPEND_CAUSE(
1199 "Failed to allocate one dynamic array field class.");
5cd6d0e5
PP
1200 goto error;
1201 }
1202
40f4ba76
PP
1203 init_array_field_class((void *) array_fc,
1204 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
5cd6d0e5 1205 destroy_dynamic_array_field_class, element_fc);
e5be10ef 1206 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc);
5cd6d0e5
PP
1207 goto end;
1208
1209error:
65300d60 1210 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
5cd6d0e5
PP
1211
1212end:
1213 return (void *) array_fc;
1214}
1215
d24d5663
PP
1216enum bt_field_class_dynamic_array_set_length_field_class_status
1217bt_field_class_dynamic_array_set_length_field_class(
40f4ba76
PP
1218 struct bt_field_class *fc,
1219 struct bt_field_class *length_fc)
5cd6d0e5
PP
1220{
1221 struct bt_field_class_dynamic_array *array_fc = (void *) fc;
1222
e5be10ef
PP
1223 BT_ASSERT_PRE_NON_NULL(fc, "Dynamic array field class");
1224 BT_ASSERT_PRE_NON_NULL(length_fc, "Length field class");
864cad70 1225 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
5cd6d0e5 1226 "Field class");
e5be10ef 1227 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, "Length field class");
bdb288b3 1228 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Dynamic array field class");
40f4ba76 1229 array_fc->length_fc = length_fc;
398454ed 1230 bt_object_get_no_null_check(length_fc);
5cd6d0e5 1231 bt_field_class_freeze(length_fc);
d24d5663 1232 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
1233}
1234
40f4ba76
PP
1235const struct bt_field_path *
1236bt_field_class_dynamic_array_borrow_length_field_path_const(
1237 const struct bt_field_class *fc)
5cd6d0e5 1238{
40f4ba76 1239 const struct bt_field_class_dynamic_array *seq_fc = (const void *) fc;
5cd6d0e5 1240
bdb288b3
PP
1241 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1242 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
5cd6d0e5
PP
1243 "Field class");
1244 return seq_fc->length_field_path;
1245}
1246
1247static
1248void destroy_string_field_class(struct bt_object *obj)
1249{
1250 BT_ASSERT(obj);
e5be10ef 1251 BT_LIB_LOGD("Destroying string field class object: %!+F", obj);
5cd6d0e5
PP
1252 g_free(obj);
1253}
1254
1122a43a 1255struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class)
5cd6d0e5
PP
1256{
1257 struct bt_field_class_string *string_fc = NULL;
1258
1122a43a 1259 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 1260 BT_LOGD_STR("Creating default string field class object.");
5cd6d0e5
PP
1261 string_fc = g_new0(struct bt_field_class_string, 1);
1262 if (!string_fc) {
870631a2
PP
1263 BT_LIB_LOGE_APPEND_CAUSE(
1264 "Failed to allocate one string field class.");
5cd6d0e5
PP
1265 goto error;
1266 }
1267
864cad70 1268 init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING,
5cd6d0e5 1269 destroy_string_field_class);
e5be10ef 1270 BT_LIB_LOGD("Created string field class object: %!+F", string_fc);
5cd6d0e5
PP
1271 goto end;
1272
1273error:
65300d60 1274 BT_OBJECT_PUT_REF_AND_RESET(string_fc);
5cd6d0e5
PP
1275
1276end:
1277 return (void *) string_fc;
1278}
1279
1280BT_HIDDEN
1e6fd1d7 1281void _bt_field_class_freeze(const struct bt_field_class *c_fc)
5cd6d0e5 1282{
1e6fd1d7
PP
1283 struct bt_field_class *fc = (void *) c_fc;
1284
5cd6d0e5
PP
1285 /*
1286 * Element/member/option field classes are frozen when added to
1287 * their owner.
1288 */
1289 BT_ASSERT(fc);
1e6fd1d7
PP
1290 fc->frozen = true;
1291
1292 switch (fc->type) {
1293 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1294 case BT_FIELD_CLASS_TYPE_VARIANT:
1295 {
1296 struct bt_field_class_named_field_class_container *container_fc =
1297 (void *) fc;
1298 uint64_t i;
1299
1300 for (i = 0; i < container_fc->named_fcs->len; i++) {
1301 struct bt_named_field_class *named_fc =
1302 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
1303 container_fc, i);
1304
1305 bt_named_field_class_freeze(named_fc);
1306 }
1307
1308 break;
1309 }
1310 default:
1311 break;
1312 }
1313}
1314
1315BT_HIDDEN
1316void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc)
1317{
1318 BT_ASSERT(named_fc);
1319 ((struct bt_named_field_class *) named_fc)->frozen = true;
1320 bt_field_class_freeze(named_fc->fc);
5cd6d0e5
PP
1321}
1322
1323BT_HIDDEN
bdb288b3 1324void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc)
5cd6d0e5 1325{
40f4ba76
PP
1326 struct bt_field_class *fc = (void *) c_fc;
1327
5cd6d0e5 1328 BT_ASSERT(fc);
862ca4ed 1329 BT_ASSERT_PRE(!fc->part_of_trace_class,
5cd6d0e5 1330 "Field class is already part of a trace: %!+F", fc);
862ca4ed 1331 fc->part_of_trace_class = true;
5cd6d0e5 1332
864cad70
PP
1333 switch (fc->type) {
1334 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1335 case BT_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5
PP
1336 {
1337 struct bt_field_class_named_field_class_container *container_fc =
1338 (void *) fc;
1339 uint64_t i;
1340
1341 for (i = 0; i < container_fc->named_fcs->len; i++) {
1342 struct bt_named_field_class *named_fc =
1343 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
1344 container_fc, i);
1345
862ca4ed 1346 bt_field_class_make_part_of_trace_class(named_fc->fc);
5cd6d0e5
PP
1347 }
1348
1349 break;
1350 }
864cad70
PP
1351 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1352 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
5cd6d0e5
PP
1353 {
1354 struct bt_field_class_array *array_fc = (void *) fc;
1355
862ca4ed 1356 bt_field_class_make_part_of_trace_class(array_fc->element_fc);
5cd6d0e5
PP
1357 break;
1358 }
1359 default:
1360 break;
1361 }
1362}
c5b9b441
PP
1363
1364void bt_field_class_get_ref(const struct bt_field_class *field_class)
1365{
1366 bt_object_get_ref(field_class);
1367}
1368
1369void bt_field_class_put_ref(const struct bt_field_class *field_class)
1370{
1371 bt_object_put_ref(field_class);
1372}
This page took 0.100783 seconds and 4 git commands to generate.