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