CTF IR -> Trace IR
[babeltrace.git] / lib / trace-ir / field-types.c
CommitLineData
273b65be 1/*
2e33ac5a 2 * field-types.c
273b65be 3 *
108b91d0 4 * Babeltrace trace IR - Event Types
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
4e8304f7
PP
29#define BT_LOG_TAG "FIELD-TYPES"
30#include <babeltrace/lib-logging-internal.h>
31
8deee039 32#include <babeltrace/assert-pre-internal.h>
108b91d0
PP
33#include <babeltrace/trace-ir/field-types-internal.h>
34#include <babeltrace/trace-ir/field-path-internal.h>
35#include <babeltrace/trace-ir/fields-internal.h>
36#include <babeltrace/trace-ir/fields.h>
37#include <babeltrace/trace-ir/utils-internal.h>
83509119 38#include <babeltrace/ref.h>
108b91d0
PP
39#include <babeltrace/trace-ir/clock-class.h>
40#include <babeltrace/trace-ir/clock-class-internal.h>
83509119
JG
41#include <babeltrace/object-internal.h>
42#include <babeltrace/ref.h>
3d9990ac
PP
43#include <babeltrace/compiler-internal.h>
44#include <babeltrace/endian-internal.h>
8b45963b 45#include <babeltrace/assert-internal.h>
7b33a0e0 46#include <babeltrace/compat/glib-internal.h>
273b65be
JG
47#include <float.h>
48#include <inttypes.h>
a39fa057 49#include <stdlib.h>
273b65be 50
7b33a0e0
PP
51enum bt_field_type_id bt_field_type_get_type_id(struct bt_field_type *ft)
52{
53 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
54 return ft->id;
55}
273b65be
JG
56
57static
7b33a0e0
PP
58void init_field_type(struct bt_field_type *ft, enum bt_field_type_id id,
59 bt_object_release_func release_func)
60{
61 BT_ASSERT(ft);
62 BT_ASSERT(bt_field_type_has_known_id(ft));
63 BT_ASSERT(release_func);
64 bt_object_init_shared(&ft->base, release_func);
65 ft->id = id;
66}
273b65be
JG
67
68static
7b33a0e0
PP
69void init_integer_field_type(struct bt_field_type_integer *ft, enum bt_field_type_id id,
70 bt_object_release_func release_func)
71{
72 init_field_type((void *) ft, id, release_func);
73 ft->range = 64;
74 ft->base = BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL;
75}
273b65be
JG
76
77static
7b33a0e0
PP
78void destroy_integer_field_type(struct bt_object *obj)
79{
80 BT_ASSERT(obj);
81 BT_LIB_LOGD("Destroying integer field type object: %!+F", obj);
82 g_free(obj);
83}
273b65be 84
7b33a0e0
PP
85static inline
86struct bt_field_type *create_integer_field_type(enum bt_field_type_id id)
87{
88 struct bt_field_type_integer *int_ft = NULL;
273b65be 89
7b33a0e0
PP
90 BT_LOGD("Creating default integer field type object: id=%s",
91 bt_common_field_type_id_string(id));
92 int_ft = g_new0(struct bt_field_type_integer, 1);
93 if (!int_ft) {
94 BT_LOGE_STR("Failed to allocate one integer field type.");
95 goto error;
96 }
273b65be 97
7b33a0e0
PP
98 init_integer_field_type(int_ft, id, destroy_integer_field_type);
99 BT_LIB_LOGD("Created integer field type object: %!+F", int_ft);
100 goto end;
273b65be 101
7b33a0e0
PP
102error:
103 BT_PUT(int_ft);
8deee039 104
7b33a0e0
PP
105end:
106 return (void *) int_ft;
107}
18acc6f8 108
7b33a0e0
PP
109struct bt_field_type *bt_field_type_unsigned_integer_create(void)
110{
111 return create_integer_field_type(BT_FIELD_TYPE_ID_UNSIGNED_INTEGER);
112}
18acc6f8 113
7b33a0e0
PP
114struct bt_field_type *bt_field_type_signed_integer_create(void)
115{
116 return create_integer_field_type(BT_FIELD_TYPE_ID_SIGNED_INTEGER);
117}
18acc6f8 118
7b33a0e0
PP
119uint64_t bt_field_type_integer_get_field_value_range(
120 struct bt_field_type *ft)
121{
122 struct bt_field_type_integer *int_ft = (void *) ft;
18acc6f8 123
7b33a0e0
PP
124 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
125 BT_ASSERT_PRE_FT_IS_INT(ft, "Field type");
126 return int_ft->range;
127}
18acc6f8 128
7b33a0e0 129BT_ASSERT_PRE_FUNC
18acc6f8 130static
7b33a0e0
PP
131bool size_is_valid_for_enumeration_field_type(struct bt_field_type *ft,
132 uint64_t size)
133{
134 // TODO
135 return true;
136}
18acc6f8 137
7b33a0e0
PP
138int bt_field_type_integer_set_field_value_range(
139 struct bt_field_type *ft, uint64_t size)
140{
141 struct bt_field_type_integer *int_ft = (void *) ft;
18acc6f8 142
7b33a0e0
PP
143 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
144 BT_ASSERT_PRE_FT_IS_INT(ft, "Field type");
145 BT_ASSERT_PRE_FT_HOT(ft, "Field type");
146 BT_ASSERT_PRE(size <= 64,
147 "Unsupported size for integer field type's field value range "
148 "(maximum is 64): size=%" PRIu64, size);
149 BT_ASSERT_PRE(int_ft->common.id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER ||
150 int_ft->common.id == BT_FIELD_TYPE_ID_SIGNED_INTEGER ||
151 size_is_valid_for_enumeration_field_type(ft, size),
152 "Invalid field value range for enumeration field type: "
153 "at least one of the current mapping ranges contains values "
154 "which are outside this range: %!+F, size=%" PRIu64, ft, size);
155 int_ft->range = size;
156 BT_LIB_LOGV("Set integer field type's field value range: %!+F", ft);
157 return 0;
158}
18acc6f8 159
7b33a0e0
PP
160enum bt_field_type_integer_preferred_display_base
161bt_field_type_integer_get_preferred_display_base(struct bt_field_type *ft)
162{
163 struct bt_field_type_integer *int_ft = (void *) ft;
18acc6f8 164
7b33a0e0
PP
165 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
166 BT_ASSERT_PRE_FT_IS_INT(ft, "Field type");
167 return int_ft->base;
168}
18acc6f8 169
7b33a0e0
PP
170int bt_field_type_integer_set_preferred_display_base(struct bt_field_type *ft,
171 enum bt_field_type_integer_preferred_display_base base)
172{
173 struct bt_field_type_integer *int_ft = (void *) ft;
18acc6f8 174
7b33a0e0
PP
175 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
176 BT_ASSERT_PRE_FT_IS_INT(ft, "Field type");
177 BT_ASSERT_PRE_FT_HOT(ft, "Field type");
178 int_ft->base = base;
179 BT_LIB_LOGV("Set integer field type's preferred display base: %!+F", ft);
180 return 0;
181}
18acc6f8
PP
182
183static
7b33a0e0
PP
184void finalize_enumeration_field_type_mapping(
185 struct bt_field_type_enumeration_mapping *mapping)
186{
187 BT_ASSERT(mapping);
18acc6f8 188
7b33a0e0
PP
189 if (mapping->label) {
190 g_string_free(mapping->label, TRUE);
191 }
18acc6f8 192
7b33a0e0
PP
193 if (mapping->ranges) {
194 g_array_free(mapping->ranges, TRUE);
195 }
196}
18acc6f8
PP
197
198static
7b33a0e0
PP
199void destroy_enumeration_field_type(struct bt_object *obj)
200{
201 struct bt_field_type_enumeration *ft = (void *) obj;
18acc6f8 202
7b33a0e0
PP
203 BT_ASSERT(ft);
204 BT_LIB_LOGD("Destroying enumeration field type object: %!+F", ft);
18acc6f8 205
7b33a0e0
PP
206 if (ft->mappings) {
207 uint64_t i;
81e36fac 208
7b33a0e0
PP
209 for (i = 0; i < ft->mappings->len; i++) {
210 finalize_enumeration_field_type_mapping(
211 BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(ft, i));
212 }
213
214 g_array_free(ft->mappings, TRUE);
215 }
216
217 if (ft->label_buf) {
218 g_ptr_array_free(ft->label_buf, TRUE);
219 }
220
221 g_free(ft);
273b65be
JG
222}
223
18acc6f8 224static
7b33a0e0 225struct bt_field_type *create_enumeration_field_type(enum bt_field_type_id id)
8deee039 226{
7b33a0e0 227 struct bt_field_type_enumeration *enum_ft = NULL;
8deee039 228
7b33a0e0
PP
229 BT_LOGD("Creating default enumeration field type object: id=%s",
230 bt_common_field_type_id_string(id));
231 enum_ft = g_new0(struct bt_field_type_enumeration, 1);
232 if (!enum_ft) {
233 BT_LOGE_STR("Failed to allocate one enumeration field type.");
234 goto error;
235 }
8deee039 236
7b33a0e0
PP
237 init_integer_field_type((void *) enum_ft, id,
238 destroy_enumeration_field_type);
239 enum_ft->mappings = g_array_new(FALSE, TRUE,
240 sizeof(struct bt_field_type_enumeration_mapping));
241 if (!enum_ft->mappings) {
242 BT_LOGE_STR("Failed to allocate a GArray.");
243 goto error;
244 }
8deee039 245
7b33a0e0
PP
246 enum_ft->label_buf = g_ptr_array_new();
247 if (!enum_ft->label_buf) {
248 BT_LOGE_STR("Failed to allocate a GArray.");
249 goto error;
8deee039
PP
250 }
251
7b33a0e0
PP
252 BT_LIB_LOGD("Created enumeration field type object: %!+F", enum_ft);
253 goto end;
254
255error:
256 BT_PUT(enum_ft);
257
258end:
259 return (void *) enum_ft;
8deee039
PP
260}
261
7b33a0e0 262struct bt_field_type *bt_field_type_unsigned_enumeration_create(void)
8deee039 263{
7b33a0e0
PP
264 return create_enumeration_field_type(
265 BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION);
8deee039
PP
266}
267
7b33a0e0 268struct bt_field_type *bt_field_type_signed_enumeration_create(void)
8deee039 269{
7b33a0e0
PP
270 return create_enumeration_field_type(
271 BT_FIELD_TYPE_ID_SIGNED_ENUMERATION);
8deee039
PP
272}
273
7b33a0e0 274uint64_t bt_field_type_enumeration_get_mapping_count(struct bt_field_type *ft)
8deee039 275{
18acc6f8 276 struct bt_field_type_enumeration *enum_ft = (void *) ft;
8deee039 277
7b33a0e0
PP
278 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
279 BT_ASSERT_PRE_FT_IS_ENUM(ft, "Field type");
280 return (uint64_t) enum_ft->mappings->len;
8deee039
PP
281}
282
7b33a0e0
PP
283void bt_field_type_unsigned_enumeration_borrow_mapping_by_index(
284 struct bt_field_type *ft, uint64_t index,
285 const char **name,
286 struct bt_field_type_unsigned_enumeration_mapping_ranges **ranges)
8deee039 287{
7b33a0e0
PP
288 struct bt_field_type_enumeration *enum_ft = (void *) ft;
289 struct bt_field_type_enumeration_mapping *mapping;
290
291 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
292 BT_ASSERT_PRE_NON_NULL(name, "Name (output)");
293 BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)");
294 BT_ASSERT_PRE_VALID_INDEX(index, enum_ft->mappings->len);
295 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION,
296 "Field type");
297 mapping = BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(ft, index);
298 *name = mapping->label->str;
299 *ranges = (void *) mapping;
8deee039
PP
300}
301
7b33a0e0
PP
302void bt_field_type_signed_enumeration_borrow_mapping_by_index(
303 struct bt_field_type *ft, uint64_t index,
304 const char **name,
305 struct bt_field_type_signed_enumeration_mapping_ranges **ranges)
8deee039 306{
7b33a0e0
PP
307 struct bt_field_type_enumeration *enum_ft = (void *) ft;
308 struct bt_field_type_enumeration_mapping *mapping;
8deee039 309
7b33a0e0
PP
310 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
311 BT_ASSERT_PRE_NON_NULL(name, "Name (output)");
312 BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)");
313 BT_ASSERT_PRE_VALID_INDEX(index, enum_ft->mappings->len);
314 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_SIGNED_ENUMERATION,
315 "Field type");
316 mapping = BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(ft, index);
317 *name = mapping->label->str;
318 *ranges = (void *) mapping;
8deee039
PP
319}
320
7b33a0e0
PP
321static inline
322uint64_t get_enumeration_field_type_mapping_range_count(
323 struct bt_field_type_enumeration_mapping *mapping)
8deee039 324{
7b33a0e0
PP
325 BT_ASSERT_PRE_NON_NULL(mapping, "Ranges");
326 return (uint64_t) mapping->ranges->len;
8deee039
PP
327}
328
7b33a0e0
PP
329uint64_t bt_field_type_unsigned_enumeration_mapping_ranges_get_range_count(
330 struct bt_field_type_unsigned_enumeration_mapping_ranges *ranges)
8deee039 331{
7b33a0e0 332 return get_enumeration_field_type_mapping_range_count((void *) ranges);
8deee039
PP
333}
334
7b33a0e0
PP
335uint64_t bt_field_type_signed_enumeration_mapping_ranges_get_range_count(
336 struct bt_field_type_signed_enumeration_mapping_ranges *ranges)
8deee039 337{
7b33a0e0 338 return get_enumeration_field_type_mapping_range_count((void *) ranges);
8deee039
PP
339}
340
7b33a0e0
PP
341static inline
342void get_enumeration_field_type_mapping_range_at_index(
343 struct bt_field_type_enumeration_mapping *mapping,
344 uint64_t index, uint64_t *lower, uint64_t *upper)
8deee039 345{
7b33a0e0 346 struct bt_field_type_enumeration_mapping_range *range;
8deee039 347
7b33a0e0
PP
348 BT_ASSERT_PRE_NON_NULL(mapping, "Ranges");
349 BT_ASSERT_PRE_NON_NULL(lower, "Range's lower (output)");
350 BT_ASSERT_PRE_NON_NULL(upper, "Range's upper (output)");
351 BT_ASSERT_PRE_VALID_INDEX(index, mapping->ranges->len);
352 range = BT_FIELD_TYPE_ENUM_MAPPING_RANGE_AT_INDEX(mapping, index);
353 *lower = range->lower.u;
354 *upper = range->upper.u;
8deee039
PP
355}
356
7b33a0e0
PP
357void bt_field_type_unsigned_enumeration_mapping_ranges_get_range_by_index(
358 struct bt_field_type_unsigned_enumeration_mapping_ranges *ranges,
359 uint64_t index, uint64_t *lower, uint64_t *upper)
8deee039 360{
7b33a0e0
PP
361 get_enumeration_field_type_mapping_range_at_index((void *) ranges,
362 index, lower, upper);
8deee039
PP
363}
364
7b33a0e0
PP
365void bt_field_type_signed_enumeration_mapping_ranges_get_range_by_index(
366 struct bt_field_type_unsigned_enumeration_mapping_ranges *ranges,
367 uint64_t index, int64_t *lower, int64_t *upper)
8deee039 368{
7b33a0e0
PP
369 get_enumeration_field_type_mapping_range_at_index((void *) ranges,
370 index, (uint64_t *) lower, (uint64_t *) upper);
371}
8deee039 372
8deee039 373
8deee039 374
7b33a0e0
PP
375int bt_field_type_unsigned_enumeration_get_mapping_labels_by_value(
376 struct bt_field_type *ft, uint64_t value,
377 bt_field_type_enumeration_mapping_label_array *label_array,
378 uint64_t *count)
8deee039 379{
7b33a0e0
PP
380 struct bt_field_type_enumeration *enum_ft = (void *) ft;
381 uint64_t i;
8deee039 382
7b33a0e0
PP
383 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
384 BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)");
385 BT_ASSERT_PRE_NON_NULL(count, "Count (output)");
386 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION,
387 "Field type");
388 g_ptr_array_set_size(enum_ft->label_buf, 0);
389
390 for (i = 0; i < enum_ft->mappings->len; i++) {
391 uint64_t j;
392 struct bt_field_type_enumeration_mapping *mapping =
393 BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(enum_ft, i);
394
395 for (j = 0; j < mapping->ranges->len; j++) {
396 struct bt_field_type_enumeration_mapping_range *range =
397 BT_FIELD_TYPE_ENUM_MAPPING_RANGE_AT_INDEX(
398 mapping, j);
399
400 if (value >= range->lower.u &&
401 value <= range->upper.u) {
402 g_ptr_array_add(enum_ft->label_buf,
403 mapping->label->str);
404 break;
405 }
406 }
8deee039
PP
407 }
408
7b33a0e0
PP
409 *label_array = (void *) enum_ft->label_buf->pdata;
410 *count = (uint64_t) enum_ft->label_buf->len;
411 return 0;
8deee039
PP
412}
413
7b33a0e0
PP
414int bt_field_type_signed_enumeration_get_mapping_labels_by_value(
415 struct bt_field_type *ft, int64_t value,
416 bt_field_type_enumeration_mapping_label_array *label_array,
417 uint64_t *count)
a6918753 418{
7b33a0e0
PP
419 struct bt_field_type_enumeration *enum_ft = (void *) ft;
420 uint64_t i;
421
422 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
423 BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)");
424 BT_ASSERT_PRE_NON_NULL(count, "Count (output)");
425 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_SIGNED_ENUMERATION,
426 "Field type");
427 g_ptr_array_set_size(enum_ft->label_buf, 0);
428
429 for (i = 0; i < enum_ft->mappings->len; i++) {
430 uint64_t j;
431 struct bt_field_type_enumeration_mapping *mapping =
432 BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(enum_ft, i);
433
434 for (j = 0; j < mapping->ranges->len; j++) {
435 struct bt_field_type_enumeration_mapping_range *range =
436 BT_FIELD_TYPE_ENUM_MAPPING_RANGE_AT_INDEX(
437 mapping, j);
438
439 if (value >= range->lower.i &&
440 value <= range->upper.i) {
441 g_ptr_array_add(enum_ft->label_buf,
442 mapping->label->str);
443 break;
444 }
445 }
a6918753
PP
446 }
447
7b33a0e0
PP
448 *label_array = (void *) enum_ft->label_buf->pdata;
449 *count = (uint64_t) enum_ft->label_buf->len;
450 return 0;
a6918753 451}
8deee039 452
7b33a0e0
PP
453static inline
454int add_mapping_to_enumeration_field_type(struct bt_field_type *ft,
455 const char *label, uint64_t lower, uint64_t upper)
8deee039 456{
7b33a0e0 457 int ret = 0;
a6918753 458 uint64_t i;
7b33a0e0
PP
459 struct bt_field_type_enumeration *enum_ft = (void *) ft;
460 struct bt_field_type_enumeration_mapping *mapping = NULL;
461 struct bt_field_type_enumeration_mapping_range *range;
8deee039 462
7b33a0e0
PP
463 BT_ASSERT(ft);
464 BT_ASSERT_PRE_NON_NULL(label, "Label");
8deee039 465
7b33a0e0
PP
466 /* Find existing mapping identified by this label */
467 for (i = 0; i < enum_ft->mappings->len; i++) {
468 struct bt_field_type_enumeration_mapping *mapping_candidate =
469 BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(enum_ft, i);
a6918753 470
7b33a0e0
PP
471 if (strcmp(mapping_candidate->label->str, label) == 0) {
472 mapping = mapping_candidate;
473 break;
a6918753 474 }
a6918753
PP
475 }
476
7b33a0e0
PP
477 if (!mapping) {
478 /* Create new mapping for this label */
479 g_array_set_size(enum_ft->mappings, enum_ft->mappings->len + 1);
480 mapping = BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(enum_ft,
481 enum_ft->mappings->len - 1);
482 mapping->ranges = g_array_new(FALSE, TRUE,
483 sizeof(struct bt_field_type_enumeration_mapping_range));
484 if (!mapping->ranges) {
485 finalize_enumeration_field_type_mapping(mapping);
486 g_array_set_size(enum_ft->mappings,
487 enum_ft->mappings->len - 1);
488 ret = -1;
489 goto end;
490 }
8deee039 491
7b33a0e0
PP
492 mapping->label = g_string_new(label);
493 if (!mapping->label) {
494 finalize_enumeration_field_type_mapping(mapping);
495 g_array_set_size(enum_ft->mappings,
496 enum_ft->mappings->len - 1);
497 ret = -1;
498 goto end;
499 }
8deee039
PP
500 }
501
7b33a0e0
PP
502 /* Add range */
503 BT_ASSERT(mapping);
504 g_array_set_size(mapping->ranges, mapping->ranges->len + 1);
505 range = BT_FIELD_TYPE_ENUM_MAPPING_RANGE_AT_INDEX(mapping,
506 mapping->ranges->len - 1);
507 range->lower.u = lower;
508 range->upper.u = upper;
509 BT_LIB_LOGV("Added mapping to enumeration field type: "
510 "%![ft-]+F, label=\"%s\", lower-unsigned=%" PRIu64 ", "
511 "upper-unsigned=%" PRIu64, ft, label, lower, upper);
512
513end:
514 return ret;
8deee039
PP
515}
516
7b33a0e0
PP
517int bt_field_type_unsigned_enumeration_map_range(
518 struct bt_field_type *ft, const char *label,
519 uint64_t range_lower, uint64_t range_upper)
8deee039 520{
7b33a0e0 521 struct bt_field_type_enumeration *enum_ft = (void *) ft;
8deee039 522
7b33a0e0
PP
523 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
524 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION,
525 "Field type");
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_ft->common.range,
531 range_lower),
532 "Range's lower bound is outside the enumeration field type's value range: "
533 "%![ft-]+F, lower=%" PRIu64, ft, range_lower);
534 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_ft->common.range,
535 range_upper),
536 "Range's upper bound is outside the enumeration field type's value range: "
537 "%![ft-]+F, upper=%" PRIu64, ft, range_upper);
538 return add_mapping_to_enumeration_field_type(ft, label, range_lower,
539 range_upper);
540}
541
542int bt_field_type_signed_enumeration_map_range(
543 struct bt_field_type *ft, const char *label,
544 int64_t range_lower, int64_t range_upper)
545{
546 struct bt_field_type_enumeration *enum_ft = (void *) ft;
8deee039 547
7b33a0e0
PP
548 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
549 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_SIGNED_ENUMERATION,
550 "Field type");
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_ft->common.range,
556 range_lower),
557 "Range's lower bound is outside the enumeration field type's value range: "
558 "%![ft-]+F, lower=%" PRId64, ft, range_lower);
559 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_ft->common.range,
560 range_upper),
561 "Range's upper bound is outside the enumeration field type's value range: "
562 "%![ft-]+F, upper=%" PRId64, ft, range_upper);
563 return add_mapping_to_enumeration_field_type(ft, label, range_lower,
564 range_upper);
8deee039
PP
565}
566
a6918753 567static
7b33a0e0 568void destroy_real_field_type(struct bt_object *obj)
a6918753 569{
7b33a0e0
PP
570 BT_ASSERT(obj);
571 BT_LIB_LOGD("Destroying real field type object: %!+F", obj);
572 g_free(obj);
a6918753
PP
573}
574
7b33a0e0 575struct bt_field_type *bt_field_type_real_create(void)
8deee039 576{
7b33a0e0 577 struct bt_field_type_real *real_ft = NULL;
8deee039 578
7b33a0e0
PP
579 BT_LOGD_STR("Creating default real field type object.");
580 real_ft = g_new0(struct bt_field_type_real, 1);
581 if (!real_ft) {
582 BT_LOGE_STR("Failed to allocate one real field type.");
583 goto error;
a6918753
PP
584 }
585
7b33a0e0
PP
586 init_field_type((void *) real_ft, BT_FIELD_TYPE_ID_REAL,
587 destroy_real_field_type);
588 BT_LIB_LOGD("Created real field type object: %!+F", real_ft);
589 goto end;
a6918753 590
7b33a0e0
PP
591error:
592 BT_PUT(real_ft);
a6918753 593
7b33a0e0
PP
594end:
595 return (void *) real_ft;
8deee039
PP
596}
597
7b33a0e0 598bt_bool bt_field_type_real_is_single_precision(struct bt_field_type *ft)
b92ddaaa 599{
7b33a0e0
PP
600 struct bt_field_type_real *real_ft = (void *) ft;
601
602 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
603 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_REAL, "Field type");
604 return real_ft->is_single_precision;
b92ddaaa
JG
605}
606
7b33a0e0
PP
607int bt_field_type_real_set_is_single_precision(struct bt_field_type *ft,
608 bt_bool is_single_precision)
b92ddaaa 609{
7b33a0e0
PP
610 struct bt_field_type_real *real_ft = (void *) ft;
611
612 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
613 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_REAL, "Field type");
614 BT_ASSERT_PRE_FT_HOT(ft, "Field type");
615 real_ft->is_single_precision = (bool) is_single_precision;
616 BT_LIB_LOGV("Set real field type's \"is single precision\" property: "
617 "%!+F", ft);
618 return 0;
b92ddaaa
JG
619}
620
273b65be 621static
7b33a0e0
PP
622int init_named_field_types_container(
623 struct bt_field_type_named_field_types_container *ft,
624 enum bt_field_type_id id, bt_object_release_func release_func)
273b65be
JG
625{
626 int ret = 0;
7b33a0e0
PP
627
628 init_field_type((void *) ft, id, release_func);
629 ft->named_fts = g_array_new(FALSE, TRUE,
630 sizeof(struct bt_named_field_type));
631 if (!ft->named_fts) {
632 BT_LOGE_STR("Failed to allocate a GArray.");
273b65be
JG
633 ret = -1;
634 goto end;
635 }
636
7b33a0e0
PP
637 ft->name_to_index = g_hash_table_new(g_str_hash, g_str_equal);
638 if (!ft->name_to_index) {
639 BT_LOGE_STR("Failed to allocate a GHashTable.");
640 ret = -1;
641 goto end;
273b65be
JG
642 }
643
273b65be
JG
644end:
645 return ret;
646}
647
18acc6f8 648static
7b33a0e0 649void finalize_named_field_type(struct bt_named_field_type *named_ft)
b3dbeb52 650{
7b33a0e0
PP
651 BT_ASSERT(named_ft);
652 BT_LIB_LOGD("Finalizing named field type: "
653 "addr=%p, name=\"%s\", %![ft-]+F",
654 named_ft, named_ft->name ? named_ft->name->str : NULL,
655 named_ft->ft);
b3dbeb52 656
7b33a0e0
PP
657 if (named_ft->name) {
658 g_string_free(named_ft->name, TRUE);
b3dbeb52
PP
659 }
660
7b33a0e0
PP
661 BT_LOGD_STR("Putting named field type's field type.");
662 bt_put(named_ft->ft);
b3dbeb52
PP
663}
664
d49e1284 665static
7b33a0e0
PP
666void finalize_named_field_types_container(
667 struct bt_field_type_named_field_types_container *ft)
d49e1284 668{
7b33a0e0 669 uint64_t i;
d49e1284 670
7b33a0e0 671 BT_ASSERT(ft);
d49e1284 672
7b33a0e0
PP
673 if (ft->named_fts) {
674 for (i = 0; i < ft->named_fts->len; i++) {
675 finalize_named_field_type(
676 &g_array_index(ft->named_fts,
677 struct bt_named_field_type, i));
d49e1284 678 }
7b33a0e0
PP
679
680 g_array_free(ft->named_fts, TRUE);
d49e1284 681 }
dfc1504d 682
7b33a0e0
PP
683 if (ft->name_to_index) {
684 g_hash_table_destroy(ft->name_to_index);
dfc1504d 685 }
d49e1284
JG
686}
687
18acc6f8 688static
7b33a0e0 689void destroy_structure_field_type(struct bt_object *obj)
9ce21c30 690{
7b33a0e0
PP
691 BT_ASSERT(obj);
692 BT_LIB_LOGD("Destroying string field type object: %!+F", obj);
693 finalize_named_field_types_container((void *) obj);
694 g_free(obj);
695}
9ce21c30 696
7b33a0e0
PP
697struct bt_field_type *bt_field_type_structure_create(void)
698{
699 int ret;
700 struct bt_field_type_structure *struct_ft = NULL;
701
702 BT_LOGD_STR("Creating default structure field type object.");
703 struct_ft = g_new0(struct bt_field_type_structure, 1);
704 if (!struct_ft) {
705 BT_LOGE_STR("Failed to allocate one structure field type.");
706 goto error;
81e36fac 707 }
9ce21c30 708
7b33a0e0
PP
709 ret = init_named_field_types_container((void *) struct_ft,
710 BT_FIELD_TYPE_ID_STRUCTURE, destroy_structure_field_type);
711 if (ret) {
712 goto error;
4e8304f7 713 }
81e36fac 714
7b33a0e0
PP
715 BT_LIB_LOGD("Created structure field type object: %!+F", struct_ft);
716 goto end;
717
718error:
719 BT_PUT(struct_ft);
720
81e36fac 721end:
7b33a0e0 722 return (void *) struct_ft;
81e36fac
PP
723}
724
18acc6f8 725static
7b33a0e0
PP
726int append_named_field_type_to_container_field_type(
727 struct bt_field_type_named_field_types_container *container_ft,
728 const char *name, struct bt_field_type *ft)
81e36fac
PP
729{
730 int ret = 0;
7b33a0e0
PP
731 struct bt_named_field_type *named_ft;
732 GString *name_str;
81e36fac 733
7b33a0e0
PP
734 BT_ASSERT(container_ft);
735 BT_ASSERT_PRE_FT_HOT(container_ft, "Field type");
736 BT_ASSERT_PRE_NON_NULL(name, "Name");
737 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
738 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_ft->name_to_index,
739 name),
740 "Duplicate member/option name in structure/variant field type: "
741 "%![container-ft-]+F, name=\"%s\"", container_ft, name);
742 name_str = g_string_new(name);
743 if (!name_str) {
744 BT_LOGE_STR("Failed to allocate a GString.");
81e36fac
PP
745 ret = -1;
746 goto end;
5d161ecc 747 }
81e36fac 748
7b33a0e0
PP
749 g_array_set_size(container_ft->named_fts,
750 container_ft->named_fts->len + 1);
751 named_ft = &g_array_index(container_ft->named_fts,
752 struct bt_named_field_type, container_ft->named_fts->len - 1);
753 named_ft->name = name_str;
754 named_ft->ft = bt_get(ft);
755 g_hash_table_insert(container_ft->name_to_index, named_ft->name->str,
756 GUINT_TO_POINTER(container_ft->named_fts->len - 1));
757 bt_field_type_freeze(ft);
81e36fac
PP
758
759end:
81e36fac
PP
760 return ret;
761}
762
7b33a0e0
PP
763int bt_field_type_structure_append_member(struct bt_field_type *ft,
764 const char *name, struct bt_field_type *member_ft)
81e36fac 765{
7b33a0e0
PP
766 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
767 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_STRUCTURE, "Field type");
768 return append_named_field_type_to_container_field_type((void *) ft,
769 name, member_ft);
770}
81e36fac 771
7b33a0e0
PP
772uint64_t bt_field_type_structure_get_member_count(struct bt_field_type *ft)
773{
774 struct bt_field_type_structure *struct_ft = (void *) ft;
81e36fac 775
7b33a0e0
PP
776 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
777 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_STRUCTURE, "Field type");
778 return (uint64_t) struct_ft->common.named_fts->len;
81e36fac
PP
779}
780
18acc6f8 781static
7b33a0e0
PP
782void borrow_named_field_type_from_container_field_type_at_index(
783 struct bt_field_type_named_field_types_container *ft,
784 uint64_t index, const char **name,
785 struct bt_field_type **out_ft)
81e36fac 786{
7b33a0e0 787 struct bt_named_field_type *named_ft;
81e36fac 788
7b33a0e0
PP
789 BT_ASSERT(ft);
790 BT_ASSERT_PRE_NON_NULL(name, "Name");
791 BT_ASSERT_PRE_NON_NULL(out_ft, "Field type (output)");
792 BT_ASSERT_PRE_VALID_INDEX(index, ft->named_fts->len);
793 named_ft = BT_FIELD_TYPE_NAMED_FT_AT_INDEX(ft, index);
794 *name = named_ft->name->str;
795 *out_ft = named_ft->ft;
81e36fac
PP
796}
797
7b33a0e0
PP
798void bt_field_type_structure_borrow_member_by_index(
799 struct bt_field_type *ft, uint64_t index,
800 const char **name, struct bt_field_type **out_ft)
96e8f959 801{
7b33a0e0
PP
802 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
803 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_STRUCTURE, "Field type");
804 borrow_named_field_type_from_container_field_type_at_index((void *) ft,
805 index, name, out_ft);
96e8f959
MD
806}
807
18acc6f8 808static
7b33a0e0
PP
809struct bt_field_type *borrow_field_type_from_container_field_type_by_name(
810 struct bt_field_type_named_field_types_container *ft,
811 const char *name)
81e36fac 812{
7b33a0e0
PP
813 struct bt_field_type *ret_ft = NULL;
814 struct bt_named_field_type *named_ft;
815 gpointer orig_key;
816 gpointer value;
96e8f959 817
7b33a0e0
PP
818 BT_ASSERT(ft);
819 BT_ASSERT_PRE_NON_NULL(name, "Name");
820 if (!g_hash_table_lookup_extended(ft->name_to_index, name, &orig_key,
821 &value)) {
81e36fac
PP
822 goto end;
823 }
824
7b33a0e0
PP
825 named_ft = BT_FIELD_TYPE_NAMED_FT_AT_INDEX(ft,
826 GPOINTER_TO_UINT(value));
827 ret_ft = named_ft->ft;
81e36fac
PP
828
829end:
7b33a0e0 830 return ret_ft;
81e36fac
PP
831}
832
7b33a0e0
PP
833struct bt_field_type *bt_field_type_structure_borrow_member_field_type_by_name(
834 struct bt_field_type *ft, const char *name)
81e36fac 835{
7b33a0e0
PP
836 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
837 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_STRUCTURE, "Field type");
838 return borrow_field_type_from_container_field_type_by_name((void *) ft,
839 name);
840}
81e36fac 841
7b33a0e0
PP
842static
843void destroy_variant_field_type(struct bt_object *obj)
844{
845 struct bt_field_type_variant *ft = (void *) obj;
81e36fac 846
7b33a0e0
PP
847 BT_ASSERT(ft);
848 BT_LIB_LOGD("Destroying variant field type object: %!+F", ft);
849 finalize_named_field_types_container((void *) ft);
850 BT_LOGD_STR("Putting selector field path.");
851 bt_put(ft->selector_field_path);
852 g_free(ft);
9ce21c30
JG
853}
854
7b33a0e0 855struct bt_field_type *bt_field_type_variant_create(void)
273b65be 856{
7b33a0e0
PP
857 int ret;
858 struct bt_field_type_variant *var_ft = NULL;
4e8304f7 859
7b33a0e0
PP
860 BT_LOGD_STR("Creating default variant field type object.");
861 var_ft = g_new0(struct bt_field_type_variant, 1);
862 if (!var_ft) {
863 BT_LOGE_STR("Failed to allocate one variant field type.");
8deee039
PP
864 goto error;
865 }
866
7b33a0e0
PP
867 ret = init_named_field_types_container((void *) var_ft,
868 BT_FIELD_TYPE_ID_VARIANT, destroy_variant_field_type);
869 if (ret) {
8deee039 870 goto error;
273b65be
JG
871 }
872
7b33a0e0 873 BT_LIB_LOGD("Created variant field type object: %!+F", var_ft);
8deee039
PP
874 goto end;
875
876error:
7b33a0e0 877 BT_PUT(var_ft);
8deee039
PP
878
879end:
7b33a0e0 880 return (void *) var_ft;
273b65be
JG
881}
882
7b33a0e0
PP
883int bt_field_type_variant_set_selector_field_type(
884 struct bt_field_type *ft, struct bt_field_type *selector_ft)
b92ddaaa 885{
7b33a0e0 886 struct bt_field_type_variant *var_ft = (void *) ft;
8deee039 887
7b33a0e0
PP
888 BT_ASSERT_PRE_NON_NULL(ft, "Variant field type");
889 BT_ASSERT_PRE_NON_NULL(selector_ft, "Selector field type");
890 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_VARIANT, "Field type");
891 BT_ASSERT_PRE_FT_IS_ENUM(selector_ft, "Selector field type");
892 BT_ASSERT_PRE_FT_HOT(ft, "Variant field type");
893 var_ft->selector_ft = selector_ft;
894 bt_field_type_freeze(selector_ft);
895 return 0;
8deee039 896}
b92ddaaa 897
7b33a0e0
PP
898int bt_field_type_variant_append_option(struct bt_field_type *ft,
899 const char *name, struct bt_field_type *option_ft)
b92ddaaa 900{
7b33a0e0
PP
901 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
902 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_VARIANT, "Field type");
903 return append_named_field_type_to_container_field_type((void *) ft,
904 name, option_ft);
905}
b92ddaaa 906
7b33a0e0
PP
907struct bt_field_type *bt_field_type_variant_borrow_option_field_type_by_name(
908 struct bt_field_type *ft, const char *name)
909{
8deee039 910 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
7b33a0e0
PP
911 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_VARIANT, "Field type");
912 return borrow_field_type_from_container_field_type_by_name((void *) ft,
913 name);
b92ddaaa
JG
914}
915
7b33a0e0 916uint64_t bt_field_type_variant_get_option_count(struct bt_field_type *ft)
273b65be 917{
7b33a0e0 918 struct bt_field_type_variant *var_ft = (void *) ft;
273b65be 919
7b33a0e0
PP
920 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
921 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_VARIANT, "Field type");
922 return (uint64_t) var_ft->common.named_fts->len;
923}
4e8304f7 924
7b33a0e0
PP
925void bt_field_type_variant_borrow_option_by_index(
926 struct bt_field_type *ft, uint64_t index,
927 const char **name, struct bt_field_type **out_ft)
928{
929 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
930 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_VARIANT, "Field type");
931 borrow_named_field_type_from_container_field_type_at_index((void *) ft,
932 index, name, out_ft);
933}
4e8304f7 934
7b33a0e0
PP
935struct bt_field_path *bt_field_type_variant_borrow_selector_field_path(
936 struct bt_field_type *ft)
937{
938 struct bt_field_type_variant *var_ft = (void *) ft;
273b65be 939
7b33a0e0
PP
940 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
941 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_VARIANT,
942 "Field type");
943 return var_ft->selector_field_path;
944}
8deee039 945
7b33a0e0
PP
946static
947void init_array_field_type(struct bt_field_type_array *ft,
948 enum bt_field_type_id id, bt_object_release_func release_func,
949 struct bt_field_type *element_ft)
950{
951 BT_ASSERT(element_ft);
952 init_field_type((void *) ft, id, release_func);
953 ft->element_ft = bt_get(element_ft);
954 bt_field_type_freeze(element_ft);
273b65be
JG
955}
956
7b33a0e0
PP
957static
958void finalize_array_field_type(struct bt_field_type_array *array_ft)
c3c35de4 959{
7b33a0e0
PP
960 BT_ASSERT(array_ft);
961 BT_LOGD_STR("Putting element field type.");
962 bt_put(array_ft->element_ft);
963}
c3c35de4 964
7b33a0e0
PP
965static
966void destroy_static_array_field_type(struct bt_object *obj)
967{
968 BT_ASSERT(obj);
969 BT_LIB_LOGD("Destroying static array field type object: %!+F", obj);
970 finalize_array_field_type((void *) obj);
971 g_free(obj);
972}
4e8304f7 973
7b33a0e0
PP
974struct bt_field_type *bt_field_type_static_array_create(
975 struct bt_field_type *element_ft, uint64_t length)
976{
977 struct bt_field_type_static_array *array_ft = NULL;
4e8304f7 978
7b33a0e0
PP
979 BT_ASSERT_PRE_NON_NULL(element_ft, "Element field type");
980 BT_LOGD_STR("Creating default static array field type object.");
981 array_ft = g_new0(struct bt_field_type_static_array, 1);
982 if (!array_ft) {
983 BT_LOGE_STR("Failed to allocate one static array field type.");
984 goto error;
4e8304f7
PP
985 }
986
7b33a0e0
PP
987 init_array_field_type((void *) array_ft, BT_FIELD_TYPE_ID_STATIC_ARRAY,
988 destroy_static_array_field_type, element_ft);
989 array_ft->length = length;
990 BT_LIB_LOGD("Created static array field type object: %!+F", array_ft);
991 goto end;
c3c35de4 992
7b33a0e0
PP
993error:
994 BT_PUT(array_ft);
8deee039 995
c3c35de4 996end:
7b33a0e0 997 return (void *) array_ft;
c3c35de4
JD
998}
999
7b33a0e0 1000struct bt_field_type *bt_field_type_array_borrow_element_field_type(
18acc6f8 1001 struct bt_field_type *ft)
b92ddaaa 1002{
7b33a0e0
PP
1003 struct bt_field_type_array *array_ft = (void *) ft;
1004
1005 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
1006 BT_ASSERT_PRE_FT_IS_ARRAY(ft, "Field type");
1007 return array_ft->element_ft;
1008}
1009
1010uint64_t bt_field_type_static_array_get_length(struct bt_field_type *ft)
1011{
1012 struct bt_field_type_static_array *array_ft = (void *) ft;
8deee039
PP
1013
1014 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
7b33a0e0 1015 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_STATIC_ARRAY,
8deee039 1016 "Field type");
7b33a0e0 1017 return (uint64_t) array_ft->length;
8deee039 1018}
b92ddaaa 1019
7b33a0e0
PP
1020static
1021void destroy_dynamic_array_field_type(struct bt_object *obj)
273b65be 1022{
7b33a0e0 1023 struct bt_field_type_dynamic_array *ft = (void *) obj;
273b65be 1024
7b33a0e0
PP
1025 BT_ASSERT(ft);
1026 BT_LIB_LOGD("Destroying dynamic array field type object: %!+F", ft);
1027 finalize_array_field_type((void *) ft);
1028 BT_LOGD_STR("Putting length field path.");
1029 bt_put(ft->length_field_path);
1030 g_free(ft);
1031}
4e8304f7 1032
7b33a0e0
PP
1033struct bt_field_type *bt_field_type_dynamic_array_create(
1034 struct bt_field_type *element_ft)
1035{
1036 struct bt_field_type_dynamic_array *array_ft = NULL;
4e8304f7 1037
7b33a0e0
PP
1038 BT_ASSERT_PRE_NON_NULL(element_ft, "Element field type");
1039 BT_LOGD_STR("Creating default dynamic array field type object.");
1040 array_ft = g_new0(struct bt_field_type_dynamic_array, 1);
1041 if (!array_ft) {
1042 BT_LOGE_STR("Failed to allocate one dynamic array field type.");
1043 goto error;
273b65be
JG
1044 }
1045
7b33a0e0
PP
1046 init_array_field_type((void *) array_ft, BT_FIELD_TYPE_ID_DYNAMIC_ARRAY,
1047 destroy_dynamic_array_field_type, element_ft);
1048 BT_LIB_LOGD("Created dynamic array field type object: %!+F", array_ft);
1049 goto end;
dfc1504d 1050
7b33a0e0
PP
1051error:
1052 BT_PUT(array_ft);
dfc1504d 1053
273b65be 1054end:
7b33a0e0
PP
1055 return (void *) array_ft;
1056}
1057
1058int bt_field_type_dynamic_array_set_length_field_type(struct bt_field_type *ft,
1059 struct bt_field_type *length_ft)
1060{
1061 struct bt_field_type_dynamic_array *array_ft = (void *) ft;
1062
1063 BT_ASSERT_PRE_NON_NULL(ft, "Dynamic array field type");
1064 BT_ASSERT_PRE_NON_NULL(length_ft, "Length field type");
1065 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_DYNAMIC_ARRAY,
1066 "Field type");
1067 BT_ASSERT_PRE_FT_IS_UNSIGNED_INT(length_ft, "Length field type");
1068 BT_ASSERT_PRE_FT_HOT(ft, "Dynamic array field type");
1069 array_ft->length_ft = length_ft;
1070 bt_field_type_freeze(length_ft);
1071 return 0;
273b65be
JG
1072}
1073
7b33a0e0 1074struct bt_field_path *bt_field_type_dynamic_array_borrow_length_field_path(
18acc6f8 1075 struct bt_field_type *ft)
b92ddaaa 1076{
7b33a0e0 1077 struct bt_field_type_dynamic_array *seq_ft = (void *) ft;
8deee039
PP
1078
1079 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
7b33a0e0 1080 BT_ASSERT_PRE_FT_HAS_ID(ft, BT_FIELD_TYPE_ID_DYNAMIC_ARRAY,
8deee039 1081 "Field type");
7b33a0e0 1082 return seq_ft->length_field_path;
8deee039 1083}
b92ddaaa 1084
7b33a0e0
PP
1085static
1086void destroy_string_field_type(struct bt_object *obj)
273b65be 1087{
7b33a0e0
PP
1088 BT_ASSERT(obj);
1089 BT_LIB_LOGD("Destroying string field type object: %!+F", obj);
1090 g_free(obj);
1091}
273b65be 1092
7b33a0e0
PP
1093struct bt_field_type *bt_field_type_string_create(void)
1094{
1095 struct bt_field_type_string *string_ft = NULL;
4e8304f7 1096
7b33a0e0
PP
1097 BT_LOGD_STR("Creating default string field type object.");
1098 string_ft = g_new0(struct bt_field_type_string, 1);
1099 if (!string_ft) {
1100 BT_LOGE_STR("Failed to allocate one string field type.");
1101 goto error;
4e8304f7
PP
1102 }
1103
7b33a0e0
PP
1104 init_field_type((void *) string_ft, BT_FIELD_TYPE_ID_STRING,
1105 destroy_string_field_type);
1106 BT_LIB_LOGD("Created string field type object: %!+F", string_ft);
1107 goto end;
4e8304f7 1108
7b33a0e0
PP
1109error:
1110 BT_PUT(string_ft);
4e8304f7 1111
24724933 1112end:
7b33a0e0 1113 return (void *) string_ft;
8deee039 1114}
265e809c 1115
8deee039 1116BT_HIDDEN
7b33a0e0 1117void _bt_field_type_freeze(struct bt_field_type *ft)
8deee039 1118{
7b33a0e0
PP
1119 /*
1120 * Element/member/option field types are frozen when added to
1121 * their owner.
1122 */
1123 BT_ASSERT(ft);
1124 ft->frozen = true;
265e809c
PP
1125}
1126
8deee039 1127BT_HIDDEN
7b33a0e0 1128void _bt_field_type_make_part_of_trace(struct bt_field_type *ft)
265e809c 1129{
7b33a0e0
PP
1130 BT_ASSERT(ft);
1131 BT_ASSERT_PRE(!ft->part_of_trace,
1132 "Field type is already part of a trace: %!+F", ft);
1133 ft->part_of_trace = true;
265e809c 1134
8deee039 1135 switch (ft->id) {
7b33a0e0 1136 case BT_FIELD_TYPE_ID_STRUCTURE:
8deee039 1137 case BT_FIELD_TYPE_ID_VARIANT:
8deee039 1138 {
7b33a0e0
PP
1139 struct bt_field_type_named_field_types_container *container_ft =
1140 (void *) ft;
1141 uint64_t i;
265e809c 1142
7b33a0e0
PP
1143 for (i = 0; i < container_ft->named_fts->len; i++) {
1144 struct bt_named_field_type *named_ft =
1145 BT_FIELD_TYPE_NAMED_FT_AT_INDEX(
1146 container_ft, i);
265e809c 1147
7b33a0e0 1148 bt_field_type_make_part_of_trace(named_ft->ft);
8deee039 1149 }
265e809c 1150
8deee039 1151 break;
265e809c 1152 }
7b33a0e0
PP
1153 case BT_FIELD_TYPE_ID_STATIC_ARRAY:
1154 case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY:
8deee039 1155 {
7b33a0e0 1156 struct bt_field_type_array *array_ft = (void *) ft;
265e809c 1157
7b33a0e0 1158 bt_field_type_make_part_of_trace(array_ft->element_ft);
8deee039
PP
1159 break;
1160 }
1161 default:
1162 break;
265e809c 1163 }
b011f6b0 1164}
This page took 0.140477 seconds and 4 git commands to generate.