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