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