lib: make trace IR API const-correct
[babeltrace.git] / lib / trace-ir / field-classes.c
CommitLineData
5cd6d0e5 1/*
5cd6d0e5
PP
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#define BT_LOG_TAG "FIELD-CLASSES"
26#include <babeltrace/lib-logging-internal.h>
27
28#include <babeltrace/assert-pre-internal.h>
40f4ba76
PP
29#include <babeltrace/trace-ir/field-classes.h>
30#include <babeltrace/trace-ir/field-classes-const.h>
5cd6d0e5
PP
31#include <babeltrace/trace-ir/field-classes-internal.h>
32#include <babeltrace/trace-ir/field-path-internal.h>
33#include <babeltrace/trace-ir/fields-internal.h>
40f4ba76 34#include <babeltrace/trace-ir/fields-const.h>
5cd6d0e5
PP
35#include <babeltrace/trace-ir/fields.h>
36#include <babeltrace/trace-ir/utils-internal.h>
65300d60 37#include <babeltrace/object.h>
5cd6d0e5
PP
38#include <babeltrace/trace-ir/clock-class.h>
39#include <babeltrace/trace-ir/clock-class-internal.h>
40#include <babeltrace/object-internal.h>
65300d60 41#include <babeltrace/object.h>
5cd6d0e5
PP
42#include <babeltrace/compiler-internal.h>
43#include <babeltrace/endian-internal.h>
44#include <babeltrace/assert-internal.h>
45#include <babeltrace/compat/glib-internal.h>
46#include <float.h>
47#include <inttypes.h>
48#include <stdlib.h>
49
40f4ba76
PP
50enum bt_field_class_type bt_field_class_get_type(
51 const struct bt_field_class *fc)
5cd6d0e5
PP
52{
53 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 54 return fc->type;
5cd6d0e5
PP
55}
56
57static
864cad70 58void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type,
5cd6d0e5
PP
59 bt_object_release_func release_func)
60{
61 BT_ASSERT(fc);
864cad70 62 BT_ASSERT(bt_field_class_has_known_type(fc));
5cd6d0e5
PP
63 BT_ASSERT(release_func);
64 bt_object_init_shared(&fc->base, release_func);
864cad70 65 fc->type = type;
5cd6d0e5
PP
66}
67
68static
864cad70
PP
69void init_integer_field_class(struct bt_field_class_integer *fc,
70 enum bt_field_class_type type,
5cd6d0e5
PP
71 bt_object_release_func release_func)
72{
864cad70 73 init_field_class((void *) fc, type, release_func);
5cd6d0e5
PP
74 fc->range = 64;
75 fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL;
76}
77
78static
79void destroy_integer_field_class(struct bt_object *obj)
80{
81 BT_ASSERT(obj);
e5be10ef 82 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj);
5cd6d0e5
PP
83 g_free(obj);
84}
85
86static inline
864cad70 87struct bt_field_class *create_integer_field_class(enum bt_field_class_type type)
5cd6d0e5
PP
88{
89 struct bt_field_class_integer *int_fc = NULL;
90
e5be10ef 91 BT_LOGD("Creating default integer field class object: type=%s",
864cad70 92 bt_common_field_class_type_string(type));
5cd6d0e5
PP
93 int_fc = g_new0(struct bt_field_class_integer, 1);
94 if (!int_fc) {
e5be10ef 95 BT_LOGE_STR("Failed to allocate one integer field class.");
5cd6d0e5
PP
96 goto error;
97 }
98
864cad70 99 init_integer_field_class(int_fc, type, destroy_integer_field_class);
e5be10ef 100 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc);
5cd6d0e5
PP
101 goto end;
102
103error:
65300d60 104 BT_OBJECT_PUT_REF_AND_RESET(int_fc);
5cd6d0e5
PP
105
106end:
107 return (void *) int_fc;
108}
109
40f4ba76
PP
110struct bt_field_class *
111bt_field_class_unsigned_integer_create(void)
5cd6d0e5 112{
40f4ba76 113 return create_integer_field_class(
e5be10ef 114 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
5cd6d0e5
PP
115}
116
40f4ba76 117struct bt_field_class *bt_field_class_signed_integer_create(void)
5cd6d0e5 118{
40f4ba76 119 return create_integer_field_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
PP
231struct bt_field_class *create_enumeration_field_class(
232 enum bt_field_class_type type)
5cd6d0e5
PP
233{
234 struct bt_field_class_enumeration *enum_fc = NULL;
235
e5be10ef 236 BT_LOGD("Creating default enumeration field class object: type=%s",
864cad70 237 bt_common_field_class_type_string(type));
5cd6d0e5
PP
238 enum_fc = g_new0(struct bt_field_class_enumeration, 1);
239 if (!enum_fc) {
e5be10ef 240 BT_LOGE_STR("Failed to allocate one enumeration field class.");
5cd6d0e5
PP
241 goto error;
242 }
243
864cad70 244 init_integer_field_class((void *) enum_fc, type,
5cd6d0e5
PP
245 destroy_enumeration_field_class);
246 enum_fc->mappings = g_array_new(FALSE, TRUE,
247 sizeof(struct bt_field_class_enumeration_mapping));
248 if (!enum_fc->mappings) {
249 BT_LOGE_STR("Failed to allocate a GArray.");
250 goto error;
251 }
252
253 enum_fc->label_buf = g_ptr_array_new();
254 if (!enum_fc->label_buf) {
255 BT_LOGE_STR("Failed to allocate a GArray.");
256 goto error;
257 }
258
e5be10ef 259 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc);
5cd6d0e5
PP
260 goto end;
261
262error:
65300d60 263 BT_OBJECT_PUT_REF_AND_RESET(enum_fc);
5cd6d0e5
PP
264
265end:
266 return (void *) enum_fc;
267}
268
40f4ba76 269struct bt_field_class *bt_field_class_unsigned_enumeration_create(void)
5cd6d0e5 270{
40f4ba76 271 return create_enumeration_field_class(
864cad70 272 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
5cd6d0e5
PP
273}
274
40f4ba76 275struct bt_field_class *bt_field_class_signed_enumeration_create(void)
5cd6d0e5 276{
40f4ba76 277 return create_enumeration_field_class(
864cad70 278 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
5cd6d0e5
PP
279}
280
40f4ba76
PP
281uint64_t bt_field_class_enumeration_get_mapping_count(
282 const struct bt_field_class *fc)
5cd6d0e5 283{
40f4ba76 284 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
285
286 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
287 BT_ASSERT_PRE_FC_IS_ENUM(fc, "Field class");
288 return (uint64_t) enum_fc->mappings->len;
289}
290
40f4ba76
PP
291void bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const(
292 const struct bt_field_class *fc, uint64_t index,
5cd6d0e5 293 const char **name,
40f4ba76 294 const struct bt_field_class_unsigned_enumeration_mapping_ranges **ranges)
5cd6d0e5 295{
40f4ba76
PP
296 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
297 const struct bt_field_class_enumeration_mapping *mapping;
5cd6d0e5
PP
298
299 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
300 BT_ASSERT_PRE_NON_NULL(name, "Name (output)");
301 BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)");
302 BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len);
864cad70 303 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5
PP
304 "Field class");
305 mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
306 *name = mapping->label->str;
307 *ranges = (void *) mapping;
308}
309
40f4ba76
PP
310void bt_field_class_signed_enumeration_borrow_mapping_by_index_const(
311 const struct bt_field_class *fc, uint64_t index,
5cd6d0e5 312 const char **name,
40f4ba76 313 const struct bt_field_class_signed_enumeration_mapping_ranges **ranges)
5cd6d0e5 314{
40f4ba76
PP
315 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
316 const struct bt_field_class_enumeration_mapping *mapping;
5cd6d0e5
PP
317
318 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
319 BT_ASSERT_PRE_NON_NULL(name, "Name (output)");
320 BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)");
321 BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len);
864cad70 322 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5
PP
323 "Field class");
324 mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
325 *name = mapping->label->str;
326 *ranges = (void *) mapping;
327}
328
329static inline
330uint64_t get_enumeration_field_class_mapping_range_count(
40f4ba76 331 const struct bt_field_class_enumeration_mapping *mapping)
5cd6d0e5
PP
332{
333 BT_ASSERT_PRE_NON_NULL(mapping, "Ranges");
334 return (uint64_t) mapping->ranges->len;
335}
336
337uint64_t bt_field_class_unsigned_enumeration_mapping_ranges_get_range_count(
40f4ba76 338 const struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges)
5cd6d0e5 339{
40f4ba76
PP
340 return get_enumeration_field_class_mapping_range_count(
341 (const void *) ranges);
5cd6d0e5
PP
342}
343
344uint64_t bt_field_class_signed_enumeration_mapping_ranges_get_range_count(
40f4ba76 345 const struct bt_field_class_signed_enumeration_mapping_ranges *ranges)
5cd6d0e5 346{
40f4ba76
PP
347 return get_enumeration_field_class_mapping_range_count(
348 (const void *) ranges);
5cd6d0e5
PP
349}
350
351static inline
352void get_enumeration_field_class_mapping_range_at_index(
40f4ba76 353 const struct bt_field_class_enumeration_mapping *mapping,
5cd6d0e5
PP
354 uint64_t index, uint64_t *lower, uint64_t *upper)
355{
40f4ba76 356 const struct bt_field_class_enumeration_mapping_range *range;
5cd6d0e5
PP
357
358 BT_ASSERT_PRE_NON_NULL(mapping, "Ranges");
359 BT_ASSERT_PRE_NON_NULL(lower, "Range's lower (output)");
360 BT_ASSERT_PRE_NON_NULL(upper, "Range's upper (output)");
361 BT_ASSERT_PRE_VALID_INDEX(index, mapping->ranges->len);
362 range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, index);
363 *lower = range->lower.u;
364 *upper = range->upper.u;
365}
366
367void bt_field_class_unsigned_enumeration_mapping_ranges_get_range_by_index(
40f4ba76 368 const struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges,
5cd6d0e5
PP
369 uint64_t index, uint64_t *lower, uint64_t *upper)
370{
40f4ba76
PP
371 get_enumeration_field_class_mapping_range_at_index(
372 (const void *) ranges, index, lower, upper);
5cd6d0e5
PP
373}
374
375void bt_field_class_signed_enumeration_mapping_ranges_get_range_by_index(
40f4ba76 376 const struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges,
5cd6d0e5
PP
377 uint64_t index, int64_t *lower, int64_t *upper)
378{
40f4ba76
PP
379 get_enumeration_field_class_mapping_range_at_index(
380 (const void *) ranges, index,
381 (uint64_t *) lower, (uint64_t *) upper);
5cd6d0e5
PP
382}
383
384
385
386int bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
40f4ba76 387 const struct bt_field_class *fc, uint64_t value,
5cd6d0e5
PP
388 bt_field_class_enumeration_mapping_label_array *label_array,
389 uint64_t *count)
390{
40f4ba76 391 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
392 uint64_t i;
393
394 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
395 BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)");
396 BT_ASSERT_PRE_NON_NULL(count, "Count (output)");
864cad70 397 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5
PP
398 "Field class");
399 g_ptr_array_set_size(enum_fc->label_buf, 0);
400
401 for (i = 0; i < enum_fc->mappings->len; i++) {
402 uint64_t j;
40f4ba76 403 const struct bt_field_class_enumeration_mapping *mapping =
5cd6d0e5
PP
404 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
405
406 for (j = 0; j < mapping->ranges->len; j++) {
40f4ba76 407 const struct bt_field_class_enumeration_mapping_range *range =
5cd6d0e5
PP
408 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
409 mapping, j);
410
411 if (value >= range->lower.u &&
412 value <= range->upper.u) {
413 g_ptr_array_add(enum_fc->label_buf,
414 mapping->label->str);
415 break;
416 }
417 }
418 }
419
420 *label_array = (void *) enum_fc->label_buf->pdata;
421 *count = (uint64_t) enum_fc->label_buf->len;
422 return 0;
423}
424
425int bt_field_class_signed_enumeration_get_mapping_labels_by_value(
40f4ba76 426 const struct bt_field_class *fc, int64_t value,
5cd6d0e5
PP
427 bt_field_class_enumeration_mapping_label_array *label_array,
428 uint64_t *count)
429{
40f4ba76 430 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
431 uint64_t i;
432
433 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
434 BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)");
435 BT_ASSERT_PRE_NON_NULL(count, "Count (output)");
864cad70 436 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5
PP
437 "Field class");
438 g_ptr_array_set_size(enum_fc->label_buf, 0);
439
440 for (i = 0; i < enum_fc->mappings->len; i++) {
441 uint64_t j;
40f4ba76 442 const struct bt_field_class_enumeration_mapping *mapping =
5cd6d0e5
PP
443 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
444
445 for (j = 0; j < mapping->ranges->len; j++) {
40f4ba76 446 const struct bt_field_class_enumeration_mapping_range *range =
5cd6d0e5
PP
447 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
448 mapping, j);
449
450 if (value >= range->lower.i &&
451 value <= range->upper.i) {
452 g_ptr_array_add(enum_fc->label_buf,
453 mapping->label->str);
454 break;
455 }
456 }
457 }
458
459 *label_array = (void *) enum_fc->label_buf->pdata;
460 *count = (uint64_t) enum_fc->label_buf->len;
461 return 0;
462}
463
464static inline
465int add_mapping_to_enumeration_field_class(struct bt_field_class *fc,
466 const char *label, uint64_t lower, uint64_t upper)
467{
468 int ret = 0;
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);
499 ret = -1;
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);
508 ret = -1;
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
40f4ba76
PP
528int bt_field_class_unsigned_enumeration_map_range(
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
40f4ba76
PP
553int bt_field_class_signed_enumeration_map_range(
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
737int append_named_field_class_to_container_field_class(
738 struct bt_field_class_named_field_class_container *container_fc,
739 const char *name, struct bt_field_class *fc)
740{
741 int ret = 0;
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.");
756 ret = -1;
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
40f4ba76
PP
775int bt_field_class_structure_append_member(struct bt_field_class *fc,
776 const char *name, struct bt_field_class *member_fc)
5cd6d0e5 777{
e5be10ef 778
5cd6d0e5 779 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 780 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class");
5cd6d0e5 781 return append_named_field_class_to_container_field_class((void *) fc,
40f4ba76 782 name, member_fc);
5cd6d0e5
PP
783}
784
40f4ba76
PP
785uint64_t bt_field_class_structure_get_member_count(
786 const struct bt_field_class *fc)
5cd6d0e5
PP
787{
788 struct bt_field_class_structure *struct_fc = (void *) fc;
789
790 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 791 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class");
5cd6d0e5
PP
792 return (uint64_t) struct_fc->common.named_fcs->len;
793}
794
795static
40f4ba76
PP
796void borrow_named_field_class_from_container_field_class_at_index_const(
797 const struct bt_field_class_named_field_class_container *fc,
5cd6d0e5 798 uint64_t index, const char **name,
40f4ba76 799 const struct bt_field_class **out_fc)
5cd6d0e5 800{
40f4ba76 801 const struct bt_named_field_class *named_fc;
5cd6d0e5
PP
802
803 BT_ASSERT(fc);
804 BT_ASSERT_PRE_NON_NULL(name, "Name");
805 BT_ASSERT_PRE_NON_NULL(out_fc, "Field class (output)");
806 BT_ASSERT_PRE_VALID_INDEX(index, fc->named_fcs->len);
807 named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, index);
808 *name = named_fc->name->str;
809 *out_fc = named_fc->fc;
810}
811
40f4ba76
PP
812void bt_field_class_structure_borrow_member_by_index_const(
813 const struct bt_field_class *fc, uint64_t index,
814 const char **name, const struct bt_field_class **out_fc)
5cd6d0e5
PP
815{
816 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 817 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class");
40f4ba76
PP
818 borrow_named_field_class_from_container_field_class_at_index_const(
819 (void *) fc, index, name, out_fc);
e5be10ef
PP
820}
821
5cd6d0e5 822static
40f4ba76
PP
823const struct bt_field_class *
824borrow_field_class_from_container_field_class_by_name_const(
825 const struct bt_field_class_named_field_class_container *fc,
5cd6d0e5
PP
826 const char *name)
827{
40f4ba76
PP
828 const struct bt_field_class *ret_fc = NULL;
829 const struct bt_named_field_class *named_fc;
5cd6d0e5
PP
830 gpointer orig_key;
831 gpointer value;
832
833 BT_ASSERT(fc);
834 BT_ASSERT_PRE_NON_NULL(name, "Name");
835 if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key,
836 &value)) {
837 goto end;
838 }
839
840 named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc,
841 GPOINTER_TO_UINT(value));
842 ret_fc = named_fc->fc;
843
844end:
845 return ret_fc;
846}
847
40f4ba76
PP
848const struct bt_field_class *
849bt_field_class_structure_borrow_member_field_class_by_name(
850 const struct bt_field_class *fc, const char *name)
5cd6d0e5
PP
851{
852 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 853 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class");
40f4ba76
PP
854 return borrow_field_class_from_container_field_class_by_name_const(
855 (void *) fc, name);
e5be10ef
PP
856}
857
5cd6d0e5
PP
858static
859void destroy_variant_field_class(struct bt_object *obj)
860{
861 struct bt_field_class_variant *fc = (void *) obj;
862
863 BT_ASSERT(fc);
e5be10ef 864 BT_LIB_LOGD("Destroying variant field class object: %!+F", fc);
5cd6d0e5
PP
865 finalize_named_field_classes_container((void *) fc);
866 BT_LOGD_STR("Putting selector field path.");
238b7404
PP
867 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path);
868 BT_LOGD_STR("Putting selector field class.");
869 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc);
5cd6d0e5
PP
870 g_free(fc);
871}
872
40f4ba76 873struct bt_field_class *bt_field_class_variant_create(void)
5cd6d0e5
PP
874{
875 int ret;
876 struct bt_field_class_variant *var_fc = NULL;
877
e5be10ef 878 BT_LOGD_STR("Creating default variant field class object.");
5cd6d0e5
PP
879 var_fc = g_new0(struct bt_field_class_variant, 1);
880 if (!var_fc) {
e5be10ef 881 BT_LOGE_STR("Failed to allocate one variant field class.");
5cd6d0e5
PP
882 goto error;
883 }
884
885 ret = init_named_field_classes_container((void *) var_fc,
864cad70 886 BT_FIELD_CLASS_TYPE_VARIANT, destroy_variant_field_class);
5cd6d0e5
PP
887 if (ret) {
888 goto error;
889 }
890
e5be10ef 891 BT_LIB_LOGD("Created variant field class object: %!+F", var_fc);
5cd6d0e5
PP
892 goto end;
893
894error:
65300d60 895 BT_OBJECT_PUT_REF_AND_RESET(var_fc);
5cd6d0e5
PP
896
897end:
898 return (void *) var_fc;
899}
900
40f4ba76
PP
901int bt_field_class_variant_set_selector_field_class(
902 struct bt_field_class *fc,
903 struct bt_field_class *selector_fc)
5cd6d0e5
PP
904{
905 struct bt_field_class_variant *var_fc = (void *) fc;
906
e5be10ef
PP
907 BT_ASSERT_PRE_NON_NULL(fc, "Variant field class");
908 BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class");
864cad70 909 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
e5be10ef
PP
910 BT_ASSERT_PRE_FC_IS_ENUM(selector_fc, "Selector field class");
911 BT_ASSERT_PRE_FC_HOT(fc, "Variant field class");
40f4ba76 912 var_fc->selector_fc = selector_fc;
398454ed 913 bt_object_get_no_null_check(selector_fc);
40f4ba76 914 bt_field_class_freeze(selector_fc);
5cd6d0e5
PP
915 return 0;
916}
917
40f4ba76
PP
918int bt_field_class_variant_append_option(
919 struct bt_field_class *fc,
920 const char *name, struct bt_field_class *option_fc)
5cd6d0e5 921{
e5be10ef 922
5cd6d0e5 923 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 924 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
5cd6d0e5 925 return append_named_field_class_to_container_field_class((void *) fc,
40f4ba76 926 name, option_fc);
5cd6d0e5
PP
927}
928
40f4ba76
PP
929const struct bt_field_class *
930bt_field_class_variant_borrow_option_field_class_by_name_const(
931 const struct bt_field_class *fc, const char *name)
5cd6d0e5
PP
932{
933 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 934 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
40f4ba76 935 return borrow_field_class_from_container_field_class_by_name_const(
e5be10ef
PP
936 (void *) fc, name);
937}
938
40f4ba76 939uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc)
5cd6d0e5 940{
40f4ba76 941 const struct bt_field_class_variant *var_fc = (const void *) fc;
5cd6d0e5
PP
942
943 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 944 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
5cd6d0e5
PP
945 return (uint64_t) var_fc->common.named_fcs->len;
946}
947
40f4ba76
PP
948void bt_field_class_variant_borrow_option_by_index_const(
949 const struct bt_field_class *fc, uint64_t index,
950 const char **name, const struct bt_field_class **out_fc)
5cd6d0e5
PP
951{
952 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 953 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class");
40f4ba76
PP
954 borrow_named_field_class_from_container_field_class_at_index_const(
955 (void *) fc, index, name, out_fc);
5cd6d0e5
PP
956}
957
40f4ba76
PP
958const struct bt_field_path *
959bt_field_class_variant_borrow_selector_field_path_const(
960 const struct bt_field_class *fc)
e5be10ef 961{
40f4ba76 962 const struct bt_field_class_variant *var_fc = (const void *) fc;
5cd6d0e5
PP
963
964 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 965 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT,
5cd6d0e5
PP
966 "Field class");
967 return var_fc->selector_field_path;
968}
969
970static
971void init_array_field_class(struct bt_field_class_array *fc,
864cad70 972 enum bt_field_class_type type, bt_object_release_func release_func,
5cd6d0e5
PP
973 struct bt_field_class *element_fc)
974{
975 BT_ASSERT(element_fc);
864cad70 976 init_field_class((void *) fc, type, release_func);
398454ed
PP
977 fc->element_fc = element_fc;
978 bt_object_get_no_null_check(element_fc);
5cd6d0e5
PP
979 bt_field_class_freeze(element_fc);
980}
981
982static
983void finalize_array_field_class(struct bt_field_class_array *array_fc)
984{
985 BT_ASSERT(array_fc);
e5be10ef 986 BT_LOGD_STR("Putting element field class.");
238b7404 987 BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc);
5cd6d0e5
PP
988}
989
990static
991void destroy_static_array_field_class(struct bt_object *obj)
992{
993 BT_ASSERT(obj);
e5be10ef 994 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj);
5cd6d0e5
PP
995 finalize_array_field_class((void *) obj);
996 g_free(obj);
997}
998
40f4ba76
PP
999struct bt_field_class *
1000bt_field_class_static_array_create(struct bt_field_class *element_fc,
e5be10ef 1001 uint64_t length)
5cd6d0e5
PP
1002{
1003 struct bt_field_class_static_array *array_fc = NULL;
1004
e5be10ef
PP
1005 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1006 BT_LOGD_STR("Creating default static array field class object.");
5cd6d0e5
PP
1007 array_fc = g_new0(struct bt_field_class_static_array, 1);
1008 if (!array_fc) {
e5be10ef 1009 BT_LOGE_STR("Failed to allocate one static array field class.");
5cd6d0e5
PP
1010 goto error;
1011 }
1012
864cad70 1013 init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
5cd6d0e5
PP
1014 destroy_static_array_field_class, element_fc);
1015 array_fc->length = length;
e5be10ef 1016 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc);
5cd6d0e5
PP
1017 goto end;
1018
1019error:
65300d60 1020 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
5cd6d0e5
PP
1021
1022end:
1023 return (void *) array_fc;
1024}
1025
40f4ba76
PP
1026const struct bt_field_class *
1027bt_field_class_array_borrow_element_field_class_const(
1028 const struct bt_field_class *fc)
5cd6d0e5 1029{
40f4ba76 1030 const struct bt_field_class_array *array_fc = (const void *) fc;
5cd6d0e5
PP
1031
1032 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1033 BT_ASSERT_PRE_FC_IS_ARRAY(fc, "Field class");
1034 return array_fc->element_fc;
1035}
1036
40f4ba76 1037uint64_t bt_field_class_static_array_get_length(const struct bt_field_class *fc)
e5be10ef 1038{
40f4ba76 1039 const struct bt_field_class_static_array *array_fc = (const void *) fc;
5cd6d0e5
PP
1040
1041 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 1042 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
5cd6d0e5
PP
1043 "Field class");
1044 return (uint64_t) array_fc->length;
1045}
1046
1047static
1048void destroy_dynamic_array_field_class(struct bt_object *obj)
1049{
1050 struct bt_field_class_dynamic_array *fc = (void *) obj;
1051
1052 BT_ASSERT(fc);
e5be10ef 1053 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc);
5cd6d0e5
PP
1054 finalize_array_field_class((void *) fc);
1055 BT_LOGD_STR("Putting length field path.");
238b7404
PP
1056 BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path);
1057 BT_LOGD_STR("Putting length field class.");
1058 BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc);
5cd6d0e5
PP
1059 g_free(fc);
1060}
1061
40f4ba76
PP
1062struct bt_field_class *bt_field_class_dynamic_array_create(
1063 struct bt_field_class *element_fc)
5cd6d0e5
PP
1064{
1065 struct bt_field_class_dynamic_array *array_fc = NULL;
1066
e5be10ef
PP
1067 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1068 BT_LOGD_STR("Creating default dynamic array field class object.");
5cd6d0e5
PP
1069 array_fc = g_new0(struct bt_field_class_dynamic_array, 1);
1070 if (!array_fc) {
e5be10ef 1071 BT_LOGE_STR("Failed to allocate one dynamic array field class.");
5cd6d0e5
PP
1072 goto error;
1073 }
1074
40f4ba76
PP
1075 init_array_field_class((void *) array_fc,
1076 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
5cd6d0e5 1077 destroy_dynamic_array_field_class, element_fc);
e5be10ef 1078 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc);
5cd6d0e5
PP
1079 goto end;
1080
1081error:
65300d60 1082 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
5cd6d0e5
PP
1083
1084end:
1085 return (void *) array_fc;
1086}
1087
40f4ba76
PP
1088int bt_field_class_dynamic_array_set_length_field_class(
1089 struct bt_field_class *fc,
1090 struct bt_field_class *length_fc)
5cd6d0e5
PP
1091{
1092 struct bt_field_class_dynamic_array *array_fc = (void *) fc;
1093
e5be10ef
PP
1094 BT_ASSERT_PRE_NON_NULL(fc, "Dynamic array field class");
1095 BT_ASSERT_PRE_NON_NULL(length_fc, "Length field class");
864cad70 1096 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
5cd6d0e5 1097 "Field class");
e5be10ef
PP
1098 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, "Length field class");
1099 BT_ASSERT_PRE_FC_HOT(fc, "Dynamic array field class");
40f4ba76 1100 array_fc->length_fc = length_fc;
398454ed 1101 bt_object_get_no_null_check(length_fc);
5cd6d0e5
PP
1102 bt_field_class_freeze(length_fc);
1103 return 0;
1104}
1105
40f4ba76
PP
1106const struct bt_field_path *
1107bt_field_class_dynamic_array_borrow_length_field_path_const(
1108 const struct bt_field_class *fc)
5cd6d0e5 1109{
40f4ba76 1110 const struct bt_field_class_dynamic_array *seq_fc = (const void *) fc;
5cd6d0e5
PP
1111
1112 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 1113 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
5cd6d0e5
PP
1114 "Field class");
1115 return seq_fc->length_field_path;
1116}
1117
1118static
1119void destroy_string_field_class(struct bt_object *obj)
1120{
1121 BT_ASSERT(obj);
e5be10ef 1122 BT_LIB_LOGD("Destroying string field class object: %!+F", obj);
5cd6d0e5
PP
1123 g_free(obj);
1124}
1125
40f4ba76 1126struct bt_field_class *bt_field_class_string_create(void)
5cd6d0e5
PP
1127{
1128 struct bt_field_class_string *string_fc = NULL;
1129
e5be10ef 1130 BT_LOGD_STR("Creating default string field class object.");
5cd6d0e5
PP
1131 string_fc = g_new0(struct bt_field_class_string, 1);
1132 if (!string_fc) {
e5be10ef 1133 BT_LOGE_STR("Failed to allocate one string field class.");
5cd6d0e5
PP
1134 goto error;
1135 }
1136
864cad70 1137 init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING,
5cd6d0e5 1138 destroy_string_field_class);
e5be10ef 1139 BT_LIB_LOGD("Created string field class object: %!+F", string_fc);
5cd6d0e5
PP
1140 goto end;
1141
1142error:
65300d60 1143 BT_OBJECT_PUT_REF_AND_RESET(string_fc);
5cd6d0e5
PP
1144
1145end:
1146 return (void *) string_fc;
1147}
1148
1149BT_HIDDEN
40f4ba76 1150void _bt_field_class_freeze(const struct bt_field_class *fc)
5cd6d0e5
PP
1151{
1152 /*
1153 * Element/member/option field classes are frozen when added to
1154 * their owner.
1155 */
1156 BT_ASSERT(fc);
40f4ba76 1157 ((struct bt_field_class *) fc)->frozen = true;
5cd6d0e5
PP
1158}
1159
1160BT_HIDDEN
40f4ba76 1161void _bt_field_class_make_part_of_trace(const struct bt_field_class *c_fc)
5cd6d0e5 1162{
40f4ba76
PP
1163 struct bt_field_class *fc = (void *) c_fc;
1164
5cd6d0e5
PP
1165 BT_ASSERT(fc);
1166 BT_ASSERT_PRE(!fc->part_of_trace,
1167 "Field class is already part of a trace: %!+F", fc);
1168 fc->part_of_trace = true;
1169
864cad70
PP
1170 switch (fc->type) {
1171 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1172 case BT_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5
PP
1173 {
1174 struct bt_field_class_named_field_class_container *container_fc =
1175 (void *) fc;
1176 uint64_t i;
1177
1178 for (i = 0; i < container_fc->named_fcs->len; i++) {
1179 struct bt_named_field_class *named_fc =
1180 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
1181 container_fc, i);
1182
1183 bt_field_class_make_part_of_trace(named_fc->fc);
1184 }
1185
1186 break;
1187 }
864cad70
PP
1188 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1189 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
5cd6d0e5
PP
1190 {
1191 struct bt_field_class_array *array_fc = (void *) fc;
1192
1193 bt_field_class_make_part_of_trace(array_fc->element_fc);
1194 break;
1195 }
1196 default:
1197 break;
1198 }
1199}
This page took 0.0787679999999999 seconds and 4 git commands to generate.