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