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