| 1 | /* |
| 2 | * field-types.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Event Types |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 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 | |
| 29 | #define BT_LOG_TAG "FIELD-TYPES" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 33 | #include <babeltrace/ctf-ir/field-path-internal.h> |
| 34 | #include <babeltrace/ctf-ir/utils.h> |
| 35 | #include <babeltrace/ref.h> |
| 36 | #include <babeltrace/ctf-ir/clock-class.h> |
| 37 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
| 38 | #include <babeltrace/ctf-writer/writer-internal.h> |
| 39 | #include <babeltrace/object-internal.h> |
| 40 | #include <babeltrace/ref.h> |
| 41 | #include <babeltrace/compiler-internal.h> |
| 42 | #include <babeltrace/endian-internal.h> |
| 43 | #include <float.h> |
| 44 | #include <inttypes.h> |
| 45 | #include <stdlib.h> |
| 46 | |
| 47 | struct range_overlap_query { |
| 48 | union { |
| 49 | uint64_t _unsigned; |
| 50 | int64_t _signed; |
| 51 | } range_start; |
| 52 | |
| 53 | union { |
| 54 | uint64_t _unsigned; |
| 55 | int64_t _signed; |
| 56 | } range_end; |
| 57 | int overlaps; |
| 58 | GQuark mapping_name; |
| 59 | }; |
| 60 | |
| 61 | static |
| 62 | void bt_ctf_field_type_destroy(struct bt_object *); |
| 63 | static |
| 64 | void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *); |
| 65 | static |
| 66 | void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *); |
| 67 | static |
| 68 | void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *); |
| 69 | static |
| 70 | void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *); |
| 71 | static |
| 72 | void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *); |
| 73 | static |
| 74 | void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *); |
| 75 | static |
| 76 | void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *); |
| 77 | static |
| 78 | void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *); |
| 79 | |
| 80 | static |
| 81 | void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = { |
| 82 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_destroy, |
| 83 | [BT_CTF_FIELD_TYPE_ID_ENUM] = |
| 84 | bt_ctf_field_type_enumeration_destroy, |
| 85 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = |
| 86 | bt_ctf_field_type_floating_point_destroy, |
| 87 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_destroy, |
| 88 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_destroy, |
| 89 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_destroy, |
| 90 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_destroy, |
| 91 | [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_destroy, |
| 92 | }; |
| 93 | |
| 94 | static |
| 95 | void generic_field_type_freeze(struct bt_ctf_field_type *); |
| 96 | static |
| 97 | void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *); |
| 98 | static |
| 99 | void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *); |
| 100 | static |
| 101 | void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *); |
| 102 | static |
| 103 | void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *); |
| 104 | static |
| 105 | void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *); |
| 106 | static |
| 107 | void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *); |
| 108 | |
| 109 | static |
| 110 | type_freeze_func const type_freeze_funcs[] = { |
| 111 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_freeze, |
| 112 | [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_freeze, |
| 113 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = generic_field_type_freeze, |
| 114 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_freeze, |
| 115 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_freeze, |
| 116 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_freeze, |
| 117 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_freeze, |
| 118 | [BT_CTF_FIELD_TYPE_ID_STRING] = generic_field_type_freeze, |
| 119 | }; |
| 120 | |
| 121 | static |
| 122 | int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *, |
| 123 | struct metadata_context *); |
| 124 | static |
| 125 | int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *, |
| 126 | struct metadata_context *); |
| 127 | static |
| 128 | int bt_ctf_field_type_floating_point_serialize( |
| 129 | struct bt_ctf_field_type *, struct metadata_context *); |
| 130 | static |
| 131 | int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *, |
| 132 | struct metadata_context *); |
| 133 | static |
| 134 | int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *, |
| 135 | struct metadata_context *); |
| 136 | static |
| 137 | int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *, |
| 138 | struct metadata_context *); |
| 139 | static |
| 140 | int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *, |
| 141 | struct metadata_context *); |
| 142 | static |
| 143 | int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *, |
| 144 | struct metadata_context *); |
| 145 | |
| 146 | static |
| 147 | type_serialize_func const type_serialize_funcs[] = { |
| 148 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_serialize, |
| 149 | [BT_CTF_FIELD_TYPE_ID_ENUM] = |
| 150 | bt_ctf_field_type_enumeration_serialize, |
| 151 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = |
| 152 | bt_ctf_field_type_floating_point_serialize, |
| 153 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = |
| 154 | bt_ctf_field_type_structure_serialize, |
| 155 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_serialize, |
| 156 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_serialize, |
| 157 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_serialize, |
| 158 | [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_serialize, |
| 159 | }; |
| 160 | |
| 161 | static |
| 162 | void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *, |
| 163 | enum bt_ctf_byte_order byte_order); |
| 164 | static |
| 165 | void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *, |
| 166 | enum bt_ctf_byte_order byte_order); |
| 167 | static |
| 168 | void bt_ctf_field_type_floating_point_set_byte_order( |
| 169 | struct bt_ctf_field_type *, enum bt_ctf_byte_order byte_order); |
| 170 | static |
| 171 | void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *, |
| 172 | enum bt_ctf_byte_order byte_order); |
| 173 | static |
| 174 | void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *, |
| 175 | enum bt_ctf_byte_order byte_order); |
| 176 | static |
| 177 | void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *, |
| 178 | enum bt_ctf_byte_order byte_order); |
| 179 | static |
| 180 | void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *, |
| 181 | enum bt_ctf_byte_order byte_order); |
| 182 | |
| 183 | static |
| 184 | void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *, |
| 185 | enum bt_ctf_byte_order) = { |
| 186 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_set_byte_order, |
| 187 | [BT_CTF_FIELD_TYPE_ID_ENUM] = |
| 188 | bt_ctf_field_type_enumeration_set_byte_order, |
| 189 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = |
| 190 | bt_ctf_field_type_floating_point_set_byte_order, |
| 191 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = |
| 192 | bt_ctf_field_type_structure_set_byte_order, |
| 193 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_set_byte_order, |
| 194 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_set_byte_order, |
| 195 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order, |
| 196 | [BT_CTF_FIELD_TYPE_ID_STRING] = NULL, |
| 197 | }; |
| 198 | |
| 199 | static |
| 200 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( |
| 201 | struct bt_ctf_field_type *); |
| 202 | static |
| 203 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( |
| 204 | struct bt_ctf_field_type *); |
| 205 | static |
| 206 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( |
| 207 | struct bt_ctf_field_type *); |
| 208 | static |
| 209 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( |
| 210 | struct bt_ctf_field_type *); |
| 211 | static |
| 212 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( |
| 213 | struct bt_ctf_field_type *); |
| 214 | static |
| 215 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy( |
| 216 | struct bt_ctf_field_type *); |
| 217 | static |
| 218 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( |
| 219 | struct bt_ctf_field_type *); |
| 220 | static |
| 221 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( |
| 222 | struct bt_ctf_field_type *); |
| 223 | |
| 224 | static |
| 225 | struct bt_ctf_field_type *(* const type_copy_funcs[])( |
| 226 | struct bt_ctf_field_type *) = { |
| 227 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_copy, |
| 228 | [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_copy, |
| 229 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_copy, |
| 230 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_copy, |
| 231 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_copy, |
| 232 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_copy, |
| 233 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_copy, |
| 234 | [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_copy, |
| 235 | }; |
| 236 | |
| 237 | static |
| 238 | int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *, |
| 239 | struct bt_ctf_field_type *); |
| 240 | static |
| 241 | int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *, |
| 242 | struct bt_ctf_field_type *); |
| 243 | static |
| 244 | int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *, |
| 245 | struct bt_ctf_field_type *); |
| 246 | static |
| 247 | int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *, |
| 248 | struct bt_ctf_field_type *); |
| 249 | static |
| 250 | int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *, |
| 251 | struct bt_ctf_field_type *); |
| 252 | static |
| 253 | int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *, |
| 254 | struct bt_ctf_field_type *); |
| 255 | static |
| 256 | int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *, |
| 257 | struct bt_ctf_field_type *); |
| 258 | static |
| 259 | int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *, |
| 260 | struct bt_ctf_field_type *); |
| 261 | |
| 262 | static |
| 263 | int (* const type_compare_funcs[])(struct bt_ctf_field_type *, |
| 264 | struct bt_ctf_field_type *) = { |
| 265 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_compare, |
| 266 | [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_compare, |
| 267 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_compare, |
| 268 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_compare, |
| 269 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_compare, |
| 270 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_compare, |
| 271 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_compare, |
| 272 | [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_compare, |
| 273 | }; |
| 274 | |
| 275 | static |
| 276 | int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *); |
| 277 | static |
| 278 | int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *); |
| 279 | static |
| 280 | int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *); |
| 281 | static |
| 282 | int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *); |
| 283 | static |
| 284 | int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *); |
| 285 | static |
| 286 | int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *); |
| 287 | |
| 288 | static |
| 289 | int (* const type_validate_funcs[])(struct bt_ctf_field_type *) = { |
| 290 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_validate, |
| 291 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = NULL, |
| 292 | [BT_CTF_FIELD_TYPE_ID_STRING] = NULL, |
| 293 | [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_validate, |
| 294 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_validate, |
| 295 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_validate, |
| 296 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_validate, |
| 297 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_validate, |
| 298 | }; |
| 299 | |
| 300 | static |
| 301 | void destroy_enumeration_mapping(struct enumeration_mapping *mapping) |
| 302 | { |
| 303 | g_free(mapping); |
| 304 | } |
| 305 | |
| 306 | static |
| 307 | void destroy_structure_field(struct structure_field *field) |
| 308 | { |
| 309 | if (!field) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | BT_LOGD("Destroying structure/variant field type's field object: " |
| 314 | "addr=%p, field-ft-addr=%p, field-name=\"%s\"", |
| 315 | field, field->type, g_quark_to_string(field->name)); |
| 316 | BT_LOGD_STR("Putting field type."); |
| 317 | bt_put(field->type); |
| 318 | g_free(field); |
| 319 | } |
| 320 | |
| 321 | static |
| 322 | void check_ranges_overlap(gpointer element, gpointer query) |
| 323 | { |
| 324 | struct enumeration_mapping *mapping = element; |
| 325 | struct range_overlap_query *overlap_query = query; |
| 326 | |
| 327 | if (mapping->range_start._signed <= overlap_query->range_end._signed |
| 328 | && overlap_query->range_start._signed <= |
| 329 | mapping->range_end._signed) { |
| 330 | overlap_query->overlaps = 1; |
| 331 | overlap_query->mapping_name = mapping->string; |
| 332 | } |
| 333 | |
| 334 | overlap_query->overlaps |= |
| 335 | mapping->string == overlap_query->mapping_name; |
| 336 | |
| 337 | if (overlap_query->overlaps) { |
| 338 | BT_LOGV("Overlapping enumeration field type mappings: " |
| 339 | "mapping-name=\"%s\", " |
| 340 | "mapping-a-range-start=%" PRId64 ", " |
| 341 | "mapping-a-range-end=%" PRId64 ", " |
| 342 | "mapping-b-range-start=%" PRId64 ", " |
| 343 | "mapping-b-range-end=%" PRId64, |
| 344 | g_quark_to_string(mapping->string), |
| 345 | mapping->range_start._signed, |
| 346 | mapping->range_end._signed, |
| 347 | overlap_query->range_start._signed, |
| 348 | overlap_query->range_end._signed); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static |
| 353 | void check_ranges_overlap_unsigned(gpointer element, gpointer query) |
| 354 | { |
| 355 | struct enumeration_mapping *mapping = element; |
| 356 | struct range_overlap_query *overlap_query = query; |
| 357 | |
| 358 | if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned |
| 359 | && overlap_query->range_start._unsigned <= |
| 360 | mapping->range_end._unsigned) { |
| 361 | overlap_query->overlaps = 1; |
| 362 | overlap_query->mapping_name = mapping->string; |
| 363 | } |
| 364 | |
| 365 | overlap_query->overlaps |= |
| 366 | mapping->string == overlap_query->mapping_name; |
| 367 | |
| 368 | if (overlap_query->overlaps) { |
| 369 | BT_LOGW("Overlapping enumeration field type mappings: " |
| 370 | "mapping-name=\"%s\", " |
| 371 | "mapping-a-range-start=%" PRIu64 ", " |
| 372 | "mapping-a-range-end=%" PRIu64 ", " |
| 373 | "mapping-b-range-start=%" PRIu64 ", " |
| 374 | "mapping-b-range-end=%" PRIu64, |
| 375 | g_quark_to_string(mapping->string), |
| 376 | mapping->range_start._unsigned, |
| 377 | mapping->range_end._unsigned, |
| 378 | overlap_query->range_start._unsigned, |
| 379 | overlap_query->range_end._unsigned); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static |
| 384 | gint compare_enumeration_mappings_signed(struct enumeration_mapping **a, |
| 385 | struct enumeration_mapping **b) |
| 386 | { |
| 387 | return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1; |
| 388 | } |
| 389 | |
| 390 | static |
| 391 | gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a, |
| 392 | struct enumeration_mapping **b) |
| 393 | { |
| 394 | return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1; |
| 395 | } |
| 396 | |
| 397 | static |
| 398 | void bt_ctf_field_type_init(struct bt_ctf_field_type *type, bt_bool init_bo) |
| 399 | { |
| 400 | assert(type && (type->id > BT_CTF_FIELD_TYPE_ID_UNKNOWN) && |
| 401 | (type->id < BT_CTF_NR_TYPE_IDS)); |
| 402 | |
| 403 | bt_object_init(type, bt_ctf_field_type_destroy); |
| 404 | type->freeze = type_freeze_funcs[type->id]; |
| 405 | type->serialize = type_serialize_funcs[type->id]; |
| 406 | |
| 407 | if (init_bo) { |
| 408 | int ret; |
| 409 | const enum bt_ctf_byte_order bo = BT_CTF_BYTE_ORDER_NATIVE; |
| 410 | |
| 411 | BT_LOGD("Setting initial field type's byte order: bo=%s", |
| 412 | bt_ctf_byte_order_string(bo)); |
| 413 | ret = bt_ctf_field_type_set_byte_order(type, bo); |
| 414 | assert(ret == 0); |
| 415 | } |
| 416 | |
| 417 | type->alignment = 1; |
| 418 | } |
| 419 | |
| 420 | static |
| 421 | int add_structure_field(GPtrArray *fields, |
| 422 | GHashTable *field_name_to_index, |
| 423 | struct bt_ctf_field_type *field_type, |
| 424 | const char *field_name) |
| 425 | { |
| 426 | int ret = 0; |
| 427 | GQuark name_quark = g_quark_from_string(field_name); |
| 428 | struct structure_field *field; |
| 429 | |
| 430 | /* Make sure structure does not contain a field of the same name */ |
| 431 | if (g_hash_table_lookup_extended(field_name_to_index, |
| 432 | GUINT_TO_POINTER(name_quark), NULL, NULL)) { |
| 433 | BT_LOGW("Structure or variant field type already contains a field type with this name: " |
| 434 | "field-name=\"%s\"", field_name); |
| 435 | ret = -1; |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | field = g_new0(struct structure_field, 1); |
| 440 | if (!field) { |
| 441 | BT_LOGE_STR("Failed to allocate one structure/variant field type field."); |
| 442 | ret = -1; |
| 443 | goto end; |
| 444 | } |
| 445 | |
| 446 | bt_get(field_type); |
| 447 | field->name = name_quark; |
| 448 | field->type = field_type; |
| 449 | g_hash_table_insert(field_name_to_index, |
| 450 | GUINT_TO_POINTER(name_quark), |
| 451 | GUINT_TO_POINTER(fields->len)); |
| 452 | g_ptr_array_add(fields, field); |
| 453 | BT_LOGV("Added structure/variant field type field: field-ft-addr=%p, " |
| 454 | "field-name=\"%s\"", field_type, field_name); |
| 455 | end: |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | static |
| 460 | void bt_ctf_field_type_destroy(struct bt_object *obj) |
| 461 | { |
| 462 | struct bt_ctf_field_type *type; |
| 463 | enum bt_ctf_field_type_id type_id; |
| 464 | |
| 465 | type = container_of(obj, struct bt_ctf_field_type, base); |
| 466 | type_id = type->id; |
| 467 | assert(type_id > BT_CTF_FIELD_TYPE_ID_UNKNOWN && |
| 468 | type_id < BT_CTF_NR_TYPE_IDS); |
| 469 | type_destroy_funcs[type_id](type); |
| 470 | } |
| 471 | |
| 472 | static |
| 473 | int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *type) |
| 474 | { |
| 475 | int ret = 0; |
| 476 | |
| 477 | struct bt_ctf_field_type_integer *integer = |
| 478 | container_of(type, struct bt_ctf_field_type_integer, |
| 479 | parent); |
| 480 | |
| 481 | if (integer->mapped_clock && integer->is_signed) { |
| 482 | BT_LOGW("Invalid integer field type: cannot be signed and have a mapped clock class: " |
| 483 | "ft-addr=%p, clock-class-addr=%p, clock-class-name=\"%s\"", |
| 484 | type, integer->mapped_clock, |
| 485 | bt_ctf_clock_class_get_name(integer->mapped_clock)); |
| 486 | ret = -1; |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | end: |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | static |
| 495 | struct enumeration_mapping *get_enumeration_mapping( |
| 496 | struct bt_ctf_field_type *type, uint64_t index) |
| 497 | { |
| 498 | struct enumeration_mapping *mapping = NULL; |
| 499 | struct bt_ctf_field_type_enumeration *enumeration; |
| 500 | |
| 501 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 502 | parent); |
| 503 | if (index >= enumeration->entries->len) { |
| 504 | BT_LOGW("Invalid parameter: index is out of bounds: " |
| 505 | "addr=%p, index=%" PRIu64 ", count=%u", |
| 506 | type, index, enumeration->entries->len); |
| 507 | goto end; |
| 508 | } |
| 509 | |
| 510 | mapping = g_ptr_array_index(enumeration->entries, index); |
| 511 | end: |
| 512 | return mapping; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Note: This algorithm is O(n^2) vs number of enumeration mappings. |
| 517 | * Only used when freezing an enumeration. |
| 518 | */ |
| 519 | static |
| 520 | void set_enumeration_range_overlap( |
| 521 | struct bt_ctf_field_type *type) |
| 522 | { |
| 523 | int64_t i, j, len; |
| 524 | struct bt_ctf_field_type *container_type; |
| 525 | struct bt_ctf_field_type_enumeration *enumeration_type; |
| 526 | int is_signed; |
| 527 | |
| 528 | BT_LOGV("Setting enumeration field type's overlap flag: addr=%p", |
| 529 | type); |
| 530 | enumeration_type = container_of(type, |
| 531 | struct bt_ctf_field_type_enumeration, parent); |
| 532 | |
| 533 | len = enumeration_type->entries->len; |
| 534 | container_type = enumeration_type->container; |
| 535 | is_signed = bt_ctf_field_type_integer_get_signed(container_type); |
| 536 | |
| 537 | for (i = 0; i < len; i++) { |
| 538 | for (j = i + 1; j < len; j++) { |
| 539 | struct enumeration_mapping *mapping[2]; |
| 540 | |
| 541 | mapping[0] = get_enumeration_mapping(type, i); |
| 542 | mapping[1] = get_enumeration_mapping(type, j); |
| 543 | if (is_signed) { |
| 544 | if (mapping[0]->range_start._signed |
| 545 | <= mapping[1]->range_end._signed |
| 546 | && mapping[0]->range_end._signed |
| 547 | >= mapping[1]->range_start._signed) { |
| 548 | enumeration_type->has_overlapping_ranges = BT_TRUE; |
| 549 | goto end; |
| 550 | } |
| 551 | } else { |
| 552 | if (mapping[0]->range_start._unsigned |
| 553 | <= mapping[1]->range_end._unsigned |
| 554 | && mapping[0]->range_end._unsigned |
| 555 | >= mapping[1]->range_start._unsigned) { |
| 556 | enumeration_type->has_overlapping_ranges = BT_TRUE; |
| 557 | goto end; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | end: |
| 564 | if (enumeration_type->has_overlapping_ranges) { |
| 565 | BT_LOGV_STR("Enumeration field type has overlapping ranges."); |
| 566 | } else { |
| 567 | BT_LOGV_STR("Enumeration field type has no overlapping ranges."); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static |
| 572 | int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type) |
| 573 | { |
| 574 | int ret = 0; |
| 575 | |
| 576 | struct bt_ctf_field_type_enumeration *enumeration = |
| 577 | container_of(type, struct bt_ctf_field_type_enumeration, |
| 578 | parent); |
| 579 | struct bt_ctf_field_type *container_type = |
| 580 | bt_ctf_field_type_enumeration_get_container_type(type); |
| 581 | |
| 582 | assert(container_type); |
| 583 | ret = bt_ctf_field_type_validate(container_type); |
| 584 | if (ret) { |
| 585 | BT_LOGW("Invalid enumeration field type: container type is invalid: " |
| 586 | "enum-ft-addr=%p, int-ft-addr=%p", |
| 587 | type, container_type); |
| 588 | goto end; |
| 589 | } |
| 590 | |
| 591 | /* Ensure enum has entries */ |
| 592 | if (enumeration->entries->len == 0) { |
| 593 | BT_LOGW("Invalid enumeration field type: no entries: " |
| 594 | "addr=%p", type); |
| 595 | ret = -1; |
| 596 | goto end; |
| 597 | } |
| 598 | |
| 599 | end: |
| 600 | BT_PUT(container_type); |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static |
| 605 | int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *type) |
| 606 | { |
| 607 | int ret = 0; |
| 608 | struct bt_ctf_field_type *element_type = NULL; |
| 609 | struct bt_ctf_field_type_sequence *sequence = |
| 610 | container_of(type, struct bt_ctf_field_type_sequence, |
| 611 | parent); |
| 612 | |
| 613 | /* Length field name should be set at this point */ |
| 614 | if (sequence->length_field_name->len == 0) { |
| 615 | BT_LOGW("Invalid sequence field type: no length field name: " |
| 616 | "addr=%p", type); |
| 617 | ret = -1; |
| 618 | goto end; |
| 619 | } |
| 620 | |
| 621 | element_type = bt_ctf_field_type_sequence_get_element_type(type); |
| 622 | assert(element_type); |
| 623 | ret = bt_ctf_field_type_validate(element_type); |
| 624 | if (ret) { |
| 625 | BT_LOGW("Invalid sequence field type: invalid element field type: " |
| 626 | "seq-ft-addr=%p, element-ft-add=%p", |
| 627 | type, element_type); |
| 628 | } |
| 629 | |
| 630 | end: |
| 631 | BT_PUT(element_type); |
| 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | static |
| 637 | int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *type) |
| 638 | { |
| 639 | int ret = 0; |
| 640 | struct bt_ctf_field_type *element_type = NULL; |
| 641 | |
| 642 | element_type = bt_ctf_field_type_array_get_element_type(type); |
| 643 | assert(element_type); |
| 644 | ret = bt_ctf_field_type_validate(element_type); |
| 645 | if (ret) { |
| 646 | BT_LOGW("Invalid array field type: invalid element field type: " |
| 647 | "array-ft-addr=%p, element-ft-add=%p", |
| 648 | type, element_type); |
| 649 | } |
| 650 | |
| 651 | BT_PUT(element_type); |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | static |
| 656 | int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *type) |
| 657 | { |
| 658 | int ret = 0; |
| 659 | struct bt_ctf_field_type *child_type = NULL; |
| 660 | int64_t field_count = bt_ctf_field_type_structure_get_field_count(type); |
| 661 | int64_t i; |
| 662 | |
| 663 | assert(field_count >= 0); |
| 664 | |
| 665 | for (i = 0; i < field_count; ++i) { |
| 666 | const char *field_name; |
| 667 | |
| 668 | ret = bt_ctf_field_type_structure_get_field_by_index(type, |
| 669 | &field_name, &child_type, i); |
| 670 | assert(ret == 0); |
| 671 | ret = bt_ctf_field_type_validate(child_type); |
| 672 | if (ret) { |
| 673 | BT_LOGW("Invalid structure field type: " |
| 674 | "a contained field type is invalid: " |
| 675 | "struct-ft-addr=%p, field-ft-addr=%p, " |
| 676 | "field-name=\"%s\", field-index=%" PRId64, |
| 677 | type, child_type, field_name, i); |
| 678 | goto end; |
| 679 | } |
| 680 | |
| 681 | BT_PUT(child_type); |
| 682 | } |
| 683 | |
| 684 | end: |
| 685 | BT_PUT(child_type); |
| 686 | |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | static |
| 691 | bt_bool bt_ctf_field_type_enumeration_has_overlapping_ranges( |
| 692 | struct bt_ctf_field_type_enumeration *enumeration_type) |
| 693 | { |
| 694 | if (!enumeration_type->parent.frozen) { |
| 695 | set_enumeration_range_overlap(&enumeration_type->parent); |
| 696 | } |
| 697 | return enumeration_type->has_overlapping_ranges; |
| 698 | } |
| 699 | |
| 700 | static |
| 701 | int bt_ctf_field_type_enumeration_get_mapping_name( |
| 702 | struct bt_ctf_field_type *enum_field_type, |
| 703 | uint64_t index, |
| 704 | const char **mapping_name) |
| 705 | { |
| 706 | int ret = 0; |
| 707 | struct enumeration_mapping *mapping; |
| 708 | |
| 709 | assert(enum_field_type); |
| 710 | mapping = get_enumeration_mapping(enum_field_type, index); |
| 711 | assert(mapping); |
| 712 | if (mapping_name) { |
| 713 | *mapping_name = g_quark_to_string(mapping->string); |
| 714 | } |
| 715 | |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | static |
| 720 | int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type) |
| 721 | { |
| 722 | int ret = 0; |
| 723 | int64_t field_count; |
| 724 | struct bt_ctf_field_type *child_type = NULL; |
| 725 | struct bt_ctf_field_type_variant *variant = |
| 726 | container_of(type, struct bt_ctf_field_type_variant, |
| 727 | parent); |
| 728 | int64_t i; |
| 729 | int64_t tag_mappings_count; |
| 730 | |
| 731 | if (variant->tag_name->len == 0) { |
| 732 | BT_LOGW("Invalid variant field type: no tag field name: " |
| 733 | "addr=%p", type); |
| 734 | ret = -1; |
| 735 | goto end; |
| 736 | } |
| 737 | |
| 738 | if (!variant->tag) { |
| 739 | BT_LOGW("Invalid variant field type: no tag field type: " |
| 740 | "addr=%p, tag-field-name=\"%s\"", type, |
| 741 | variant->tag_name->str); |
| 742 | ret = -1; |
| 743 | goto end; |
| 744 | } |
| 745 | |
| 746 | if (bt_ctf_field_type_enumeration_has_overlapping_ranges( |
| 747 | variant->tag)) { |
| 748 | BT_LOGW("Invalid variant field type: enumeration tag field type has overlapping ranges: " |
| 749 | "variant-ft-addr=%p, tag-field-name=\"%s\", " |
| 750 | "enum-ft-addr=%p", type, variant->tag_name->str, |
| 751 | variant->tag); |
| 752 | ret = -1; |
| 753 | goto end; |
| 754 | } |
| 755 | |
| 756 | tag_mappings_count = |
| 757 | bt_ctf_field_type_enumeration_get_mapping_count( |
| 758 | (struct bt_ctf_field_type *) variant->tag); |
| 759 | assert(tag_mappings_count >= 0); |
| 760 | |
| 761 | /* |
| 762 | * Validate that each mapping found in the tag has a name which |
| 763 | * is also the name of a field in this variant field type. |
| 764 | * |
| 765 | * The opposite is accepted: variant FT fields which cannot be |
| 766 | * selected because the variant FT tag has no mapping named as |
| 767 | * such. This scenario, while not ideal, cannot cause any error. |
| 768 | */ |
| 769 | for (i = 0; i < tag_mappings_count; ++i) { |
| 770 | const char *label; |
| 771 | struct bt_ctf_field_type *ft; |
| 772 | |
| 773 | ret = bt_ctf_field_type_enumeration_get_mapping_name( |
| 774 | (struct bt_ctf_field_type *) variant->tag, |
| 775 | i, &label); |
| 776 | assert(ret == 0); |
| 777 | assert(label); |
| 778 | ft = bt_ctf_field_type_variant_get_field_type_by_name( |
| 779 | type, label); |
| 780 | if (!ft) { |
| 781 | BT_LOGW("Invalid variant field type: " |
| 782 | "enumeration tag field type contains a mapping which does not name a variant field type field: " |
| 783 | "variant-ft-addr=%p, tag-field-name=\"%s\", " |
| 784 | "enum-ft-addr=%p, mapping-name=\"%s\"", |
| 785 | type, variant->tag_name->str, variant->tag, |
| 786 | label); |
| 787 | ret = -1; |
| 788 | goto end; |
| 789 | } |
| 790 | |
| 791 | BT_PUT(ft); |
| 792 | } |
| 793 | |
| 794 | field_count = bt_ctf_field_type_variant_get_field_count(type); |
| 795 | if (field_count < 0) { |
| 796 | BT_LOGW("Invalid variant field type: no fields: " |
| 797 | "addr=%p, tag-field-name=\"%s\"", |
| 798 | type, variant->tag_name->str); |
| 799 | ret = -1; |
| 800 | goto end; |
| 801 | } |
| 802 | |
| 803 | for (i = 0; i < field_count; ++i) { |
| 804 | const char *field_name; |
| 805 | |
| 806 | ret = bt_ctf_field_type_variant_get_field_by_index(type, |
| 807 | &field_name, &child_type, i); |
| 808 | assert(ret == 0); |
| 809 | ret = bt_ctf_field_type_validate(child_type); |
| 810 | if (ret) { |
| 811 | BT_LOGW("Invalid variant field type: " |
| 812 | "a contained field type is invalid: " |
| 813 | "variant-ft-addr=%p, tag-field-name=\"%s\", " |
| 814 | "field-ft-addr=%p, field-name=\"%s\", " |
| 815 | "field-index=%" PRId64, |
| 816 | type, variant->tag_name->str, child_type, |
| 817 | field_name, i); |
| 818 | goto end; |
| 819 | } |
| 820 | |
| 821 | BT_PUT(child_type); |
| 822 | } |
| 823 | |
| 824 | end: |
| 825 | BT_PUT(child_type); |
| 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * This function validates a given field type without considering |
| 832 | * where this field type is located. It only validates the properties |
| 833 | * of the given field type and the properties of its children if |
| 834 | * applicable. |
| 835 | */ |
| 836 | BT_HIDDEN |
| 837 | int bt_ctf_field_type_validate(struct bt_ctf_field_type *type) |
| 838 | { |
| 839 | int ret = 0; |
| 840 | enum bt_ctf_field_type_id id = bt_ctf_field_type_get_type_id(type); |
| 841 | |
| 842 | assert(type); |
| 843 | |
| 844 | if (type->valid) { |
| 845 | /* Already marked as valid */ |
| 846 | goto end; |
| 847 | } |
| 848 | |
| 849 | if (type_validate_funcs[id]) { |
| 850 | ret = type_validate_funcs[id](type); |
| 851 | } |
| 852 | |
| 853 | if (!ret && type->frozen) { |
| 854 | /* Field type is valid */ |
| 855 | BT_LOGV("Marking field type as valid: addr=%p", type); |
| 856 | type->valid = 1; |
| 857 | } |
| 858 | |
| 859 | end: |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size) |
| 864 | { |
| 865 | struct bt_ctf_field_type_integer *integer = |
| 866 | g_new0(struct bt_ctf_field_type_integer, 1); |
| 867 | |
| 868 | BT_LOGD("Creating integer field type object: size=%u", size); |
| 869 | |
| 870 | if (!integer) { |
| 871 | BT_LOGE_STR("Failed to allocate one integer field type."); |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | if (size == 0 || size > 64) { |
| 876 | BT_LOGW("Invalid parameter: size must be between 1 and 64: " |
| 877 | "size=%u", size); |
| 878 | return NULL; |
| 879 | } |
| 880 | |
| 881 | integer->parent.id = BT_CTF_FIELD_TYPE_ID_INTEGER; |
| 882 | integer->size = size; |
| 883 | integer->base = BT_CTF_INTEGER_BASE_DECIMAL; |
| 884 | integer->encoding = BT_CTF_STRING_ENCODING_NONE; |
| 885 | bt_ctf_field_type_init(&integer->parent, TRUE); |
| 886 | BT_LOGD("Created integer field type object: addr=%p, size=%u", |
| 887 | &integer->parent, size); |
| 888 | return &integer->parent; |
| 889 | } |
| 890 | |
| 891 | int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type) |
| 892 | { |
| 893 | int ret = 0; |
| 894 | struct bt_ctf_field_type_integer *integer; |
| 895 | |
| 896 | if (!type) { |
| 897 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 898 | ret = -1; |
| 899 | goto end; |
| 900 | } |
| 901 | |
| 902 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 903 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 904 | "addr=%p, ft-id=%s", type, |
| 905 | bt_ctf_field_type_id_string(type->id)); |
| 906 | ret = -1; |
| 907 | goto end; |
| 908 | } |
| 909 | |
| 910 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 911 | ret = (int) integer->size; |
| 912 | end: |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type) |
| 917 | { |
| 918 | int ret = 0; |
| 919 | struct bt_ctf_field_type_integer *integer; |
| 920 | |
| 921 | if (!type) { |
| 922 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 923 | ret = -1; |
| 924 | goto end; |
| 925 | } |
| 926 | |
| 927 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 928 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 929 | "addr=%p, ft-id=%s", type, |
| 930 | bt_ctf_field_type_id_string(type->id)); |
| 931 | ret = -1; |
| 932 | goto end; |
| 933 | } |
| 934 | |
| 935 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 936 | ret = integer->is_signed; |
| 937 | end: |
| 938 | return ret; |
| 939 | } |
| 940 | |
| 941 | bt_bool bt_ctf_field_type_integer_is_signed( |
| 942 | struct bt_ctf_field_type *int_field_type) |
| 943 | { |
| 944 | return bt_ctf_field_type_integer_get_signed(int_field_type) ? |
| 945 | BT_TRUE : BT_FALSE; |
| 946 | } |
| 947 | |
| 948 | int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type, |
| 949 | bt_bool is_signed) |
| 950 | { |
| 951 | int ret = 0; |
| 952 | struct bt_ctf_field_type_integer *integer; |
| 953 | |
| 954 | if (!type) { |
| 955 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 956 | ret = -1; |
| 957 | goto end; |
| 958 | } |
| 959 | |
| 960 | if (type->frozen) { |
| 961 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 962 | type); |
| 963 | ret = -1; |
| 964 | goto end; |
| 965 | } |
| 966 | |
| 967 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 968 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 969 | "addr=%p, ft-id=%s", type, |
| 970 | bt_ctf_field_type_id_string(type->id)); |
| 971 | ret = -1; |
| 972 | goto end; |
| 973 | } |
| 974 | |
| 975 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 976 | integer->is_signed = !!is_signed; |
| 977 | BT_LOGV("Set integer field type's signedness: addr=%p, is-signed=%d", |
| 978 | type, is_signed); |
| 979 | end: |
| 980 | return ret; |
| 981 | } |
| 982 | |
| 983 | int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type *type, |
| 984 | size_t size) |
| 985 | { |
| 986 | int ret = 0; |
| 987 | struct bt_ctf_field_type_integer *integer; |
| 988 | |
| 989 | if (!type) { |
| 990 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 991 | ret = -1; |
| 992 | goto end; |
| 993 | } |
| 994 | |
| 995 | if (type->frozen) { |
| 996 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 997 | type); |
| 998 | ret = -1; |
| 999 | goto end; |
| 1000 | } |
| 1001 | |
| 1002 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1003 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 1004 | "addr=%p, ft-id=%s", type, |
| 1005 | bt_ctf_field_type_id_string(type->id)); |
| 1006 | ret = -1; |
| 1007 | goto end; |
| 1008 | } |
| 1009 | |
| 1010 | if (size == 0 || size > 64) { |
| 1011 | BT_LOGW("Invalid parameter: size must be between 1 and 64: " |
| 1012 | "addr=%p, size=%zu", type, size); |
| 1013 | ret = -1; |
| 1014 | goto end; |
| 1015 | } |
| 1016 | |
| 1017 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 1018 | integer->size = size; |
| 1019 | BT_LOGV("Set integer field type's size: addr=%p, size=%zu", |
| 1020 | type, size); |
| 1021 | end: |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( |
| 1026 | struct bt_ctf_field_type *type) |
| 1027 | { |
| 1028 | enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN; |
| 1029 | struct bt_ctf_field_type_integer *integer; |
| 1030 | |
| 1031 | if (!type) { |
| 1032 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1033 | goto end; |
| 1034 | } |
| 1035 | |
| 1036 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1037 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 1038 | "addr=%p, ft-id=%s", type, |
| 1039 | bt_ctf_field_type_id_string(type->id)); |
| 1040 | goto end; |
| 1041 | } |
| 1042 | |
| 1043 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 1044 | ret = integer->base; |
| 1045 | end: |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type, |
| 1050 | enum bt_ctf_integer_base base) |
| 1051 | { |
| 1052 | int ret = 0; |
| 1053 | |
| 1054 | if (!type) { |
| 1055 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1056 | ret = -1; |
| 1057 | goto end; |
| 1058 | } |
| 1059 | |
| 1060 | if (type->frozen) { |
| 1061 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 1062 | type); |
| 1063 | ret = -1; |
| 1064 | goto end; |
| 1065 | } |
| 1066 | |
| 1067 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1068 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 1069 | "addr=%p, ft-id=%s", type, |
| 1070 | bt_ctf_field_type_id_string(type->id)); |
| 1071 | ret = -1; |
| 1072 | goto end; |
| 1073 | } |
| 1074 | |
| 1075 | switch (base) { |
| 1076 | case BT_CTF_INTEGER_BASE_BINARY: |
| 1077 | case BT_CTF_INTEGER_BASE_OCTAL: |
| 1078 | case BT_CTF_INTEGER_BASE_DECIMAL: |
| 1079 | case BT_CTF_INTEGER_BASE_HEXADECIMAL: |
| 1080 | { |
| 1081 | struct bt_ctf_field_type_integer *integer = container_of(type, |
| 1082 | struct bt_ctf_field_type_integer, parent); |
| 1083 | integer->base = base; |
| 1084 | break; |
| 1085 | } |
| 1086 | default: |
| 1087 | BT_LOGW("Invalid parameter: unknown integer field type base: " |
| 1088 | "addr=%p, base=%d", type, base); |
| 1089 | ret = -1; |
| 1090 | } |
| 1091 | |
| 1092 | BT_LOGV("Set integer field type's base: addr=%p, base=%s", |
| 1093 | type, bt_ctf_integer_base_string(base)); |
| 1094 | |
| 1095 | end: |
| 1096 | return ret; |
| 1097 | } |
| 1098 | |
| 1099 | enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding( |
| 1100 | struct bt_ctf_field_type *type) |
| 1101 | { |
| 1102 | enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN; |
| 1103 | struct bt_ctf_field_type_integer *integer; |
| 1104 | |
| 1105 | if (!type) { |
| 1106 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1107 | goto end; |
| 1108 | } |
| 1109 | |
| 1110 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1111 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 1112 | "addr=%p, ft-id=%s", type, |
| 1113 | bt_ctf_field_type_id_string(type->id)); |
| 1114 | goto end; |
| 1115 | } |
| 1116 | |
| 1117 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 1118 | ret = integer->encoding; |
| 1119 | end: |
| 1120 | return ret; |
| 1121 | } |
| 1122 | |
| 1123 | int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type, |
| 1124 | enum bt_ctf_string_encoding encoding) |
| 1125 | { |
| 1126 | int ret = 0; |
| 1127 | struct bt_ctf_field_type_integer *integer; |
| 1128 | |
| 1129 | if (!type) { |
| 1130 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1131 | ret = -1; |
| 1132 | goto end; |
| 1133 | } |
| 1134 | |
| 1135 | if (type->frozen) { |
| 1136 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 1137 | type); |
| 1138 | ret = -1; |
| 1139 | goto end; |
| 1140 | } |
| 1141 | |
| 1142 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1143 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 1144 | "addr=%p, ft-id=%s", type, |
| 1145 | bt_ctf_field_type_id_string(type->id)); |
| 1146 | ret = -1; |
| 1147 | goto end; |
| 1148 | } |
| 1149 | |
| 1150 | if (encoding < BT_CTF_STRING_ENCODING_NONE || |
| 1151 | encoding >= BT_CTF_STRING_ENCODING_UNKNOWN) { |
| 1152 | BT_LOGW("Invalid parameter: unknown string encoding: " |
| 1153 | "addr=%p, encoding=%d", type, encoding); |
| 1154 | ret = -1; |
| 1155 | goto end; |
| 1156 | } |
| 1157 | |
| 1158 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 1159 | integer->encoding = encoding; |
| 1160 | BT_LOGV("Set integer field type's encoding: addr=%p, encoding=%s", |
| 1161 | type, bt_ctf_string_encoding_string(encoding)); |
| 1162 | end: |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | struct bt_ctf_clock_class *bt_ctf_field_type_integer_get_mapped_clock_class( |
| 1167 | struct bt_ctf_field_type *type) |
| 1168 | { |
| 1169 | struct bt_ctf_field_type_integer *integer; |
| 1170 | struct bt_ctf_clock_class *clock_class = NULL; |
| 1171 | |
| 1172 | if (!type) { |
| 1173 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1174 | goto end; |
| 1175 | } |
| 1176 | |
| 1177 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 1178 | clock_class = integer->mapped_clock; |
| 1179 | bt_get(clock_class); |
| 1180 | end: |
| 1181 | return clock_class; |
| 1182 | } |
| 1183 | |
| 1184 | BT_HIDDEN |
| 1185 | int bt_ctf_field_type_integer_set_mapped_clock_class_no_check( |
| 1186 | struct bt_ctf_field_type *type, |
| 1187 | struct bt_ctf_clock_class *clock_class) |
| 1188 | { |
| 1189 | struct bt_ctf_field_type_integer *integer; |
| 1190 | int ret = 0; |
| 1191 | |
| 1192 | if (!type) { |
| 1193 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1194 | ret = -1; |
| 1195 | goto end; |
| 1196 | } |
| 1197 | |
| 1198 | if (!clock_class) { |
| 1199 | BT_LOGW_STR("Invalid parameter: clock class is NULL."); |
| 1200 | ret = -1; |
| 1201 | goto end; |
| 1202 | } |
| 1203 | |
| 1204 | if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1205 | BT_LOGW("Invalid parameter: field type is not an integer field type: " |
| 1206 | "addr=%p, ft-id=%s", type, |
| 1207 | bt_ctf_field_type_id_string(type->id)); |
| 1208 | goto end; |
| 1209 | } |
| 1210 | |
| 1211 | if (!bt_ctf_clock_class_is_valid(clock_class)) { |
| 1212 | BT_LOGW("Invalid parameter: clock class is invalid: ft-addr=%p" |
| 1213 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 1214 | type, clock_class, |
| 1215 | bt_ctf_clock_class_get_name(clock_class)); |
| 1216 | ret = -1; |
| 1217 | goto end; |
| 1218 | } |
| 1219 | |
| 1220 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 1221 | bt_put(integer->mapped_clock); |
| 1222 | integer->mapped_clock = bt_get(clock_class); |
| 1223 | BT_LOGV("Set integer field type's mapped clock class: ft-addr=%p, " |
| 1224 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 1225 | type, clock_class, bt_ctf_clock_class_get_name(clock_class)); |
| 1226 | end: |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | int bt_ctf_field_type_integer_set_mapped_clock_class( |
| 1231 | struct bt_ctf_field_type *type, |
| 1232 | struct bt_ctf_clock_class *clock_class) |
| 1233 | { |
| 1234 | int ret = 0; |
| 1235 | |
| 1236 | if (!type) { |
| 1237 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1238 | ret = -1; |
| 1239 | goto end; |
| 1240 | } |
| 1241 | |
| 1242 | if (type->frozen) { |
| 1243 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 1244 | type); |
| 1245 | ret = -1; |
| 1246 | goto end; |
| 1247 | } |
| 1248 | |
| 1249 | ret = bt_ctf_field_type_integer_set_mapped_clock_class_no_check( |
| 1250 | type, clock_class); |
| 1251 | |
| 1252 | end: |
| 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | static |
| 1257 | void bt_ctf_field_type_enum_iter_destroy(struct bt_object *obj) |
| 1258 | { |
| 1259 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter = |
| 1260 | container_of(obj, |
| 1261 | struct bt_ctf_field_type_enumeration_mapping_iterator, |
| 1262 | base); |
| 1263 | |
| 1264 | BT_LOGD("Destroying enumeration field type mapping iterator: addr=%p", |
| 1265 | obj); |
| 1266 | BT_LOGD_STR("Putting parent enumeration field type."); |
| 1267 | bt_put(&iter->enumeration_type->parent); |
| 1268 | g_free(iter); |
| 1269 | } |
| 1270 | |
| 1271 | static |
| 1272 | struct bt_ctf_field_type_enumeration_mapping_iterator * |
| 1273 | bt_ctf_field_type_enumeration_find_mappings_type( |
| 1274 | struct bt_ctf_field_type *type, |
| 1275 | enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type) |
| 1276 | { |
| 1277 | struct bt_ctf_field_type_enumeration *enumeration_type; |
| 1278 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL; |
| 1279 | |
| 1280 | if (!type) { |
| 1281 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1282 | goto end; |
| 1283 | } |
| 1284 | |
| 1285 | if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 1286 | BT_LOGW("Invalid parameter: field type is not an enumeration field type: " |
| 1287 | "addr=%p, ft-id=%s", type, |
| 1288 | bt_ctf_field_type_id_string(type->id)); |
| 1289 | goto end; |
| 1290 | } |
| 1291 | |
| 1292 | enumeration_type = container_of(type, |
| 1293 | struct bt_ctf_field_type_enumeration, parent); |
| 1294 | iter = g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator, 1); |
| 1295 | if (!iter) { |
| 1296 | BT_LOGE_STR("Failed to allocate one enumeration field type mapping."); |
| 1297 | goto end; |
| 1298 | } |
| 1299 | |
| 1300 | bt_object_init(&iter->base, bt_ctf_field_type_enum_iter_destroy); |
| 1301 | bt_get(type); |
| 1302 | iter->enumeration_type = enumeration_type; |
| 1303 | iter->index = -1; |
| 1304 | iter->type = iterator_type; |
| 1305 | end: |
| 1306 | return iter; |
| 1307 | } |
| 1308 | |
| 1309 | struct bt_ctf_field_type_enumeration_mapping_iterator * |
| 1310 | bt_ctf_field_type_enumeration_find_mappings_by_name( |
| 1311 | struct bt_ctf_field_type *type, const char *name) |
| 1312 | { |
| 1313 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter; |
| 1314 | |
| 1315 | iter = bt_ctf_field_type_enumeration_find_mappings_type( |
| 1316 | type, ITERATOR_BY_NAME); |
| 1317 | if (!iter) { |
| 1318 | BT_LOGE("Cannot create enumeration field type mapping iterator: " |
| 1319 | "ft-addr=%p, mapping-name=\"%s\"", type, name); |
| 1320 | goto error; |
| 1321 | } |
| 1322 | |
| 1323 | iter->u.name_quark = g_quark_try_string(name); |
| 1324 | if (!iter->u.name_quark) { |
| 1325 | BT_LOGV("No such enumeration field type mapping name: " |
| 1326 | "ft-addr=%p, mapping-name=\"%s\"", |
| 1327 | type, name); |
| 1328 | goto error; |
| 1329 | } |
| 1330 | |
| 1331 | /* Advance iterator to first entry, or leave index at -1. */ |
| 1332 | if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) { |
| 1333 | /* No entry found. */ |
| 1334 | BT_LOGV("No such enumeration field type mapping name: " |
| 1335 | "ft-addr=%p, mapping-name=\"%s\"", |
| 1336 | type, name); |
| 1337 | goto error; |
| 1338 | } |
| 1339 | |
| 1340 | return iter; |
| 1341 | error: |
| 1342 | bt_put(iter); |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | |
| 1346 | int bt_ctf_field_type_enumeration_mapping_iterator_next( |
| 1347 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter) |
| 1348 | { |
| 1349 | struct bt_ctf_field_type_enumeration *enumeration; |
| 1350 | struct bt_ctf_field_type *type; |
| 1351 | int i, ret = 0, len; |
| 1352 | |
| 1353 | if (!iter) { |
| 1354 | BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL."); |
| 1355 | ret = -1; |
| 1356 | goto end; |
| 1357 | } |
| 1358 | |
| 1359 | enumeration = iter->enumeration_type; |
| 1360 | type = &enumeration->parent; |
| 1361 | len = enumeration->entries->len; |
| 1362 | for (i = iter->index + 1; i < len; i++) { |
| 1363 | struct enumeration_mapping *mapping = |
| 1364 | get_enumeration_mapping(type, i); |
| 1365 | |
| 1366 | switch (iter->type) { |
| 1367 | case ITERATOR_BY_NAME: |
| 1368 | if (mapping->string == iter->u.name_quark) { |
| 1369 | iter->index = i; |
| 1370 | goto end; |
| 1371 | } |
| 1372 | break; |
| 1373 | case ITERATOR_BY_SIGNED_VALUE: |
| 1374 | { |
| 1375 | int64_t value = iter->u.signed_value; |
| 1376 | |
| 1377 | if (value >= mapping->range_start._signed && |
| 1378 | value <= mapping->range_end._signed) { |
| 1379 | iter->index = i; |
| 1380 | goto end; |
| 1381 | } |
| 1382 | break; |
| 1383 | } |
| 1384 | case ITERATOR_BY_UNSIGNED_VALUE: |
| 1385 | { |
| 1386 | uint64_t value = iter->u.unsigned_value; |
| 1387 | |
| 1388 | if (value >= mapping->range_start._unsigned && |
| 1389 | value <= mapping->range_end._unsigned) { |
| 1390 | iter->index = i; |
| 1391 | goto end; |
| 1392 | } |
| 1393 | break; |
| 1394 | } |
| 1395 | default: |
| 1396 | BT_LOGF("Invalid enumeration field type mapping iterator type: " |
| 1397 | "type=%d", iter->type); |
| 1398 | abort(); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | ret = -1; |
| 1403 | end: |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | struct bt_ctf_field_type_enumeration_mapping_iterator * |
| 1408 | bt_ctf_field_type_enumeration_find_mappings_by_signed_value( |
| 1409 | struct bt_ctf_field_type *type, int64_t value) |
| 1410 | { |
| 1411 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter; |
| 1412 | |
| 1413 | iter = bt_ctf_field_type_enumeration_find_mappings_type( |
| 1414 | type, ITERATOR_BY_SIGNED_VALUE); |
| 1415 | if (!iter) { |
| 1416 | BT_LOGE("Cannot create enumeration field type mapping iterator: " |
| 1417 | "ft-addr=%p, value=%" PRId64, type, value); |
| 1418 | goto error; |
| 1419 | } |
| 1420 | |
| 1421 | if (bt_ctf_field_type_integer_get_signed( |
| 1422 | iter->enumeration_type->container) != 1) { |
| 1423 | BT_LOGW("Invalid parameter: enumeration field type is unsigned: " |
| 1424 | "enum-ft-addr=%p, int-ft-addr=%p", |
| 1425 | type, iter->enumeration_type->container); |
| 1426 | goto error; |
| 1427 | } |
| 1428 | |
| 1429 | iter->u.signed_value = value; |
| 1430 | |
| 1431 | /* Advance iterator to first entry, or leave index at -1. */ |
| 1432 | if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) { |
| 1433 | /* No entry found. */ |
| 1434 | goto error; |
| 1435 | } |
| 1436 | |
| 1437 | return iter; |
| 1438 | error: |
| 1439 | bt_put(iter); |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | |
| 1443 | struct bt_ctf_field_type_enumeration_mapping_iterator * |
| 1444 | bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value( |
| 1445 | struct bt_ctf_field_type *type, uint64_t value) |
| 1446 | { |
| 1447 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL; |
| 1448 | |
| 1449 | if (!type) { |
| 1450 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1451 | goto error; |
| 1452 | } |
| 1453 | |
| 1454 | iter = bt_ctf_field_type_enumeration_find_mappings_type( |
| 1455 | type, ITERATOR_BY_UNSIGNED_VALUE); |
| 1456 | if (!iter) { |
| 1457 | BT_LOGE("Cannot create enumeration field type mapping iterator: " |
| 1458 | "ft-addr=%p, value=%" PRIu64, type, value); |
| 1459 | goto error; |
| 1460 | } |
| 1461 | |
| 1462 | if (bt_ctf_field_type_integer_get_signed( |
| 1463 | iter->enumeration_type->container) != 0) { |
| 1464 | BT_LOGW("Invalid parameter: enumeration field type is signed: " |
| 1465 | "enum-ft-addr=%p, int-ft-addr=%p", |
| 1466 | type, iter->enumeration_type->container); |
| 1467 | goto error; |
| 1468 | } |
| 1469 | iter->u.unsigned_value = value; |
| 1470 | |
| 1471 | /* Advance iterator to first entry, or leave index at -1. */ |
| 1472 | if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) { |
| 1473 | /* No entry found. */ |
| 1474 | goto error; |
| 1475 | } |
| 1476 | |
| 1477 | return iter; |
| 1478 | error: |
| 1479 | bt_put(iter); |
| 1480 | return NULL; |
| 1481 | } |
| 1482 | |
| 1483 | int bt_ctf_field_type_enumeration_mapping_iterator_get_signed( |
| 1484 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter, |
| 1485 | const char **mapping_name, int64_t *range_begin, |
| 1486 | int64_t *range_end) |
| 1487 | { |
| 1488 | int ret = 0; |
| 1489 | |
| 1490 | if (!iter) { |
| 1491 | BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL."); |
| 1492 | ret = -1; |
| 1493 | goto end; |
| 1494 | } |
| 1495 | |
| 1496 | ret = bt_ctf_field_type_enumeration_get_mapping_signed( |
| 1497 | &iter->enumeration_type->parent, iter->index, |
| 1498 | mapping_name, range_begin, range_end); |
| 1499 | end: |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned( |
| 1504 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter, |
| 1505 | const char **mapping_name, uint64_t *range_begin, |
| 1506 | uint64_t *range_end) |
| 1507 | { |
| 1508 | int ret = 0; |
| 1509 | |
| 1510 | if (!iter) { |
| 1511 | BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL."); |
| 1512 | ret = -1; |
| 1513 | goto end; |
| 1514 | } |
| 1515 | |
| 1516 | ret = bt_ctf_field_type_enumeration_get_mapping_unsigned( |
| 1517 | &iter->enumeration_type->parent, iter->index, |
| 1518 | mapping_name, range_begin, range_end); |
| 1519 | end: |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
| 1523 | int bt_ctf_field_type_enumeration_get_mapping_signed( |
| 1524 | struct bt_ctf_field_type *enum_field_type, |
| 1525 | uint64_t index, const char **mapping_name, int64_t *range_begin, |
| 1526 | int64_t *range_end) |
| 1527 | { |
| 1528 | int ret = 0; |
| 1529 | struct enumeration_mapping *mapping; |
| 1530 | |
| 1531 | if (!enum_field_type) { |
| 1532 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1533 | ret = -1; |
| 1534 | goto end; |
| 1535 | } |
| 1536 | |
| 1537 | mapping = get_enumeration_mapping(enum_field_type, index); |
| 1538 | if (!mapping) { |
| 1539 | /* get_enumeration_mapping() reports any error */ |
| 1540 | ret = -1; |
| 1541 | goto end; |
| 1542 | } |
| 1543 | |
| 1544 | if (mapping_name) { |
| 1545 | *mapping_name = g_quark_to_string(mapping->string); |
| 1546 | assert(*mapping_name); |
| 1547 | } |
| 1548 | |
| 1549 | if (range_begin) { |
| 1550 | *range_begin = mapping->range_start._signed; |
| 1551 | } |
| 1552 | |
| 1553 | if (range_end) { |
| 1554 | *range_end = mapping->range_end._signed; |
| 1555 | } |
| 1556 | end: |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | int bt_ctf_field_type_enumeration_get_mapping_unsigned( |
| 1561 | struct bt_ctf_field_type *enum_field_type, |
| 1562 | uint64_t index, |
| 1563 | const char **mapping_name, uint64_t *range_begin, |
| 1564 | uint64_t *range_end) |
| 1565 | { |
| 1566 | int ret = 0; |
| 1567 | struct enumeration_mapping *mapping; |
| 1568 | |
| 1569 | if (!enum_field_type) { |
| 1570 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1571 | ret = -1; |
| 1572 | goto end; |
| 1573 | } |
| 1574 | |
| 1575 | mapping = get_enumeration_mapping(enum_field_type, index); |
| 1576 | if (!mapping) { |
| 1577 | /* get_enumeration_mapping() reports any error */ |
| 1578 | ret = -1; |
| 1579 | goto end; |
| 1580 | } |
| 1581 | |
| 1582 | if (mapping_name) { |
| 1583 | *mapping_name = g_quark_to_string(mapping->string); |
| 1584 | assert(*mapping_name); |
| 1585 | } |
| 1586 | |
| 1587 | if (range_begin) { |
| 1588 | *range_begin = mapping->range_start._unsigned; |
| 1589 | } |
| 1590 | |
| 1591 | if (range_end) { |
| 1592 | *range_end = mapping->range_end._unsigned; |
| 1593 | } |
| 1594 | end: |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( |
| 1599 | struct bt_ctf_field_type *integer_container_type) |
| 1600 | { |
| 1601 | struct bt_ctf_field_type_enumeration *enumeration = NULL; |
| 1602 | |
| 1603 | BT_LOGD("Creating enumeration field type object: int-ft-addr=%p", |
| 1604 | integer_container_type); |
| 1605 | |
| 1606 | if (!integer_container_type) { |
| 1607 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1608 | goto error; |
| 1609 | } |
| 1610 | |
| 1611 | if (integer_container_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
| 1612 | BT_LOGW("Invalid parameter: container field type is not an integer field type: " |
| 1613 | "container-ft-addr=%p, container-ft-id=%s", |
| 1614 | integer_container_type, |
| 1615 | bt_ctf_field_type_id_string(integer_container_type->id)); |
| 1616 | goto error; |
| 1617 | } |
| 1618 | |
| 1619 | enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1); |
| 1620 | if (!enumeration) { |
| 1621 | BT_LOGE_STR("Failed to allocate one enumeration field type."); |
| 1622 | goto error; |
| 1623 | } |
| 1624 | |
| 1625 | enumeration->parent.id = BT_CTF_FIELD_TYPE_ID_ENUM; |
| 1626 | bt_get(integer_container_type); |
| 1627 | enumeration->container = integer_container_type; |
| 1628 | enumeration->entries = g_ptr_array_new_with_free_func( |
| 1629 | (GDestroyNotify)destroy_enumeration_mapping); |
| 1630 | bt_ctf_field_type_init(&enumeration->parent, FALSE); |
| 1631 | BT_LOGD("Created enumeration field type object: addr=%p, " |
| 1632 | "int-ft-addr=%p, int-ft-size=%u", |
| 1633 | &enumeration->parent, integer_container_type, |
| 1634 | bt_ctf_field_type_integer_get_size(integer_container_type)); |
| 1635 | return &enumeration->parent; |
| 1636 | error: |
| 1637 | g_free(enumeration); |
| 1638 | return NULL; |
| 1639 | } |
| 1640 | |
| 1641 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( |
| 1642 | struct bt_ctf_field_type *type) |
| 1643 | { |
| 1644 | struct bt_ctf_field_type *container_type = NULL; |
| 1645 | struct bt_ctf_field_type_enumeration *enumeration_type; |
| 1646 | |
| 1647 | if (!type) { |
| 1648 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1649 | goto end; |
| 1650 | } |
| 1651 | |
| 1652 | if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 1653 | BT_LOGW("Invalid parameter: field type is not an enumeration field type: " |
| 1654 | "addr=%p, ft-id=%s", type, |
| 1655 | bt_ctf_field_type_id_string(type->id)); |
| 1656 | goto end; |
| 1657 | } |
| 1658 | |
| 1659 | enumeration_type = container_of(type, |
| 1660 | struct bt_ctf_field_type_enumeration, parent); |
| 1661 | container_type = enumeration_type->container; |
| 1662 | bt_get(container_type); |
| 1663 | end: |
| 1664 | return container_type; |
| 1665 | } |
| 1666 | |
| 1667 | int bt_ctf_field_type_enumeration_add_mapping_signed( |
| 1668 | struct bt_ctf_field_type *type, const char *string, |
| 1669 | int64_t range_start, int64_t range_end) |
| 1670 | { |
| 1671 | int ret = 0; |
| 1672 | GQuark mapping_name; |
| 1673 | struct enumeration_mapping *mapping; |
| 1674 | struct bt_ctf_field_type_enumeration *enumeration; |
| 1675 | char *escaped_string; |
| 1676 | |
| 1677 | if (!type) { |
| 1678 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1679 | ret = -1; |
| 1680 | goto end; |
| 1681 | } |
| 1682 | |
| 1683 | if (!string) { |
| 1684 | BT_LOGW_STR("Invalid parameter: string is NULL."); |
| 1685 | ret = -1; |
| 1686 | goto end; |
| 1687 | } |
| 1688 | |
| 1689 | if (type->frozen) { |
| 1690 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 1691 | type); |
| 1692 | ret = -1; |
| 1693 | goto end; |
| 1694 | } |
| 1695 | |
| 1696 | if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 1697 | BT_LOGW("Invalid parameter: field type is not an enumeration field type: " |
| 1698 | "addr=%p, ft-id=%s", type, |
| 1699 | bt_ctf_field_type_id_string(type->id)); |
| 1700 | ret = -1; |
| 1701 | goto end; |
| 1702 | } |
| 1703 | |
| 1704 | if (range_end < range_start) { |
| 1705 | BT_LOGW("Invalid parameter: range's end is lesser than range's start: " |
| 1706 | "addr=%p, range-start=%" PRId64 ", range-end=%" PRId64, |
| 1707 | type, range_start, range_end); |
| 1708 | ret = -1; |
| 1709 | goto end; |
| 1710 | } |
| 1711 | |
| 1712 | if (strlen(string) == 0) { |
| 1713 | BT_LOGW("Invalid parameter: mapping name is an empty string: " |
| 1714 | "enum-ft-addr=%p, mapping-name-addr=%p", type, |
| 1715 | string); |
| 1716 | ret = -1; |
| 1717 | goto end; |
| 1718 | } |
| 1719 | |
| 1720 | escaped_string = g_strescape(string, NULL); |
| 1721 | if (!escaped_string) { |
| 1722 | BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, " |
| 1723 | "mapping-name-addr=%p, mapping-name=\"%s\"", |
| 1724 | type, string, string); |
| 1725 | ret = -1; |
| 1726 | goto end; |
| 1727 | } |
| 1728 | |
| 1729 | mapping = g_new(struct enumeration_mapping, 1); |
| 1730 | if (!mapping) { |
| 1731 | BT_LOGE_STR("Failed to allocate one enumeration mapping."); |
| 1732 | ret = -1; |
| 1733 | goto error_free; |
| 1734 | } |
| 1735 | mapping_name = g_quark_from_string(escaped_string); |
| 1736 | *mapping = (struct enumeration_mapping) { |
| 1737 | .range_start._signed = range_start, |
| 1738 | .range_end._signed = range_end, |
| 1739 | .string = mapping_name, |
| 1740 | }; |
| 1741 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 1742 | parent); |
| 1743 | g_ptr_array_add(enumeration->entries, mapping); |
| 1744 | g_ptr_array_sort(enumeration->entries, |
| 1745 | (GCompareFunc)compare_enumeration_mappings_signed); |
| 1746 | BT_LOGV("Added mapping to signed enumeration field type: addr=%p, " |
| 1747 | "name=\"%s\", range-start=%" PRId64 ", " |
| 1748 | "range-end=%" PRId64, |
| 1749 | type, string, range_start, range_end); |
| 1750 | error_free: |
| 1751 | free(escaped_string); |
| 1752 | end: |
| 1753 | return ret; |
| 1754 | } |
| 1755 | |
| 1756 | int bt_ctf_field_type_enumeration_add_mapping_unsigned( |
| 1757 | struct bt_ctf_field_type *type, const char *string, |
| 1758 | uint64_t range_start, uint64_t range_end) |
| 1759 | { |
| 1760 | int ret = 0; |
| 1761 | GQuark mapping_name; |
| 1762 | struct enumeration_mapping *mapping; |
| 1763 | struct bt_ctf_field_type_enumeration *enumeration; |
| 1764 | char *escaped_string; |
| 1765 | |
| 1766 | if (!type) { |
| 1767 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1768 | ret = -1; |
| 1769 | goto end; |
| 1770 | } |
| 1771 | |
| 1772 | if (!string) { |
| 1773 | BT_LOGW_STR("Invalid parameter: string is NULL."); |
| 1774 | ret = -1; |
| 1775 | goto end; |
| 1776 | } |
| 1777 | |
| 1778 | if (type->frozen) { |
| 1779 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 1780 | type); |
| 1781 | ret = -1; |
| 1782 | goto end; |
| 1783 | } |
| 1784 | |
| 1785 | if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 1786 | BT_LOGW("Invalid parameter: field type is not an enumeration field type: " |
| 1787 | "addr=%p, ft-id=%s", type, |
| 1788 | bt_ctf_field_type_id_string(type->id)); |
| 1789 | ret = -1; |
| 1790 | goto end; |
| 1791 | } |
| 1792 | |
| 1793 | if (range_end < range_start) { |
| 1794 | BT_LOGW("Invalid parameter: range's end is lesser than range's start: " |
| 1795 | "addr=%p, range-start=%" PRIu64 ", range-end=%" PRIu64, |
| 1796 | type, range_start, range_end); |
| 1797 | ret = -1; |
| 1798 | goto end; |
| 1799 | } |
| 1800 | |
| 1801 | if (strlen(string) == 0) { |
| 1802 | BT_LOGW("Invalid parameter: mapping name is an empty string: " |
| 1803 | "enum-ft-addr=%p, mapping-name-addr=%p", type, |
| 1804 | string); |
| 1805 | ret = -1; |
| 1806 | goto end; |
| 1807 | } |
| 1808 | |
| 1809 | escaped_string = g_strescape(string, NULL); |
| 1810 | if (!escaped_string) { |
| 1811 | BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, " |
| 1812 | "mapping-name-addr=%p, mapping-name=\"%s\"", |
| 1813 | type, string, string); |
| 1814 | ret = -1; |
| 1815 | goto end; |
| 1816 | } |
| 1817 | |
| 1818 | mapping = g_new(struct enumeration_mapping, 1); |
| 1819 | if (!mapping) { |
| 1820 | BT_LOGE_STR("Failed to allocate one enumeration mapping."); |
| 1821 | ret = -1; |
| 1822 | goto error_free; |
| 1823 | } |
| 1824 | mapping_name = g_quark_from_string(escaped_string); |
| 1825 | *mapping = (struct enumeration_mapping) { |
| 1826 | .range_start._unsigned = range_start, |
| 1827 | .range_end._unsigned = range_end, |
| 1828 | .string = mapping_name, |
| 1829 | }; |
| 1830 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 1831 | parent); |
| 1832 | g_ptr_array_add(enumeration->entries, mapping); |
| 1833 | g_ptr_array_sort(enumeration->entries, |
| 1834 | (GCompareFunc)compare_enumeration_mappings_unsigned); |
| 1835 | BT_LOGV("Added mapping to unsigned enumeration field type: addr=%p, " |
| 1836 | "name=\"%s\", range-start=%" PRIu64 ", " |
| 1837 | "range-end=%" PRIu64, |
| 1838 | type, string, range_start, range_end); |
| 1839 | error_free: |
| 1840 | free(escaped_string); |
| 1841 | end: |
| 1842 | return ret; |
| 1843 | } |
| 1844 | |
| 1845 | int64_t bt_ctf_field_type_enumeration_get_mapping_count( |
| 1846 | struct bt_ctf_field_type *type) |
| 1847 | { |
| 1848 | int64_t ret = 0; |
| 1849 | struct bt_ctf_field_type_enumeration *enumeration; |
| 1850 | |
| 1851 | if (!type) { |
| 1852 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1853 | ret = (int64_t) -1; |
| 1854 | goto end; |
| 1855 | } |
| 1856 | |
| 1857 | if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 1858 | BT_LOGW("Invalid parameter: field type is not an enumeration field type: " |
| 1859 | "addr=%p, ft-id=%s", type, |
| 1860 | bt_ctf_field_type_id_string(type->id)); |
| 1861 | ret = (int64_t) -1; |
| 1862 | goto end; |
| 1863 | } |
| 1864 | |
| 1865 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 1866 | parent); |
| 1867 | ret = (int64_t) enumeration->entries->len; |
| 1868 | end: |
| 1869 | return ret; |
| 1870 | } |
| 1871 | |
| 1872 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void) |
| 1873 | { |
| 1874 | struct bt_ctf_field_type_floating_point *floating_point = |
| 1875 | g_new0(struct bt_ctf_field_type_floating_point, 1); |
| 1876 | |
| 1877 | BT_LOGD_STR("Creating floating point number field type object."); |
| 1878 | |
| 1879 | if (!floating_point) { |
| 1880 | BT_LOGE_STR("Failed to allocate one floating point number field type."); |
| 1881 | goto end; |
| 1882 | } |
| 1883 | |
| 1884 | floating_point->parent.id = BT_CTF_FIELD_TYPE_ID_FLOAT; |
| 1885 | floating_point->exp_dig = sizeof(float) * CHAR_BIT - FLT_MANT_DIG; |
| 1886 | floating_point->mant_dig = FLT_MANT_DIG; |
| 1887 | bt_ctf_field_type_init(&floating_point->parent, TRUE); |
| 1888 | BT_LOGD("Created floating point number field type object: addr=%p, " |
| 1889 | "exp-size=%u, mant-size=%u", &floating_point->parent, |
| 1890 | floating_point->exp_dig, floating_point->mant_dig); |
| 1891 | end: |
| 1892 | return floating_point ? &floating_point->parent : NULL; |
| 1893 | } |
| 1894 | |
| 1895 | int bt_ctf_field_type_floating_point_get_exponent_digits( |
| 1896 | struct bt_ctf_field_type *type) |
| 1897 | { |
| 1898 | int ret = 0; |
| 1899 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1900 | |
| 1901 | if (!type) { |
| 1902 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1903 | ret = -1; |
| 1904 | goto end; |
| 1905 | } |
| 1906 | |
| 1907 | if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) { |
| 1908 | BT_LOGW("Invalid parameter: field type is not a floating point number field type: " |
| 1909 | "addr=%p, ft-id=%s", type, |
| 1910 | bt_ctf_field_type_id_string(type->id)); |
| 1911 | ret = -1; |
| 1912 | goto end; |
| 1913 | } |
| 1914 | |
| 1915 | floating_point = container_of(type, |
| 1916 | struct bt_ctf_field_type_floating_point, parent); |
| 1917 | ret = (int) floating_point->exp_dig; |
| 1918 | end: |
| 1919 | return ret; |
| 1920 | } |
| 1921 | |
| 1922 | int bt_ctf_field_type_floating_point_set_exponent_digits( |
| 1923 | struct bt_ctf_field_type *type, |
| 1924 | unsigned int exponent_digits) |
| 1925 | { |
| 1926 | int ret = 0; |
| 1927 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1928 | |
| 1929 | if (!type) { |
| 1930 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1931 | ret = -1; |
| 1932 | goto end; |
| 1933 | } |
| 1934 | |
| 1935 | if (type->frozen) { |
| 1936 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 1937 | type); |
| 1938 | ret = -1; |
| 1939 | goto end; |
| 1940 | } |
| 1941 | |
| 1942 | if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) { |
| 1943 | BT_LOGW("Invalid parameter: field type is not a floating point number field type: " |
| 1944 | "addr=%p, ft-id=%s", type, |
| 1945 | bt_ctf_field_type_id_string(type->id)); |
| 1946 | ret = -1; |
| 1947 | goto end; |
| 1948 | } |
| 1949 | |
| 1950 | floating_point = container_of(type, |
| 1951 | struct bt_ctf_field_type_floating_point, parent); |
| 1952 | if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) && |
| 1953 | (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) && |
| 1954 | (exponent_digits != |
| 1955 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) { |
| 1956 | BT_LOGW("Invalid parameter: invalid exponent size: " |
| 1957 | "addr=%p, exp-size=%u", type, exponent_digits); |
| 1958 | ret = -1; |
| 1959 | goto end; |
| 1960 | } |
| 1961 | |
| 1962 | floating_point->exp_dig = exponent_digits; |
| 1963 | BT_LOGV("Set floating point number field type's exponent size: addr=%p, " |
| 1964 | "exp-size=%u", type, exponent_digits); |
| 1965 | end: |
| 1966 | return ret; |
| 1967 | } |
| 1968 | |
| 1969 | int bt_ctf_field_type_floating_point_get_mantissa_digits( |
| 1970 | struct bt_ctf_field_type *type) |
| 1971 | { |
| 1972 | int ret = 0; |
| 1973 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1974 | |
| 1975 | if (!type) { |
| 1976 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 1977 | ret = -1; |
| 1978 | goto end; |
| 1979 | } |
| 1980 | |
| 1981 | if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) { |
| 1982 | BT_LOGW("Invalid parameter: field type is not a floating point number field type: " |
| 1983 | "addr=%p, ft-id=%s", type, |
| 1984 | bt_ctf_field_type_id_string(type->id)); |
| 1985 | ret = -1; |
| 1986 | goto end; |
| 1987 | } |
| 1988 | |
| 1989 | floating_point = container_of(type, |
| 1990 | struct bt_ctf_field_type_floating_point, parent); |
| 1991 | ret = (int) floating_point->mant_dig; |
| 1992 | end: |
| 1993 | return ret; |
| 1994 | } |
| 1995 | |
| 1996 | int bt_ctf_field_type_floating_point_set_mantissa_digits( |
| 1997 | struct bt_ctf_field_type *type, |
| 1998 | unsigned int mantissa_digits) |
| 1999 | { |
| 2000 | int ret = 0; |
| 2001 | struct bt_ctf_field_type_floating_point *floating_point; |
| 2002 | |
| 2003 | if (!type) { |
| 2004 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2005 | ret = -1; |
| 2006 | goto end; |
| 2007 | } |
| 2008 | |
| 2009 | if (type->frozen) { |
| 2010 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 2011 | type); |
| 2012 | ret = -1; |
| 2013 | goto end; |
| 2014 | } |
| 2015 | |
| 2016 | if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) { |
| 2017 | BT_LOGW("Invalid parameter: field type is not a floating point number field type: " |
| 2018 | "addr=%p, ft-id=%s", type, |
| 2019 | bt_ctf_field_type_id_string(type->id)); |
| 2020 | ret = -1; |
| 2021 | goto end; |
| 2022 | } |
| 2023 | |
| 2024 | floating_point = container_of(type, |
| 2025 | struct bt_ctf_field_type_floating_point, parent); |
| 2026 | |
| 2027 | if ((mantissa_digits != FLT_MANT_DIG) && |
| 2028 | (mantissa_digits != DBL_MANT_DIG) && |
| 2029 | (mantissa_digits != LDBL_MANT_DIG)) { |
| 2030 | BT_LOGW("Invalid parameter: invalid mantissa size: " |
| 2031 | "addr=%p, mant-size=%u", type, mantissa_digits); |
| 2032 | ret = -1; |
| 2033 | goto end; |
| 2034 | } |
| 2035 | |
| 2036 | floating_point->mant_dig = mantissa_digits; |
| 2037 | BT_LOGV("Set floating point number field type's mantissa size: addr=%p, " |
| 2038 | "mant-size=%u", type, mantissa_digits); |
| 2039 | end: |
| 2040 | return ret; |
| 2041 | } |
| 2042 | |
| 2043 | struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void) |
| 2044 | { |
| 2045 | struct bt_ctf_field_type_structure *structure = |
| 2046 | g_new0(struct bt_ctf_field_type_structure, 1); |
| 2047 | |
| 2048 | BT_LOGD_STR("Creating structure field type object."); |
| 2049 | |
| 2050 | if (!structure) { |
| 2051 | BT_LOGE_STR("Failed to allocate one structure field type."); |
| 2052 | goto error; |
| 2053 | } |
| 2054 | |
| 2055 | structure->parent.id = BT_CTF_FIELD_TYPE_ID_STRUCT; |
| 2056 | structure->fields = g_ptr_array_new_with_free_func( |
| 2057 | (GDestroyNotify)destroy_structure_field); |
| 2058 | structure->field_name_to_index = g_hash_table_new(NULL, NULL); |
| 2059 | bt_ctf_field_type_init(&structure->parent, TRUE); |
| 2060 | BT_LOGD("Created structure field type object: addr=%p", |
| 2061 | &structure->parent); |
| 2062 | return &structure->parent; |
| 2063 | error: |
| 2064 | return NULL; |
| 2065 | } |
| 2066 | |
| 2067 | int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type, |
| 2068 | struct bt_ctf_field_type *field_type, |
| 2069 | const char *field_name) |
| 2070 | { |
| 2071 | int ret = 0; |
| 2072 | struct bt_ctf_field_type_structure *structure; |
| 2073 | |
| 2074 | /* |
| 2075 | * TODO: check that `field_type` does not contain `type`, |
| 2076 | * recursively. |
| 2077 | */ |
| 2078 | if (!type) { |
| 2079 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2080 | ret = -1; |
| 2081 | goto end; |
| 2082 | } |
| 2083 | |
| 2084 | if (!field_name) { |
| 2085 | BT_LOGW_STR("Invalid parameter: field name is NULL."); |
| 2086 | ret = -1; |
| 2087 | goto end; |
| 2088 | } |
| 2089 | |
| 2090 | if (type->frozen) { |
| 2091 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 2092 | type); |
| 2093 | ret = -1; |
| 2094 | goto end; |
| 2095 | } |
| 2096 | |
| 2097 | if (bt_ctf_validate_identifier(field_name)) { |
| 2098 | BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: " |
| 2099 | "struct-ft-addr=%p, field-name=\"%s\"", |
| 2100 | type, field_name); |
| 2101 | ret = -1; |
| 2102 | goto end; |
| 2103 | } |
| 2104 | |
| 2105 | if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 2106 | BT_LOGW("Invalid parameter: field type is not a structure field type: " |
| 2107 | "addr=%p, ft-id=%s", type, |
| 2108 | bt_ctf_field_type_id_string(type->id)); |
| 2109 | ret = -1; |
| 2110 | goto end; |
| 2111 | } |
| 2112 | |
| 2113 | if (type == field_type) { |
| 2114 | BT_LOGW("Invalid parameter: structure field type and field type to add are the same: " |
| 2115 | "addr=%p", type); |
| 2116 | ret = -1; |
| 2117 | goto end; |
| 2118 | } |
| 2119 | |
| 2120 | structure = container_of(type, |
| 2121 | struct bt_ctf_field_type_structure, parent); |
| 2122 | if (add_structure_field(structure->fields, |
| 2123 | structure->field_name_to_index, field_type, field_name)) { |
| 2124 | BT_LOGW("Cannot add field to structure field type: " |
| 2125 | "struct-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"", |
| 2126 | type, field_type, field_name); |
| 2127 | ret = -1; |
| 2128 | goto end; |
| 2129 | } |
| 2130 | |
| 2131 | BT_LOGV("Added structure field type field: struct-ft-addr=%p, " |
| 2132 | "field-ft-addr=%p, field-name=\"%s\"", type, |
| 2133 | field_type, field_name); |
| 2134 | end: |
| 2135 | return ret; |
| 2136 | } |
| 2137 | |
| 2138 | int64_t bt_ctf_field_type_structure_get_field_count( |
| 2139 | struct bt_ctf_field_type *type) |
| 2140 | { |
| 2141 | int64_t ret = 0; |
| 2142 | struct bt_ctf_field_type_structure *structure; |
| 2143 | |
| 2144 | if (!type) { |
| 2145 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2146 | ret = (int64_t) -1; |
| 2147 | goto end; |
| 2148 | } |
| 2149 | |
| 2150 | if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 2151 | BT_LOGW("Invalid parameter: field type is not a structure field type: " |
| 2152 | "addr=%p, ft-id=%s", type, |
| 2153 | bt_ctf_field_type_id_string(type->id)); |
| 2154 | ret = (int64_t) -1; |
| 2155 | goto end; |
| 2156 | } |
| 2157 | |
| 2158 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 2159 | parent); |
| 2160 | ret = (int64_t) structure->fields->len; |
| 2161 | end: |
| 2162 | return ret; |
| 2163 | } |
| 2164 | |
| 2165 | int bt_ctf_field_type_structure_get_field_by_index( |
| 2166 | struct bt_ctf_field_type *type, |
| 2167 | const char **field_name, struct bt_ctf_field_type **field_type, |
| 2168 | uint64_t index) |
| 2169 | { |
| 2170 | struct bt_ctf_field_type_structure *structure; |
| 2171 | struct structure_field *field; |
| 2172 | int ret = 0; |
| 2173 | |
| 2174 | if (!type) { |
| 2175 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2176 | ret = -1; |
| 2177 | goto end; |
| 2178 | } |
| 2179 | |
| 2180 | if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 2181 | BT_LOGW("Invalid parameter: field type is not a structure field type: " |
| 2182 | "addr=%p, ft-id=%s", type, |
| 2183 | bt_ctf_field_type_id_string(type->id)); |
| 2184 | ret = -1; |
| 2185 | goto end; |
| 2186 | } |
| 2187 | |
| 2188 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 2189 | parent); |
| 2190 | if (index >= structure->fields->len) { |
| 2191 | BT_LOGW("Invalid parameter: index is out of bounds: " |
| 2192 | "addr=%p, index=%" PRIu64 ", count=%u", |
| 2193 | type, index, structure->fields->len); |
| 2194 | ret = -1; |
| 2195 | goto end; |
| 2196 | } |
| 2197 | |
| 2198 | field = g_ptr_array_index(structure->fields, index); |
| 2199 | if (field_type) { |
| 2200 | *field_type = field->type; |
| 2201 | bt_get(field->type); |
| 2202 | } |
| 2203 | if (field_name) { |
| 2204 | *field_name = g_quark_to_string(field->name); |
| 2205 | assert(*field_name); |
| 2206 | } |
| 2207 | end: |
| 2208 | return ret; |
| 2209 | } |
| 2210 | |
| 2211 | struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( |
| 2212 | struct bt_ctf_field_type *type, |
| 2213 | const char *name) |
| 2214 | { |
| 2215 | size_t index; |
| 2216 | GQuark name_quark; |
| 2217 | struct structure_field *field; |
| 2218 | struct bt_ctf_field_type_structure *structure; |
| 2219 | struct bt_ctf_field_type *field_type = NULL; |
| 2220 | |
| 2221 | if (!type) { |
| 2222 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2223 | goto end; |
| 2224 | } |
| 2225 | |
| 2226 | if (!name) { |
| 2227 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 2228 | goto end; |
| 2229 | } |
| 2230 | |
| 2231 | name_quark = g_quark_try_string(name); |
| 2232 | if (!name_quark) { |
| 2233 | BT_LOGV("No such structure field type field name: " |
| 2234 | "ft-addr=%p, field-name=\"%s\"", |
| 2235 | type, name); |
| 2236 | goto end; |
| 2237 | } |
| 2238 | |
| 2239 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 2240 | parent); |
| 2241 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 2242 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { |
| 2243 | BT_LOGV("No such structure field type field name: " |
| 2244 | "ft-addr=%p, field-name=\"%s\"", |
| 2245 | type, name); |
| 2246 | goto end; |
| 2247 | } |
| 2248 | |
| 2249 | field = structure->fields->pdata[index]; |
| 2250 | field_type = field->type; |
| 2251 | bt_get(field_type); |
| 2252 | end: |
| 2253 | return field_type; |
| 2254 | } |
| 2255 | |
| 2256 | struct bt_ctf_field_type *bt_ctf_field_type_variant_create( |
| 2257 | struct bt_ctf_field_type *enum_tag, const char *tag_name) |
| 2258 | { |
| 2259 | struct bt_ctf_field_type_variant *variant = NULL; |
| 2260 | |
| 2261 | BT_LOGD("Creating variant field type object: " |
| 2262 | "tag-ft-addr=%p, tag-field-name=\"%s\"", |
| 2263 | enum_tag, tag_name); |
| 2264 | |
| 2265 | if (tag_name && bt_ctf_validate_identifier(tag_name)) { |
| 2266 | BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: " |
| 2267 | "tag-ft-addr=%p, tag-field-name=\"%s\"", |
| 2268 | enum_tag, tag_name); |
| 2269 | goto error; |
| 2270 | } |
| 2271 | |
| 2272 | variant = g_new0(struct bt_ctf_field_type_variant, 1); |
| 2273 | if (!variant) { |
| 2274 | BT_LOGE_STR("Failed to allocate one variant field type."); |
| 2275 | goto error; |
| 2276 | } |
| 2277 | |
| 2278 | variant->parent.id = BT_CTF_FIELD_TYPE_ID_VARIANT; |
| 2279 | variant->tag_name = g_string_new(tag_name); |
| 2280 | variant->field_name_to_index = g_hash_table_new(NULL, NULL); |
| 2281 | variant->fields = g_ptr_array_new_with_free_func( |
| 2282 | (GDestroyNotify) destroy_structure_field); |
| 2283 | if (enum_tag) { |
| 2284 | bt_get(enum_tag); |
| 2285 | variant->tag = container_of(enum_tag, |
| 2286 | struct bt_ctf_field_type_enumeration, parent); |
| 2287 | } |
| 2288 | |
| 2289 | bt_ctf_field_type_init(&variant->parent, TRUE); |
| 2290 | /* A variant's alignment is undefined */ |
| 2291 | variant->parent.alignment = 0; |
| 2292 | BT_LOGD("Created variant field type object: addr=%p, " |
| 2293 | "tag-ft-addr=%p, tag-field-name=\"%s\"", |
| 2294 | &variant->parent, enum_tag, tag_name); |
| 2295 | return &variant->parent; |
| 2296 | error: |
| 2297 | return NULL; |
| 2298 | } |
| 2299 | |
| 2300 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( |
| 2301 | struct bt_ctf_field_type *type) |
| 2302 | { |
| 2303 | struct bt_ctf_field_type_variant *variant; |
| 2304 | struct bt_ctf_field_type *tag_type = NULL; |
| 2305 | |
| 2306 | if (!type) { |
| 2307 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2308 | goto end; |
| 2309 | } |
| 2310 | |
| 2311 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2312 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2313 | "addr=%p, ft-id=%s", type, |
| 2314 | bt_ctf_field_type_id_string(type->id)); |
| 2315 | goto end; |
| 2316 | } |
| 2317 | |
| 2318 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 2319 | if (!variant->tag) { |
| 2320 | BT_LOGV("Variant field type has no tag field type: " |
| 2321 | "addr=%p", type); |
| 2322 | goto end; |
| 2323 | } |
| 2324 | |
| 2325 | tag_type = &variant->tag->parent; |
| 2326 | bt_get(tag_type); |
| 2327 | end: |
| 2328 | return tag_type; |
| 2329 | } |
| 2330 | |
| 2331 | const char *bt_ctf_field_type_variant_get_tag_name( |
| 2332 | struct bt_ctf_field_type *type) |
| 2333 | { |
| 2334 | struct bt_ctf_field_type_variant *variant; |
| 2335 | const char *tag_name = NULL; |
| 2336 | |
| 2337 | if (!type) { |
| 2338 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2339 | goto end; |
| 2340 | } |
| 2341 | |
| 2342 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2343 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2344 | "addr=%p, ft-id=%s", type, |
| 2345 | bt_ctf_field_type_id_string(type->id)); |
| 2346 | goto end; |
| 2347 | } |
| 2348 | |
| 2349 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 2350 | if (variant->tag_name->len == 0) { |
| 2351 | BT_LOGV("Variant field type has no tag field name: " |
| 2352 | "addr=%p", type); |
| 2353 | goto end; |
| 2354 | } |
| 2355 | |
| 2356 | tag_name = variant->tag_name->str; |
| 2357 | end: |
| 2358 | return tag_name; |
| 2359 | } |
| 2360 | |
| 2361 | int bt_ctf_field_type_variant_set_tag_name( |
| 2362 | struct bt_ctf_field_type *type, const char *name) |
| 2363 | { |
| 2364 | int ret = 0; |
| 2365 | struct bt_ctf_field_type_variant *variant; |
| 2366 | |
| 2367 | if (!type) { |
| 2368 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2369 | ret = -1; |
| 2370 | goto end; |
| 2371 | } |
| 2372 | |
| 2373 | if (type->frozen) { |
| 2374 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 2375 | type); |
| 2376 | ret = -1; |
| 2377 | goto end; |
| 2378 | } |
| 2379 | |
| 2380 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2381 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2382 | "addr=%p, ft-id=%s", type, |
| 2383 | bt_ctf_field_type_id_string(type->id)); |
| 2384 | ret = -1; |
| 2385 | goto end; |
| 2386 | } |
| 2387 | |
| 2388 | if (bt_ctf_validate_identifier(name)) { |
| 2389 | BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: " |
| 2390 | "variant-ft-addr=%p, tag-field-name=\"%s\"", |
| 2391 | type, name); |
| 2392 | ret = -1; |
| 2393 | goto end; |
| 2394 | } |
| 2395 | |
| 2396 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 2397 | g_string_assign(variant->tag_name, name); |
| 2398 | BT_LOGV("Set variant field type's tag field name: addr=%p, " |
| 2399 | "tag-field-name=\"%s\"", type, name); |
| 2400 | end: |
| 2401 | return ret; |
| 2402 | } |
| 2403 | |
| 2404 | int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type, |
| 2405 | struct bt_ctf_field_type *field_type, |
| 2406 | const char *field_name) |
| 2407 | { |
| 2408 | size_t i; |
| 2409 | int ret = 0; |
| 2410 | struct bt_ctf_field_type_variant *variant; |
| 2411 | GQuark field_name_quark = g_quark_from_string(field_name); |
| 2412 | |
| 2413 | /* |
| 2414 | * TODO: check that `field_type` does not contain `type`, |
| 2415 | * recursively. |
| 2416 | */ |
| 2417 | if (!type) { |
| 2418 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2419 | ret = -1; |
| 2420 | goto end; |
| 2421 | } |
| 2422 | |
| 2423 | if (type->frozen) { |
| 2424 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 2425 | type); |
| 2426 | ret = -1; |
| 2427 | goto end; |
| 2428 | } |
| 2429 | |
| 2430 | if (bt_ctf_validate_identifier(field_name)) { |
| 2431 | BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: " |
| 2432 | "variant-ft-addr=%p, field-name=\"%s\"", |
| 2433 | type, field_name); |
| 2434 | ret = -1; |
| 2435 | goto end; |
| 2436 | } |
| 2437 | |
| 2438 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2439 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2440 | "addr=%p, ft-id=%s", type, |
| 2441 | bt_ctf_field_type_id_string(type->id)); |
| 2442 | ret = -1; |
| 2443 | goto end; |
| 2444 | } |
| 2445 | |
| 2446 | if (type == field_type) { |
| 2447 | BT_LOGW("Invalid parameter: variant field type and field type to add are the same: " |
| 2448 | "addr=%p", type); |
| 2449 | ret = -1; |
| 2450 | goto end; |
| 2451 | } |
| 2452 | |
| 2453 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 2454 | |
| 2455 | /* The user has explicitly provided a tag; validate against it. */ |
| 2456 | if (variant->tag) { |
| 2457 | int name_found = 0; |
| 2458 | |
| 2459 | /* Make sure this name is present in the enum tag */ |
| 2460 | for (i = 0; i < variant->tag->entries->len; i++) { |
| 2461 | struct enumeration_mapping *mapping = |
| 2462 | g_ptr_array_index(variant->tag->entries, i); |
| 2463 | |
| 2464 | if (mapping->string == field_name_quark) { |
| 2465 | name_found = 1; |
| 2466 | break; |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | if (!name_found) { |
| 2471 | /* Validation failed */ |
| 2472 | BT_LOGW("Invalid parameter: field name does not name a tag field type's mapping: " |
| 2473 | "variant-ft-addr=%p, tag-ft-addr=%p, " |
| 2474 | "tag-field-name=\"%s\"" |
| 2475 | "field-ft-addr=%p, field-name=\"%s\"", |
| 2476 | type, variant->tag, variant->tag_name->str, |
| 2477 | field_type, field_name); |
| 2478 | ret = -1; |
| 2479 | goto end; |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | if (add_structure_field(variant->fields, variant->field_name_to_index, |
| 2484 | field_type, field_name)) { |
| 2485 | BT_LOGW("Cannot add field to variant field type: " |
| 2486 | "variant-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"", |
| 2487 | type, field_type, field_name); |
| 2488 | ret = -1; |
| 2489 | goto end; |
| 2490 | } |
| 2491 | |
| 2492 | BT_LOGV("Added variant field type field: variant-ft-addr=%p, " |
| 2493 | "field-ft-addr=%p, field-name=\"%s\"", type, |
| 2494 | field_type, field_name); |
| 2495 | |
| 2496 | end: |
| 2497 | return ret; |
| 2498 | } |
| 2499 | |
| 2500 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( |
| 2501 | struct bt_ctf_field_type *type, |
| 2502 | const char *field_name) |
| 2503 | { |
| 2504 | size_t index; |
| 2505 | GQuark name_quark; |
| 2506 | struct structure_field *field; |
| 2507 | struct bt_ctf_field_type_variant *variant; |
| 2508 | struct bt_ctf_field_type *field_type = NULL; |
| 2509 | |
| 2510 | if (!type) { |
| 2511 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2512 | goto end; |
| 2513 | } |
| 2514 | |
| 2515 | if (!field_name) { |
| 2516 | BT_LOGW_STR("Invalid parameter: field name is NULL."); |
| 2517 | goto end; |
| 2518 | } |
| 2519 | |
| 2520 | name_quark = g_quark_try_string(field_name); |
| 2521 | if (!name_quark) { |
| 2522 | BT_LOGV("No such variant field type field name: " |
| 2523 | "ft-addr=%p, field-name=\"%s\"", |
| 2524 | type, field_name); |
| 2525 | goto end; |
| 2526 | } |
| 2527 | |
| 2528 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 2529 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 2530 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { |
| 2531 | BT_LOGV("No such variant field type field name: " |
| 2532 | "ft-addr=%p, field-name=\"%s\"", |
| 2533 | type, field_name); |
| 2534 | goto end; |
| 2535 | } |
| 2536 | |
| 2537 | field = g_ptr_array_index(variant->fields, index); |
| 2538 | field_type = field->type; |
| 2539 | bt_get(field_type); |
| 2540 | end: |
| 2541 | return field_type; |
| 2542 | } |
| 2543 | |
| 2544 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( |
| 2545 | struct bt_ctf_field_type *type, |
| 2546 | struct bt_ctf_field *tag) |
| 2547 | { |
| 2548 | int ret; |
| 2549 | const char *enum_value; |
| 2550 | struct bt_ctf_field_type *field_type = NULL; |
| 2551 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL; |
| 2552 | |
| 2553 | if (!type) { |
| 2554 | BT_LOGW_STR("Invalid parameter: variant field type is NULL."); |
| 2555 | goto end; |
| 2556 | } |
| 2557 | |
| 2558 | if (!tag) { |
| 2559 | BT_LOGW_STR("Invalid parameter: tag field is NULL."); |
| 2560 | goto end; |
| 2561 | } |
| 2562 | |
| 2563 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2564 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2565 | "addr=%p, ft-id=%s", type, |
| 2566 | bt_ctf_field_type_id_string(type->id)); |
| 2567 | goto end; |
| 2568 | } |
| 2569 | |
| 2570 | iter = bt_ctf_field_enumeration_get_mappings(tag); |
| 2571 | if (!iter) { |
| 2572 | BT_LOGE("Cannot get enumeration field type mapping iterator from enumeration field: " |
| 2573 | "enum-field-addr=%p", tag); |
| 2574 | goto end; |
| 2575 | } |
| 2576 | |
| 2577 | ret = bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter, |
| 2578 | &enum_value, NULL, NULL); |
| 2579 | if (ret) { |
| 2580 | BT_LOGW("Cannot get enumeration field type mapping iterator's current mapping: " |
| 2581 | "iter-addr=%p", iter); |
| 2582 | goto end; |
| 2583 | } |
| 2584 | |
| 2585 | field_type = bt_ctf_field_type_variant_get_field_type_by_name( |
| 2586 | type, enum_value); |
| 2587 | end: |
| 2588 | bt_put(iter); |
| 2589 | return field_type; |
| 2590 | } |
| 2591 | |
| 2592 | int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type) |
| 2593 | { |
| 2594 | int64_t ret = 0; |
| 2595 | struct bt_ctf_field_type_variant *variant; |
| 2596 | |
| 2597 | if (!type) { |
| 2598 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2599 | ret = (int64_t) -1; |
| 2600 | goto end; |
| 2601 | } |
| 2602 | |
| 2603 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2604 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2605 | "addr=%p, ft-id=%s", type, |
| 2606 | bt_ctf_field_type_id_string(type->id)); |
| 2607 | ret = (int64_t) -1; |
| 2608 | goto end; |
| 2609 | } |
| 2610 | |
| 2611 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2612 | parent); |
| 2613 | ret = (int64_t) variant->fields->len; |
| 2614 | end: |
| 2615 | return ret; |
| 2616 | |
| 2617 | } |
| 2618 | |
| 2619 | int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type *type, |
| 2620 | const char **field_name, struct bt_ctf_field_type **field_type, |
| 2621 | uint64_t index) |
| 2622 | { |
| 2623 | struct bt_ctf_field_type_variant *variant; |
| 2624 | struct structure_field *field; |
| 2625 | int ret = 0; |
| 2626 | |
| 2627 | if (!type) { |
| 2628 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2629 | ret = -1; |
| 2630 | goto end; |
| 2631 | } |
| 2632 | |
| 2633 | if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 2634 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 2635 | "addr=%p, ft-id=%s", type, |
| 2636 | bt_ctf_field_type_id_string(type->id)); |
| 2637 | ret = -1; |
| 2638 | goto end; |
| 2639 | } |
| 2640 | |
| 2641 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2642 | parent); |
| 2643 | if (index >= variant->fields->len) { |
| 2644 | BT_LOGW("Invalid parameter: index is out of bounds: " |
| 2645 | "addr=%p, index=%" PRIu64 ", count=%u", |
| 2646 | type, index, variant->fields->len); |
| 2647 | ret = -1; |
| 2648 | goto end; |
| 2649 | } |
| 2650 | |
| 2651 | field = g_ptr_array_index(variant->fields, index); |
| 2652 | if (field_type) { |
| 2653 | *field_type = field->type; |
| 2654 | bt_get(field->type); |
| 2655 | } |
| 2656 | if (field_name) { |
| 2657 | *field_name = g_quark_to_string(field->name); |
| 2658 | assert(*field_name); |
| 2659 | } |
| 2660 | end: |
| 2661 | return ret; |
| 2662 | } |
| 2663 | |
| 2664 | struct bt_ctf_field_type *bt_ctf_field_type_array_create( |
| 2665 | struct bt_ctf_field_type *element_type, |
| 2666 | unsigned int length) |
| 2667 | { |
| 2668 | struct bt_ctf_field_type_array *array = NULL; |
| 2669 | |
| 2670 | BT_LOGD("Creating array field type object: element-ft-addr=%p, " |
| 2671 | "length=%u", element_type, length); |
| 2672 | |
| 2673 | if (!element_type) { |
| 2674 | BT_LOGW_STR("Invalid parameter: element field type is NULL."); |
| 2675 | goto error; |
| 2676 | } |
| 2677 | |
| 2678 | if (length == 0) { |
| 2679 | BT_LOGW_STR("Invalid parameter: length is zero."); |
| 2680 | goto error; |
| 2681 | } |
| 2682 | |
| 2683 | array = g_new0(struct bt_ctf_field_type_array, 1); |
| 2684 | if (!array) { |
| 2685 | BT_LOGE_STR("Failed to allocate one array field type."); |
| 2686 | goto error; |
| 2687 | } |
| 2688 | |
| 2689 | array->parent.id = BT_CTF_FIELD_TYPE_ID_ARRAY; |
| 2690 | bt_get(element_type); |
| 2691 | array->element_type = element_type; |
| 2692 | array->length = length; |
| 2693 | bt_ctf_field_type_init(&array->parent, FALSE); |
| 2694 | BT_LOGD("Created array field type object: addr=%p, " |
| 2695 | "element-ft-addr=%p, length=%u", |
| 2696 | &array->parent, element_type, length); |
| 2697 | return &array->parent; |
| 2698 | error: |
| 2699 | return NULL; |
| 2700 | } |
| 2701 | |
| 2702 | struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( |
| 2703 | struct bt_ctf_field_type *type) |
| 2704 | { |
| 2705 | struct bt_ctf_field_type *ret = NULL; |
| 2706 | struct bt_ctf_field_type_array *array; |
| 2707 | |
| 2708 | if (!type) { |
| 2709 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2710 | goto end; |
| 2711 | } |
| 2712 | |
| 2713 | if (type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) { |
| 2714 | BT_LOGW("Invalid parameter: field type is not an array field type: " |
| 2715 | "addr=%p, ft-id=%s", type, |
| 2716 | bt_ctf_field_type_id_string(type->id)); |
| 2717 | goto end; |
| 2718 | } |
| 2719 | |
| 2720 | array = container_of(type, struct bt_ctf_field_type_array, parent); |
| 2721 | ret = array->element_type; |
| 2722 | bt_get(ret); |
| 2723 | end: |
| 2724 | return ret; |
| 2725 | } |
| 2726 | |
| 2727 | BT_HIDDEN |
| 2728 | int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *type, |
| 2729 | struct bt_ctf_field_type *element_type) |
| 2730 | { |
| 2731 | int ret = 0; |
| 2732 | struct bt_ctf_field_type_array *array; |
| 2733 | |
| 2734 | if (!type) { |
| 2735 | BT_LOGW_STR("Invalid parameter: array field type is NULL."); |
| 2736 | ret = -1; |
| 2737 | goto end; |
| 2738 | } |
| 2739 | |
| 2740 | if (!element_type) { |
| 2741 | BT_LOGW_STR("Invalid parameter: element field type is NULL."); |
| 2742 | ret = -1; |
| 2743 | goto end; |
| 2744 | } |
| 2745 | |
| 2746 | if (type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) { |
| 2747 | BT_LOGW("Invalid parameter: field type is not an array field type: " |
| 2748 | "addr=%p, ft-id=%s", type, |
| 2749 | bt_ctf_field_type_id_string(type->id)); |
| 2750 | ret = -1; |
| 2751 | goto end; |
| 2752 | } |
| 2753 | |
| 2754 | array = container_of(type, struct bt_ctf_field_type_array, parent); |
| 2755 | |
| 2756 | if (array->element_type) { |
| 2757 | BT_PUT(array->element_type); |
| 2758 | } |
| 2759 | |
| 2760 | array->element_type = element_type; |
| 2761 | bt_get(array->element_type); |
| 2762 | BT_LOGV("Set array field type's element field type: array-ft-addr=%p, " |
| 2763 | "element-ft-addr=%p", type, element_type); |
| 2764 | |
| 2765 | end: |
| 2766 | return ret; |
| 2767 | } |
| 2768 | |
| 2769 | int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type) |
| 2770 | { |
| 2771 | int64_t ret; |
| 2772 | struct bt_ctf_field_type_array *array; |
| 2773 | |
| 2774 | if (!type) { |
| 2775 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2776 | ret = (int64_t) -1; |
| 2777 | goto end; |
| 2778 | } |
| 2779 | |
| 2780 | if (type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) { |
| 2781 | BT_LOGW("Invalid parameter: field type is not an array field type: " |
| 2782 | "addr=%p, ft-id=%s", type, |
| 2783 | bt_ctf_field_type_id_string(type->id)); |
| 2784 | ret = (int64_t) -1; |
| 2785 | goto end; |
| 2786 | } |
| 2787 | |
| 2788 | array = container_of(type, struct bt_ctf_field_type_array, parent); |
| 2789 | ret = (int64_t) array->length; |
| 2790 | end: |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
| 2794 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( |
| 2795 | struct bt_ctf_field_type *element_type, |
| 2796 | const char *length_field_name) |
| 2797 | { |
| 2798 | struct bt_ctf_field_type_sequence *sequence = NULL; |
| 2799 | |
| 2800 | BT_LOGD("Creating sequence field type object: element-ft-addr=%p, " |
| 2801 | "length-field-name=\"%s\"", element_type, length_field_name); |
| 2802 | |
| 2803 | if (!element_type) { |
| 2804 | BT_LOGW_STR("Invalid parameter: element field type is NULL."); |
| 2805 | goto error; |
| 2806 | } |
| 2807 | |
| 2808 | if (bt_ctf_validate_identifier(length_field_name)) { |
| 2809 | BT_LOGW("Invalid parameter: length field name is not a valid CTF identifier: " |
| 2810 | "length-field-name=\"%s\"", length_field_name); |
| 2811 | goto error; |
| 2812 | } |
| 2813 | |
| 2814 | sequence = g_new0(struct bt_ctf_field_type_sequence, 1); |
| 2815 | if (!sequence) { |
| 2816 | BT_LOGE_STR("Failed to allocate one sequence field type."); |
| 2817 | goto error; |
| 2818 | } |
| 2819 | |
| 2820 | sequence->parent.id = BT_CTF_FIELD_TYPE_ID_SEQUENCE; |
| 2821 | bt_get(element_type); |
| 2822 | sequence->element_type = element_type; |
| 2823 | sequence->length_field_name = g_string_new(length_field_name); |
| 2824 | bt_ctf_field_type_init(&sequence->parent, FALSE); |
| 2825 | BT_LOGD("Created sequence field type object: addr=%p, " |
| 2826 | "element-ft-addr=%p, length-field-name=\"%s\"", |
| 2827 | &sequence->parent, element_type, length_field_name); |
| 2828 | return &sequence->parent; |
| 2829 | error: |
| 2830 | return NULL; |
| 2831 | } |
| 2832 | |
| 2833 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( |
| 2834 | struct bt_ctf_field_type *type) |
| 2835 | { |
| 2836 | struct bt_ctf_field_type *ret = NULL; |
| 2837 | struct bt_ctf_field_type_sequence *sequence; |
| 2838 | |
| 2839 | if (!type) { |
| 2840 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2841 | goto end; |
| 2842 | } |
| 2843 | |
| 2844 | if (type->id != BT_CTF_FIELD_TYPE_ID_SEQUENCE) { |
| 2845 | BT_LOGW("Invalid parameter: field type is not a sequence field type: " |
| 2846 | "addr=%p, ft-id=%s", type, |
| 2847 | bt_ctf_field_type_id_string(type->id)); |
| 2848 | goto end; |
| 2849 | } |
| 2850 | |
| 2851 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 2852 | parent); |
| 2853 | ret = sequence->element_type; |
| 2854 | bt_get(ret); |
| 2855 | end: |
| 2856 | return ret; |
| 2857 | } |
| 2858 | |
| 2859 | BT_HIDDEN |
| 2860 | int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *type, |
| 2861 | struct bt_ctf_field_type *element_type) |
| 2862 | { |
| 2863 | int ret = 0; |
| 2864 | struct bt_ctf_field_type_sequence *sequence; |
| 2865 | |
| 2866 | if (!type) { |
| 2867 | BT_LOGW_STR("Invalid parameter: sequence field type is NULL."); |
| 2868 | ret = -1; |
| 2869 | goto end; |
| 2870 | } |
| 2871 | |
| 2872 | if (!element_type) { |
| 2873 | BT_LOGW_STR("Invalid parameter: element field type is NULL."); |
| 2874 | ret = -1; |
| 2875 | goto end; |
| 2876 | } |
| 2877 | |
| 2878 | if (type->id != BT_CTF_FIELD_TYPE_ID_SEQUENCE) { |
| 2879 | BT_LOGW("Invalid parameter: field type is not a sequence field type: " |
| 2880 | "addr=%p, ft-id=%s", type, |
| 2881 | bt_ctf_field_type_id_string(type->id)); |
| 2882 | ret = -1; |
| 2883 | goto end; |
| 2884 | } |
| 2885 | |
| 2886 | sequence = container_of(type, struct bt_ctf_field_type_sequence, parent); |
| 2887 | if (sequence->element_type) { |
| 2888 | BT_PUT(sequence->element_type); |
| 2889 | } |
| 2890 | |
| 2891 | sequence->element_type = element_type; |
| 2892 | bt_get(sequence->element_type); |
| 2893 | BT_LOGV("Set sequence field type's element field type: sequence-ft-addr=%p, " |
| 2894 | "element-ft-addr=%p", type, element_type); |
| 2895 | |
| 2896 | end: |
| 2897 | return ret; |
| 2898 | } |
| 2899 | |
| 2900 | const char *bt_ctf_field_type_sequence_get_length_field_name( |
| 2901 | struct bt_ctf_field_type *type) |
| 2902 | { |
| 2903 | const char *ret = NULL; |
| 2904 | struct bt_ctf_field_type_sequence *sequence; |
| 2905 | |
| 2906 | if (!type) { |
| 2907 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2908 | goto end; |
| 2909 | } |
| 2910 | |
| 2911 | if (type->id != BT_CTF_FIELD_TYPE_ID_SEQUENCE) { |
| 2912 | BT_LOGW("Invalid parameter: field type is not a sequence field type: " |
| 2913 | "addr=%p, ft-id=%s", type, |
| 2914 | bt_ctf_field_type_id_string(type->id)); |
| 2915 | goto end; |
| 2916 | } |
| 2917 | |
| 2918 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 2919 | parent); |
| 2920 | ret = sequence->length_field_name->str; |
| 2921 | end: |
| 2922 | return ret; |
| 2923 | } |
| 2924 | |
| 2925 | struct bt_ctf_field_type *bt_ctf_field_type_string_create(void) |
| 2926 | { |
| 2927 | struct bt_ctf_field_type_string *string = |
| 2928 | g_new0(struct bt_ctf_field_type_string, 1); |
| 2929 | |
| 2930 | BT_LOGD_STR("Creating string field type object."); |
| 2931 | |
| 2932 | if (!string) { |
| 2933 | BT_LOGE_STR("Failed to allocate one string field type."); |
| 2934 | return NULL; |
| 2935 | } |
| 2936 | |
| 2937 | string->parent.id = BT_CTF_FIELD_TYPE_ID_STRING; |
| 2938 | bt_ctf_field_type_init(&string->parent, TRUE); |
| 2939 | string->encoding = BT_CTF_STRING_ENCODING_UTF8; |
| 2940 | string->parent.alignment = CHAR_BIT; |
| 2941 | BT_LOGD("Created string field type object: addr=%p", &string->parent); |
| 2942 | return &string->parent; |
| 2943 | } |
| 2944 | |
| 2945 | enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding( |
| 2946 | struct bt_ctf_field_type *type) |
| 2947 | { |
| 2948 | struct bt_ctf_field_type_string *string; |
| 2949 | enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN; |
| 2950 | |
| 2951 | if (!type) { |
| 2952 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2953 | goto end; |
| 2954 | } |
| 2955 | |
| 2956 | if (type->id != BT_CTF_FIELD_TYPE_ID_STRING) { |
| 2957 | BT_LOGW("Invalid parameter: field type is not a string field type: " |
| 2958 | "addr=%p, ft-id=%s", type, |
| 2959 | bt_ctf_field_type_id_string(type->id)); |
| 2960 | goto end; |
| 2961 | } |
| 2962 | |
| 2963 | string = container_of(type, struct bt_ctf_field_type_string, |
| 2964 | parent); |
| 2965 | ret = string->encoding; |
| 2966 | end: |
| 2967 | return ret; |
| 2968 | } |
| 2969 | |
| 2970 | int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type, |
| 2971 | enum bt_ctf_string_encoding encoding) |
| 2972 | { |
| 2973 | int ret = 0; |
| 2974 | struct bt_ctf_field_type_string *string; |
| 2975 | |
| 2976 | if (!type) { |
| 2977 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 2978 | ret = -1; |
| 2979 | goto end; |
| 2980 | } |
| 2981 | |
| 2982 | if (type->id != BT_CTF_FIELD_TYPE_ID_STRING) { |
| 2983 | BT_LOGW("Invalid parameter: field type is not a string field type: " |
| 2984 | "addr=%p, ft-id=%s", type, |
| 2985 | bt_ctf_field_type_id_string(type->id)); |
| 2986 | ret = -1; |
| 2987 | goto end; |
| 2988 | } |
| 2989 | |
| 2990 | if ((encoding != BT_CTF_STRING_ENCODING_UTF8 && |
| 2991 | encoding != BT_CTF_STRING_ENCODING_ASCII)) { |
| 2992 | BT_LOGW("Invalid parameter: unknown string encoding: " |
| 2993 | "addr=%p, encoding=%d", type, encoding); |
| 2994 | ret = -1; |
| 2995 | goto end; |
| 2996 | } |
| 2997 | |
| 2998 | string = container_of(type, struct bt_ctf_field_type_string, parent); |
| 2999 | string->encoding = encoding; |
| 3000 | BT_LOGV("Set string field type's encoding: addr=%p, encoding=%s", |
| 3001 | type, bt_ctf_string_encoding_string(encoding)); |
| 3002 | end: |
| 3003 | return ret; |
| 3004 | } |
| 3005 | |
| 3006 | int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type) |
| 3007 | { |
| 3008 | int ret; |
| 3009 | enum bt_ctf_field_type_id type_id; |
| 3010 | |
| 3011 | if (!type) { |
| 3012 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3013 | ret = -1; |
| 3014 | goto end; |
| 3015 | } |
| 3016 | |
| 3017 | if (type->frozen) { |
| 3018 | ret = (int) type->alignment; |
| 3019 | goto end; |
| 3020 | } |
| 3021 | |
| 3022 | type_id = bt_ctf_field_type_get_type_id(type); |
| 3023 | switch (type_id) { |
| 3024 | case BT_CTF_FIELD_TYPE_ID_SEQUENCE: |
| 3025 | { |
| 3026 | struct bt_ctf_field_type *element = |
| 3027 | bt_ctf_field_type_sequence_get_element_type(type); |
| 3028 | |
| 3029 | assert(element); |
| 3030 | ret = bt_ctf_field_type_get_alignment(element); |
| 3031 | bt_put(element); |
| 3032 | break; |
| 3033 | } |
| 3034 | case BT_CTF_FIELD_TYPE_ID_ARRAY: |
| 3035 | { |
| 3036 | struct bt_ctf_field_type *element = |
| 3037 | bt_ctf_field_type_array_get_element_type(type); |
| 3038 | |
| 3039 | assert(element); |
| 3040 | ret = bt_ctf_field_type_get_alignment(element); |
| 3041 | bt_put(element); |
| 3042 | break; |
| 3043 | } |
| 3044 | case BT_CTF_FIELD_TYPE_ID_STRUCT: |
| 3045 | { |
| 3046 | int64_t i, element_count; |
| 3047 | |
| 3048 | element_count = bt_ctf_field_type_structure_get_field_count( |
| 3049 | type); |
| 3050 | assert(element_count >= 0); |
| 3051 | |
| 3052 | for (i = 0; i < element_count; i++) { |
| 3053 | struct bt_ctf_field_type *field; |
| 3054 | int field_alignment; |
| 3055 | |
| 3056 | ret = bt_ctf_field_type_structure_get_field_by_index( |
| 3057 | type, NULL, &field, i); |
| 3058 | assert(ret == 0); |
| 3059 | assert(field); |
| 3060 | field_alignment = bt_ctf_field_type_get_alignment( |
| 3061 | field); |
| 3062 | bt_put(field); |
| 3063 | if (field_alignment < 0) { |
| 3064 | ret = field_alignment; |
| 3065 | goto end; |
| 3066 | } |
| 3067 | |
| 3068 | type->alignment = MAX(field_alignment, type->alignment); |
| 3069 | } |
| 3070 | ret = (int) type->alignment; |
| 3071 | break; |
| 3072 | } |
| 3073 | case BT_CTF_FIELD_TYPE_ID_UNKNOWN: |
| 3074 | BT_LOGW("Invalid parameter: unknown field type ID: " |
| 3075 | "addr=%p, ft-id=%d", type, type_id); |
| 3076 | ret = -1; |
| 3077 | break; |
| 3078 | default: |
| 3079 | ret = (int) type->alignment; |
| 3080 | break; |
| 3081 | } |
| 3082 | end: |
| 3083 | return ret; |
| 3084 | } |
| 3085 | |
| 3086 | static inline |
| 3087 | int is_power_of_two(unsigned int value) |
| 3088 | { |
| 3089 | return ((value & (value - 1)) == 0) && value > 0; |
| 3090 | } |
| 3091 | |
| 3092 | int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, |
| 3093 | unsigned int alignment) |
| 3094 | { |
| 3095 | int ret = 0; |
| 3096 | enum bt_ctf_field_type_id type_id; |
| 3097 | |
| 3098 | /* Alignment must be a power of two */ |
| 3099 | if (!type) { |
| 3100 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3101 | ret = -1; |
| 3102 | goto end; |
| 3103 | } |
| 3104 | |
| 3105 | if (type->frozen) { |
| 3106 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 3107 | type); |
| 3108 | ret = -1; |
| 3109 | goto end; |
| 3110 | } |
| 3111 | |
| 3112 | if (!is_power_of_two(alignment)) { |
| 3113 | BT_LOGW("Invalid parameter: alignment is not a power of two: " |
| 3114 | "addr=%p, align=%u", type, alignment); |
| 3115 | ret = -1; |
| 3116 | goto end; |
| 3117 | } |
| 3118 | |
| 3119 | type_id = bt_ctf_field_type_get_type_id(type); |
| 3120 | if (type_id == BT_CTF_FIELD_TYPE_ID_UNKNOWN) { |
| 3121 | BT_LOGW("Invalid parameter: unknown field type ID: " |
| 3122 | "addr=%p, ft-id=%d", type, type_id); |
| 3123 | ret = -1; |
| 3124 | goto end; |
| 3125 | } |
| 3126 | |
| 3127 | if (type->id == BT_CTF_FIELD_TYPE_ID_STRING && |
| 3128 | alignment != CHAR_BIT) { |
| 3129 | BT_LOGW("Invalid parameter: alignment must be %u for a string field type: " |
| 3130 | "addr=%p, align=%u", CHAR_BIT, type, alignment); |
| 3131 | ret = -1; |
| 3132 | goto end; |
| 3133 | } |
| 3134 | |
| 3135 | if (type_id == BT_CTF_FIELD_TYPE_ID_VARIANT || |
| 3136 | type_id == BT_CTF_FIELD_TYPE_ID_SEQUENCE || |
| 3137 | type_id == BT_CTF_FIELD_TYPE_ID_ARRAY) { |
| 3138 | /* Setting an alignment on these types makes no sense */ |
| 3139 | BT_LOGW("Invalid parameter: cannot set the alignment of this field type: " |
| 3140 | "addr=%p, ft-id=%s", type, |
| 3141 | bt_ctf_field_type_id_string(type->id)); |
| 3142 | ret = -1; |
| 3143 | goto end; |
| 3144 | } |
| 3145 | |
| 3146 | type->alignment = alignment; |
| 3147 | ret = 0; |
| 3148 | BT_LOGV("Set field type's alignment: addr=%p, align=%u", |
| 3149 | type, alignment); |
| 3150 | end: |
| 3151 | return ret; |
| 3152 | } |
| 3153 | |
| 3154 | enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( |
| 3155 | struct bt_ctf_field_type *type) |
| 3156 | { |
| 3157 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; |
| 3158 | |
| 3159 | if (!type) { |
| 3160 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3161 | goto end; |
| 3162 | } |
| 3163 | |
| 3164 | switch (type->id) { |
| 3165 | case BT_CTF_FIELD_TYPE_ID_INTEGER: |
| 3166 | { |
| 3167 | struct bt_ctf_field_type_integer *integer = container_of( |
| 3168 | type, struct bt_ctf_field_type_integer, parent); |
| 3169 | ret = integer->user_byte_order; |
| 3170 | break; |
| 3171 | } |
| 3172 | case BT_CTF_FIELD_TYPE_ID_ENUM: |
| 3173 | { |
| 3174 | struct bt_ctf_field_type_enumeration *enum_ft = container_of( |
| 3175 | type, struct bt_ctf_field_type_enumeration, parent); |
| 3176 | ret = bt_ctf_field_type_get_byte_order(enum_ft->container); |
| 3177 | break; |
| 3178 | } |
| 3179 | case BT_CTF_FIELD_TYPE_ID_FLOAT: |
| 3180 | { |
| 3181 | struct bt_ctf_field_type_floating_point *floating_point = |
| 3182 | container_of(type, |
| 3183 | struct bt_ctf_field_type_floating_point, |
| 3184 | parent); |
| 3185 | ret = floating_point->user_byte_order; |
| 3186 | break; |
| 3187 | } |
| 3188 | default: |
| 3189 | BT_LOGW("Invalid parameter: cannot get the byte order of this field type: " |
| 3190 | "addr=%p, ft-id=%s", type, |
| 3191 | bt_ctf_field_type_id_string(type->id)); |
| 3192 | goto end; |
| 3193 | } |
| 3194 | |
| 3195 | assert(ret == BT_CTF_BYTE_ORDER_NATIVE || |
| 3196 | ret == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || |
| 3197 | ret == BT_CTF_BYTE_ORDER_BIG_ENDIAN || |
| 3198 | ret == BT_CTF_BYTE_ORDER_NETWORK); |
| 3199 | |
| 3200 | end: |
| 3201 | return ret; |
| 3202 | } |
| 3203 | |
| 3204 | int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, |
| 3205 | enum bt_ctf_byte_order byte_order) |
| 3206 | { |
| 3207 | int ret = 0; |
| 3208 | |
| 3209 | if (!type) { |
| 3210 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3211 | ret = -1; |
| 3212 | goto end; |
| 3213 | } |
| 3214 | |
| 3215 | if (type->frozen) { |
| 3216 | BT_LOGW("Invalid parameter: field type is frozen: addr=%p", |
| 3217 | type); |
| 3218 | ret = -1; |
| 3219 | goto end; |
| 3220 | } |
| 3221 | |
| 3222 | if (byte_order != BT_CTF_BYTE_ORDER_NATIVE && |
| 3223 | byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN && |
| 3224 | byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN && |
| 3225 | byte_order != BT_CTF_BYTE_ORDER_NETWORK) { |
| 3226 | BT_LOGW("Invalid parameter: unknown byte order: " |
| 3227 | "addr=%p, bo=%s", type, |
| 3228 | bt_ctf_byte_order_string(byte_order)); |
| 3229 | ret = -1; |
| 3230 | goto end; |
| 3231 | } |
| 3232 | |
| 3233 | if (set_byte_order_funcs[type->id]) { |
| 3234 | set_byte_order_funcs[type->id](type, byte_order); |
| 3235 | } |
| 3236 | |
| 3237 | BT_LOGV("Set field type's byte order: addr=%p, bo=%s", |
| 3238 | type, bt_ctf_byte_order_string(byte_order)); |
| 3239 | |
| 3240 | end: |
| 3241 | return ret; |
| 3242 | } |
| 3243 | |
| 3244 | enum bt_ctf_field_type_id bt_ctf_field_type_get_type_id( |
| 3245 | struct bt_ctf_field_type *type) |
| 3246 | { |
| 3247 | if (!type) { |
| 3248 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3249 | return BT_CTF_FIELD_TYPE_ID_UNKNOWN; |
| 3250 | } |
| 3251 | |
| 3252 | return type->id; |
| 3253 | } |
| 3254 | |
| 3255 | int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type) |
| 3256 | { |
| 3257 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_INTEGER; |
| 3258 | } |
| 3259 | |
| 3260 | int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type) |
| 3261 | { |
| 3262 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_FLOAT; |
| 3263 | } |
| 3264 | |
| 3265 | int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type) |
| 3266 | { |
| 3267 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_ENUM; |
| 3268 | } |
| 3269 | |
| 3270 | int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type) |
| 3271 | { |
| 3272 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_STRING; |
| 3273 | } |
| 3274 | |
| 3275 | int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type) |
| 3276 | { |
| 3277 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_STRUCT; |
| 3278 | } |
| 3279 | |
| 3280 | int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type) |
| 3281 | { |
| 3282 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_ARRAY; |
| 3283 | } |
| 3284 | |
| 3285 | int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type) |
| 3286 | { |
| 3287 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_SEQUENCE; |
| 3288 | } |
| 3289 | |
| 3290 | int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type) |
| 3291 | { |
| 3292 | return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_VARIANT; |
| 3293 | } |
| 3294 | |
| 3295 | /* Pre-2.0 CTF writer backward compatibility */ |
| 3296 | void bt_ctf_field_type_get(struct bt_ctf_field_type *type) |
| 3297 | { |
| 3298 | bt_get(type); |
| 3299 | } |
| 3300 | |
| 3301 | /* Pre-2.0 CTF writer backward compatibility */ |
| 3302 | void bt_ctf_field_type_put(struct bt_ctf_field_type *type) |
| 3303 | { |
| 3304 | bt_put(type); |
| 3305 | } |
| 3306 | |
| 3307 | BT_HIDDEN |
| 3308 | void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type) |
| 3309 | { |
| 3310 | if (!type || type->frozen) { |
| 3311 | return; |
| 3312 | } |
| 3313 | |
| 3314 | type->freeze(type); |
| 3315 | } |
| 3316 | |
| 3317 | BT_HIDDEN |
| 3318 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( |
| 3319 | struct bt_ctf_field_type_variant *variant, |
| 3320 | int64_t tag_value) |
| 3321 | { |
| 3322 | struct bt_ctf_field_type *type = NULL; |
| 3323 | GQuark field_name_quark; |
| 3324 | gpointer index; |
| 3325 | struct structure_field *field_entry; |
| 3326 | struct range_overlap_query query = { |
| 3327 | .range_start._signed = tag_value, |
| 3328 | .range_end._signed = tag_value, |
| 3329 | .mapping_name = 0, |
| 3330 | .overlaps = 0, |
| 3331 | }; |
| 3332 | |
| 3333 | g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap, |
| 3334 | &query); |
| 3335 | if (!query.overlaps) { |
| 3336 | goto end; |
| 3337 | } |
| 3338 | |
| 3339 | field_name_quark = query.mapping_name; |
| 3340 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 3341 | GUINT_TO_POINTER(field_name_quark), NULL, &index)) { |
| 3342 | goto end; |
| 3343 | } |
| 3344 | |
| 3345 | field_entry = g_ptr_array_index(variant->fields, (size_t) index); |
| 3346 | type = field_entry->type; |
| 3347 | end: |
| 3348 | return type; |
| 3349 | } |
| 3350 | |
| 3351 | BT_HIDDEN |
| 3352 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( |
| 3353 | struct bt_ctf_field_type_variant *variant, |
| 3354 | uint64_t tag_value) |
| 3355 | { |
| 3356 | struct bt_ctf_field_type *type = NULL; |
| 3357 | GQuark field_name_quark; |
| 3358 | gpointer index; |
| 3359 | struct structure_field *field_entry; |
| 3360 | struct range_overlap_query query = { |
| 3361 | .range_start._unsigned = tag_value, |
| 3362 | .range_end._unsigned = tag_value, |
| 3363 | .mapping_name = 0, |
| 3364 | .overlaps = 0, |
| 3365 | }; |
| 3366 | |
| 3367 | g_ptr_array_foreach(variant->tag->entries, |
| 3368 | check_ranges_overlap_unsigned, |
| 3369 | &query); |
| 3370 | if (!query.overlaps) { |
| 3371 | goto end; |
| 3372 | } |
| 3373 | |
| 3374 | field_name_quark = query.mapping_name; |
| 3375 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 3376 | GUINT_TO_POINTER(field_name_quark), NULL, &index)) { |
| 3377 | goto end; |
| 3378 | } |
| 3379 | |
| 3380 | field_entry = g_ptr_array_index(variant->fields, (size_t)index); |
| 3381 | type = field_entry->type; |
| 3382 | end: |
| 3383 | return type; |
| 3384 | } |
| 3385 | |
| 3386 | BT_HIDDEN |
| 3387 | int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, |
| 3388 | struct metadata_context *context) |
| 3389 | { |
| 3390 | int ret; |
| 3391 | |
| 3392 | assert(type); |
| 3393 | assert(context); |
| 3394 | |
| 3395 | /* Make sure field type is valid before serializing it */ |
| 3396 | ret = bt_ctf_field_type_validate(type); |
| 3397 | if (ret) { |
| 3398 | BT_LOGW("Cannot serialize field type's metadata: field type is invalid: " |
| 3399 | "addr=%p", type); |
| 3400 | goto end; |
| 3401 | } |
| 3402 | |
| 3403 | ret = type->serialize(type, context); |
| 3404 | end: |
| 3405 | return ret; |
| 3406 | } |
| 3407 | |
| 3408 | struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type) |
| 3409 | { |
| 3410 | struct bt_ctf_field_type *copy = NULL; |
| 3411 | |
| 3412 | if (!type) { |
| 3413 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3414 | goto end; |
| 3415 | } |
| 3416 | |
| 3417 | copy = type_copy_funcs[type->id](type); |
| 3418 | if (!copy) { |
| 3419 | BT_LOGE_STR("Cannot copy field type."); |
| 3420 | goto end; |
| 3421 | } |
| 3422 | |
| 3423 | copy->alignment = type->alignment; |
| 3424 | end: |
| 3425 | return copy; |
| 3426 | } |
| 3427 | |
| 3428 | BT_HIDDEN |
| 3429 | int bt_ctf_field_type_structure_get_field_name_index( |
| 3430 | struct bt_ctf_field_type *type, const char *name) |
| 3431 | { |
| 3432 | int ret; |
| 3433 | size_t index; |
| 3434 | GQuark name_quark; |
| 3435 | struct bt_ctf_field_type_structure *structure; |
| 3436 | |
| 3437 | if (!type) { |
| 3438 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3439 | ret = -1; |
| 3440 | goto end; |
| 3441 | } |
| 3442 | |
| 3443 | if (!name) { |
| 3444 | BT_LOGW_STR("Invalid parameter: field name is NULL."); |
| 3445 | ret = -1; |
| 3446 | goto end; |
| 3447 | } |
| 3448 | |
| 3449 | if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
| 3450 | BT_LOGW("Invalid parameter: field type is not a structure field type: " |
| 3451 | "addr=%p, ft-id=%s", type, |
| 3452 | bt_ctf_field_type_id_string(type->id)); |
| 3453 | ret = -1; |
| 3454 | goto end; |
| 3455 | } |
| 3456 | |
| 3457 | name_quark = g_quark_try_string(name); |
| 3458 | if (!name_quark) { |
| 3459 | BT_LOGV("No such structure field type field name: " |
| 3460 | "ft-addr=%p, field-name=\"%s\"", |
| 3461 | type, name); |
| 3462 | ret = -1; |
| 3463 | goto end; |
| 3464 | } |
| 3465 | |
| 3466 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 3467 | parent); |
| 3468 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 3469 | GUINT_TO_POINTER(name_quark), |
| 3470 | NULL, (gpointer *)&index)) { |
| 3471 | BT_LOGV("No such structure field type field name: " |
| 3472 | "ft-addr=%p, field-name=\"%s\"", |
| 3473 | type, name); |
| 3474 | ret = -1; |
| 3475 | goto end; |
| 3476 | } |
| 3477 | ret = (int) index; |
| 3478 | end: |
| 3479 | return ret; |
| 3480 | } |
| 3481 | |
| 3482 | BT_HIDDEN |
| 3483 | int bt_ctf_field_type_variant_get_field_name_index( |
| 3484 | struct bt_ctf_field_type *type, const char *name) |
| 3485 | { |
| 3486 | int ret; |
| 3487 | size_t index; |
| 3488 | GQuark name_quark; |
| 3489 | struct bt_ctf_field_type_variant *variant; |
| 3490 | |
| 3491 | if (!type) { |
| 3492 | BT_LOGW_STR("Invalid parameter: variant field type is NULL."); |
| 3493 | ret = -1; |
| 3494 | goto end; |
| 3495 | } |
| 3496 | |
| 3497 | if (!name) { |
| 3498 | BT_LOGW_STR("Invalid parameter: field name is NULL."); |
| 3499 | ret = -1; |
| 3500 | goto end; |
| 3501 | } |
| 3502 | |
| 3503 | if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 3504 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 3505 | "addr=%p, ft-id=%s", type, |
| 3506 | bt_ctf_field_type_id_string(type->id)); |
| 3507 | ret = -1; |
| 3508 | goto end; |
| 3509 | } |
| 3510 | |
| 3511 | name_quark = g_quark_try_string(name); |
| 3512 | if (!name_quark) { |
| 3513 | BT_LOGV("No such variant field type field name: " |
| 3514 | "ft-addr=%p, field-name=\"%s\"", |
| 3515 | type, name); |
| 3516 | ret = -1; |
| 3517 | goto end; |
| 3518 | } |
| 3519 | |
| 3520 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 3521 | parent); |
| 3522 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 3523 | GUINT_TO_POINTER(name_quark), |
| 3524 | NULL, (gpointer *)&index)) { |
| 3525 | BT_LOGV("No such variant field type field name: " |
| 3526 | "ft-addr=%p, field-name=\"%s\"", |
| 3527 | type, name); |
| 3528 | ret = -1; |
| 3529 | goto end; |
| 3530 | } |
| 3531 | ret = (int) index; |
| 3532 | end: |
| 3533 | return ret; |
| 3534 | } |
| 3535 | |
| 3536 | BT_HIDDEN |
| 3537 | int bt_ctf_field_type_sequence_set_length_field_path( |
| 3538 | struct bt_ctf_field_type *type, |
| 3539 | struct bt_ctf_field_path *path) |
| 3540 | { |
| 3541 | int ret = 0; |
| 3542 | struct bt_ctf_field_type_sequence *sequence; |
| 3543 | |
| 3544 | if (!type) { |
| 3545 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3546 | ret = -1; |
| 3547 | goto end; |
| 3548 | } |
| 3549 | |
| 3550 | if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_SEQUENCE) { |
| 3551 | BT_LOGW("Invalid parameter: field type is not a sequence field type: " |
| 3552 | "addr=%p, ft-id=%s", type, |
| 3553 | bt_ctf_field_type_id_string(type->id)); |
| 3554 | ret = -1; |
| 3555 | goto end; |
| 3556 | } |
| 3557 | |
| 3558 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 3559 | parent); |
| 3560 | bt_get(path); |
| 3561 | BT_MOVE(sequence->length_field_path, path); |
| 3562 | BT_LOGV("Set sequence field type's length field path: ft-addr=%p, " |
| 3563 | "field-path-addr=%p", type, path); |
| 3564 | end: |
| 3565 | return ret; |
| 3566 | } |
| 3567 | |
| 3568 | BT_HIDDEN |
| 3569 | int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, |
| 3570 | struct bt_ctf_field_path *path) |
| 3571 | { |
| 3572 | int ret = 0; |
| 3573 | struct bt_ctf_field_type_variant *variant; |
| 3574 | |
| 3575 | if (!type) { |
| 3576 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 3577 | ret = -1; |
| 3578 | goto end; |
| 3579 | } |
| 3580 | |
| 3581 | if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_VARIANT) { |
| 3582 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 3583 | "addr=%p, ft-id=%s", type, |
| 3584 | bt_ctf_field_type_id_string(type->id)); |
| 3585 | ret = -1; |
| 3586 | goto end; |
| 3587 | } |
| 3588 | |
| 3589 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 3590 | parent); |
| 3591 | bt_get(path); |
| 3592 | BT_MOVE(variant->tag_field_path, path); |
| 3593 | BT_LOGV("Set variant field type's tag field path: ft-addr=%p, " |
| 3594 | "field-path-addr=%p", type, path); |
| 3595 | end: |
| 3596 | return ret; |
| 3597 | } |
| 3598 | |
| 3599 | BT_HIDDEN |
| 3600 | int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type, |
| 3601 | struct bt_ctf_field_type *tag) |
| 3602 | { |
| 3603 | int ret = 0; |
| 3604 | struct bt_ctf_field_type_variant *variant; |
| 3605 | |
| 3606 | if (!type) { |
| 3607 | BT_LOGW_STR("Invalid parameter: variant field type is NULL."); |
| 3608 | ret = -1; |
| 3609 | goto end; |
| 3610 | } |
| 3611 | |
| 3612 | if (!tag) { |
| 3613 | BT_LOGW_STR("Invalid parameter: tag field type is NULL."); |
| 3614 | ret = -1; |
| 3615 | goto end; |
| 3616 | } |
| 3617 | |
| 3618 | if (bt_ctf_field_type_get_type_id(tag) != BT_CTF_FIELD_TYPE_ID_ENUM) { |
| 3619 | BT_LOGW("Invalid parameter: field type is not an enumeration field type: " |
| 3620 | "addr=%p, ft-id=%s", type, |
| 3621 | bt_ctf_field_type_id_string(type->id)); |
| 3622 | ret = -1; |
| 3623 | goto end; |
| 3624 | } |
| 3625 | |
| 3626 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 3627 | parent); |
| 3628 | bt_get(tag); |
| 3629 | if (variant->tag) { |
| 3630 | bt_put(&variant->tag->parent); |
| 3631 | } |
| 3632 | variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration, |
| 3633 | parent); |
| 3634 | BT_LOGV("Set variant field type's tag field type: variant-ft-addr=%p, " |
| 3635 | "tag-ft-addr=%p", type, tag); |
| 3636 | end: |
| 3637 | return ret; |
| 3638 | } |
| 3639 | |
| 3640 | static |
| 3641 | void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type) |
| 3642 | { |
| 3643 | struct bt_ctf_field_type_integer *integer = |
| 3644 | (struct bt_ctf_field_type_integer *) type; |
| 3645 | |
| 3646 | if (!type) { |
| 3647 | return; |
| 3648 | } |
| 3649 | |
| 3650 | BT_LOGD("Destroying integer field type object: addr=%p", type); |
| 3651 | BT_LOGD_STR("Putting mapped clock class."); |
| 3652 | bt_put(integer->mapped_clock); |
| 3653 | g_free(integer); |
| 3654 | } |
| 3655 | |
| 3656 | static |
| 3657 | void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type) |
| 3658 | { |
| 3659 | struct bt_ctf_field_type_enumeration *enumeration = |
| 3660 | (struct bt_ctf_field_type_enumeration *) type; |
| 3661 | |
| 3662 | if (!type) { |
| 3663 | return; |
| 3664 | } |
| 3665 | |
| 3666 | BT_LOGD("Destroying enumeration field type object: addr=%p", type); |
| 3667 | g_ptr_array_free(enumeration->entries, TRUE); |
| 3668 | BT_LOGD_STR("Putting container field type."); |
| 3669 | bt_put(enumeration->container); |
| 3670 | g_free(enumeration); |
| 3671 | } |
| 3672 | |
| 3673 | static |
| 3674 | void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type) |
| 3675 | { |
| 3676 | struct bt_ctf_field_type_floating_point *floating_point = |
| 3677 | (struct bt_ctf_field_type_floating_point *) type; |
| 3678 | |
| 3679 | if (!type) { |
| 3680 | return; |
| 3681 | } |
| 3682 | |
| 3683 | BT_LOGD("Destroying floating point number field type object: addr=%p", type); |
| 3684 | g_free(floating_point); |
| 3685 | } |
| 3686 | |
| 3687 | static |
| 3688 | void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type) |
| 3689 | { |
| 3690 | struct bt_ctf_field_type_structure *structure = |
| 3691 | (struct bt_ctf_field_type_structure *) type; |
| 3692 | |
| 3693 | if (!type) { |
| 3694 | return; |
| 3695 | } |
| 3696 | |
| 3697 | BT_LOGD("Destroying structure field type object: addr=%p", type); |
| 3698 | g_ptr_array_free(structure->fields, TRUE); |
| 3699 | g_hash_table_destroy(structure->field_name_to_index); |
| 3700 | g_free(structure); |
| 3701 | } |
| 3702 | |
| 3703 | static |
| 3704 | void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type) |
| 3705 | { |
| 3706 | struct bt_ctf_field_type_variant *variant = |
| 3707 | (struct bt_ctf_field_type_variant *) type; |
| 3708 | |
| 3709 | if (!type) { |
| 3710 | return; |
| 3711 | } |
| 3712 | |
| 3713 | BT_LOGD("Destroying variant field type object: addr=%p", type); |
| 3714 | g_ptr_array_free(variant->fields, TRUE); |
| 3715 | g_hash_table_destroy(variant->field_name_to_index); |
| 3716 | g_string_free(variant->tag_name, TRUE); |
| 3717 | BT_LOGD_STR("Putting tag field type."); |
| 3718 | bt_put(&variant->tag->parent); |
| 3719 | BT_PUT(variant->tag_field_path); |
| 3720 | g_free(variant); |
| 3721 | } |
| 3722 | |
| 3723 | static |
| 3724 | void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type) |
| 3725 | { |
| 3726 | struct bt_ctf_field_type_array *array = |
| 3727 | (struct bt_ctf_field_type_array *) type; |
| 3728 | |
| 3729 | if (!type) { |
| 3730 | return; |
| 3731 | } |
| 3732 | |
| 3733 | BT_LOGD("Destroying array field type object: addr=%p", type); |
| 3734 | BT_LOGD_STR("Putting element field type."); |
| 3735 | bt_put(array->element_type); |
| 3736 | g_free(array); |
| 3737 | } |
| 3738 | |
| 3739 | static |
| 3740 | void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type) |
| 3741 | { |
| 3742 | struct bt_ctf_field_type_sequence *sequence = |
| 3743 | (struct bt_ctf_field_type_sequence *) type; |
| 3744 | |
| 3745 | if (!type) { |
| 3746 | return; |
| 3747 | } |
| 3748 | |
| 3749 | BT_LOGD("Destroying sequence field type object: addr=%p", type); |
| 3750 | BT_LOGD_STR("Putting element field type."); |
| 3751 | bt_put(sequence->element_type); |
| 3752 | g_string_free(sequence->length_field_name, TRUE); |
| 3753 | BT_LOGD_STR("Putting length field path."); |
| 3754 | BT_PUT(sequence->length_field_path); |
| 3755 | g_free(sequence); |
| 3756 | } |
| 3757 | |
| 3758 | static |
| 3759 | void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type) |
| 3760 | { |
| 3761 | struct bt_ctf_field_type_string *string = |
| 3762 | (struct bt_ctf_field_type_string *) type; |
| 3763 | |
| 3764 | if (!type) { |
| 3765 | return; |
| 3766 | } |
| 3767 | |
| 3768 | BT_LOGD("Destroying string field type object: addr=%p", type); |
| 3769 | g_free(string); |
| 3770 | } |
| 3771 | |
| 3772 | static |
| 3773 | void generic_field_type_freeze(struct bt_ctf_field_type *type) |
| 3774 | { |
| 3775 | type->frozen = 1; |
| 3776 | } |
| 3777 | |
| 3778 | static |
| 3779 | void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *type) |
| 3780 | { |
| 3781 | struct bt_ctf_field_type_integer *integer_type = container_of( |
| 3782 | type, struct bt_ctf_field_type_integer, parent); |
| 3783 | |
| 3784 | BT_LOGD("Freezing integer field type object: addr=%p", type); |
| 3785 | |
| 3786 | if (integer_type->mapped_clock) { |
| 3787 | BT_LOGD_STR("Freezing integer field type's mapped clock class."); |
| 3788 | bt_ctf_clock_class_freeze(integer_type->mapped_clock); |
| 3789 | } |
| 3790 | |
| 3791 | generic_field_type_freeze(type); |
| 3792 | } |
| 3793 | |
| 3794 | static |
| 3795 | void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type) |
| 3796 | { |
| 3797 | struct bt_ctf_field_type_enumeration *enumeration_type = container_of( |
| 3798 | type, struct bt_ctf_field_type_enumeration, parent); |
| 3799 | |
| 3800 | BT_LOGD("Freezing enumeration field type object: addr=%p", type); |
| 3801 | set_enumeration_range_overlap(type); |
| 3802 | generic_field_type_freeze(type); |
| 3803 | BT_LOGD("Freezing enumeration field type object's container field type: int-ft-addr=%p", |
| 3804 | enumeration_type->container); |
| 3805 | bt_ctf_field_type_freeze(enumeration_type->container); |
| 3806 | } |
| 3807 | |
| 3808 | static |
| 3809 | void freeze_structure_field(struct structure_field *field) |
| 3810 | { |
| 3811 | BT_LOGD("Freezing structure/variant field type field: field-addr=%p, " |
| 3812 | "field-ft-addr=%p, field-name=\"%s\"", field, |
| 3813 | field->type, g_quark_to_string(field->name)); |
| 3814 | bt_ctf_field_type_freeze(field->type); |
| 3815 | } |
| 3816 | |
| 3817 | static |
| 3818 | void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type) |
| 3819 | { |
| 3820 | struct bt_ctf_field_type_structure *structure_type = container_of( |
| 3821 | type, struct bt_ctf_field_type_structure, parent); |
| 3822 | |
| 3823 | /* Cache the alignment */ |
| 3824 | BT_LOGD("Freezing structure field type object: addr=%p", type); |
| 3825 | type->alignment = bt_ctf_field_type_get_alignment(type); |
| 3826 | generic_field_type_freeze(type); |
| 3827 | g_ptr_array_foreach(structure_type->fields, |
| 3828 | (GFunc) freeze_structure_field, NULL); |
| 3829 | } |
| 3830 | |
| 3831 | static |
| 3832 | void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type) |
| 3833 | { |
| 3834 | struct bt_ctf_field_type_variant *variant_type = container_of( |
| 3835 | type, struct bt_ctf_field_type_variant, parent); |
| 3836 | |
| 3837 | BT_LOGD("Freezing variant field type object: addr=%p", type); |
| 3838 | generic_field_type_freeze(type); |
| 3839 | g_ptr_array_foreach(variant_type->fields, |
| 3840 | (GFunc) freeze_structure_field, NULL); |
| 3841 | } |
| 3842 | |
| 3843 | static |
| 3844 | void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type) |
| 3845 | { |
| 3846 | struct bt_ctf_field_type_array *array_type = container_of( |
| 3847 | type, struct bt_ctf_field_type_array, parent); |
| 3848 | |
| 3849 | /* Cache the alignment */ |
| 3850 | BT_LOGD("Freezing array field type object: addr=%p", type); |
| 3851 | type->alignment = bt_ctf_field_type_get_alignment(type); |
| 3852 | generic_field_type_freeze(type); |
| 3853 | BT_LOGD("Freezing array field type object's element field type: element-ft-addr=%p", |
| 3854 | array_type->element_type); |
| 3855 | bt_ctf_field_type_freeze(array_type->element_type); |
| 3856 | } |
| 3857 | |
| 3858 | static |
| 3859 | void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type) |
| 3860 | { |
| 3861 | struct bt_ctf_field_type_sequence *sequence_type = container_of( |
| 3862 | type, struct bt_ctf_field_type_sequence, parent); |
| 3863 | |
| 3864 | /* Cache the alignment */ |
| 3865 | BT_LOGD("Freezing sequence field type object: addr=%p", type); |
| 3866 | type->alignment = bt_ctf_field_type_get_alignment(type); |
| 3867 | generic_field_type_freeze(type); |
| 3868 | BT_LOGD("Freezing sequence field type object's element field type: element-ft-addr=%p", |
| 3869 | sequence_type->element_type); |
| 3870 | bt_ctf_field_type_freeze(sequence_type->element_type); |
| 3871 | } |
| 3872 | |
| 3873 | static |
| 3874 | const char *get_encoding_string(enum bt_ctf_string_encoding encoding) |
| 3875 | { |
| 3876 | const char *encoding_string; |
| 3877 | |
| 3878 | switch (encoding) { |
| 3879 | case BT_CTF_STRING_ENCODING_NONE: |
| 3880 | encoding_string = "none"; |
| 3881 | break; |
| 3882 | case BT_CTF_STRING_ENCODING_ASCII: |
| 3883 | encoding_string = "ASCII"; |
| 3884 | break; |
| 3885 | case BT_CTF_STRING_ENCODING_UTF8: |
| 3886 | encoding_string = "UTF8"; |
| 3887 | break; |
| 3888 | default: |
| 3889 | encoding_string = "unknown"; |
| 3890 | break; |
| 3891 | } |
| 3892 | |
| 3893 | return encoding_string; |
| 3894 | } |
| 3895 | |
| 3896 | static |
| 3897 | const char *get_integer_base_string(enum bt_ctf_integer_base base) |
| 3898 | { |
| 3899 | const char *base_string; |
| 3900 | |
| 3901 | switch (base) { |
| 3902 | case BT_CTF_INTEGER_BASE_DECIMAL: |
| 3903 | base_string = "decimal"; |
| 3904 | break; |
| 3905 | case BT_CTF_INTEGER_BASE_HEXADECIMAL: |
| 3906 | base_string = "hexadecimal"; |
| 3907 | break; |
| 3908 | case BT_CTF_INTEGER_BASE_OCTAL: |
| 3909 | base_string = "octal"; |
| 3910 | break; |
| 3911 | case BT_CTF_INTEGER_BASE_BINARY: |
| 3912 | base_string = "binary"; |
| 3913 | break; |
| 3914 | default: |
| 3915 | base_string = "unknown"; |
| 3916 | break; |
| 3917 | } |
| 3918 | |
| 3919 | return base_string; |
| 3920 | } |
| 3921 | |
| 3922 | static |
| 3923 | int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type, |
| 3924 | struct metadata_context *context) |
| 3925 | { |
| 3926 | struct bt_ctf_field_type_integer *integer = container_of(type, |
| 3927 | struct bt_ctf_field_type_integer, parent); |
| 3928 | int ret = 0; |
| 3929 | |
| 3930 | BT_LOGD("Serializing integer field type's metadata: " |
| 3931 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 3932 | g_string_append_printf(context->string, |
| 3933 | "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s", |
| 3934 | integer->size, type->alignment, |
| 3935 | (integer->is_signed ? "true" : "false"), |
| 3936 | get_encoding_string(integer->encoding), |
| 3937 | get_integer_base_string(integer->base), |
| 3938 | get_byte_order_string(integer->user_byte_order)); |
| 3939 | if (integer->mapped_clock) { |
| 3940 | const char *clock_name = bt_ctf_clock_class_get_name( |
| 3941 | integer->mapped_clock); |
| 3942 | |
| 3943 | assert(clock_name); |
| 3944 | g_string_append_printf(context->string, |
| 3945 | "; map = clock.%s.value", clock_name); |
| 3946 | } |
| 3947 | |
| 3948 | g_string_append(context->string, "; }"); |
| 3949 | return ret; |
| 3950 | } |
| 3951 | |
| 3952 | static |
| 3953 | int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type, |
| 3954 | struct metadata_context *context) |
| 3955 | { |
| 3956 | size_t entry; |
| 3957 | int ret; |
| 3958 | struct bt_ctf_field_type_enumeration *enumeration = container_of(type, |
| 3959 | struct bt_ctf_field_type_enumeration, parent); |
| 3960 | struct bt_ctf_field_type *container_type; |
| 3961 | int container_signed; |
| 3962 | |
| 3963 | BT_LOGD("Serializing enumeration field type's metadata: " |
| 3964 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 3965 | container_type = bt_ctf_field_type_enumeration_get_container_type(type); |
| 3966 | assert(container_type); |
| 3967 | container_signed = bt_ctf_field_type_integer_get_signed(container_type); |
| 3968 | assert(container_signed >= 0); |
| 3969 | g_string_append(context->string, "enum : "); |
| 3970 | BT_LOGD_STR("Serializing enumeration field type's container field type's metadata."); |
| 3971 | ret = bt_ctf_field_type_serialize(enumeration->container, context); |
| 3972 | if (ret) { |
| 3973 | BT_LOGW("Cannot serialize enumeration field type's container field type's metadata: " |
| 3974 | "container-ft-addr=%p", enumeration->container); |
| 3975 | goto end; |
| 3976 | } |
| 3977 | |
| 3978 | g_string_append(context->string, " { "); |
| 3979 | for (entry = 0; entry < enumeration->entries->len; entry++) { |
| 3980 | struct enumeration_mapping *mapping = |
| 3981 | enumeration->entries->pdata[entry]; |
| 3982 | |
| 3983 | if (container_signed) { |
| 3984 | if (mapping->range_start._signed == |
| 3985 | mapping->range_end._signed) { |
| 3986 | g_string_append_printf(context->string, |
| 3987 | "\"%s\" = %" PRId64, |
| 3988 | g_quark_to_string(mapping->string), |
| 3989 | mapping->range_start._signed); |
| 3990 | } else { |
| 3991 | g_string_append_printf(context->string, |
| 3992 | "\"%s\" = %" PRId64 " ... %" PRId64, |
| 3993 | g_quark_to_string(mapping->string), |
| 3994 | mapping->range_start._signed, |
| 3995 | mapping->range_end._signed); |
| 3996 | } |
| 3997 | } else { |
| 3998 | if (mapping->range_start._unsigned == |
| 3999 | mapping->range_end._unsigned) { |
| 4000 | g_string_append_printf(context->string, |
| 4001 | "\"%s\" = %" PRIu64, |
| 4002 | g_quark_to_string(mapping->string), |
| 4003 | mapping->range_start._unsigned); |
| 4004 | } else { |
| 4005 | g_string_append_printf(context->string, |
| 4006 | "\"%s\" = %" PRIu64 " ... %" PRIu64, |
| 4007 | g_quark_to_string(mapping->string), |
| 4008 | mapping->range_start._unsigned, |
| 4009 | mapping->range_end._unsigned); |
| 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | g_string_append(context->string, |
| 4014 | ((entry != (enumeration->entries->len - 1)) ? |
| 4015 | ", " : " }")); |
| 4016 | } |
| 4017 | |
| 4018 | if (context->field_name->len) { |
| 4019 | g_string_append_printf(context->string, " %s", |
| 4020 | context->field_name->str); |
| 4021 | g_string_assign(context->field_name, ""); |
| 4022 | } |
| 4023 | end: |
| 4024 | bt_put(container_type); |
| 4025 | return ret; |
| 4026 | } |
| 4027 | |
| 4028 | static |
| 4029 | int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type, |
| 4030 | struct metadata_context *context) |
| 4031 | { |
| 4032 | struct bt_ctf_field_type_floating_point *floating_point = container_of( |
| 4033 | type, struct bt_ctf_field_type_floating_point, parent); |
| 4034 | |
| 4035 | BT_LOGD("Serializing floating point number field type's metadata: " |
| 4036 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 4037 | g_string_append_printf(context->string, |
| 4038 | "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }", |
| 4039 | floating_point->exp_dig, |
| 4040 | floating_point->mant_dig, |
| 4041 | get_byte_order_string(floating_point->user_byte_order), |
| 4042 | type->alignment); |
| 4043 | return 0; |
| 4044 | } |
| 4045 | |
| 4046 | static |
| 4047 | int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type, |
| 4048 | struct metadata_context *context) |
| 4049 | { |
| 4050 | size_t i; |
| 4051 | unsigned int indent; |
| 4052 | int ret = 0; |
| 4053 | struct bt_ctf_field_type_structure *structure = container_of(type, |
| 4054 | struct bt_ctf_field_type_structure, parent); |
| 4055 | GString *structure_field_name = context->field_name; |
| 4056 | |
| 4057 | BT_LOGD("Serializing structure field type's metadata: " |
| 4058 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 4059 | context->field_name = g_string_new(""); |
| 4060 | |
| 4061 | context->current_indentation_level++; |
| 4062 | g_string_append(context->string, "struct {\n"); |
| 4063 | |
| 4064 | for (i = 0; i < structure->fields->len; i++) { |
| 4065 | struct structure_field *field = structure->fields->pdata[i]; |
| 4066 | |
| 4067 | BT_LOGD("Serializing structure field type's field metadata: " |
| 4068 | "index=%" PRId64 ", " |
| 4069 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4070 | i, field, g_quark_to_string(field->name)); |
| 4071 | |
| 4072 | for (indent = 0; indent < context->current_indentation_level; |
| 4073 | indent++) { |
| 4074 | g_string_append_c(context->string, '\t'); |
| 4075 | } |
| 4076 | |
| 4077 | g_string_assign(context->field_name, |
| 4078 | g_quark_to_string(field->name)); |
| 4079 | ret = bt_ctf_field_type_serialize(field->type, context); |
| 4080 | if (ret) { |
| 4081 | BT_LOGW("Cannot serialize structure field type's field's metadata: " |
| 4082 | "index=%" PRId64 ", " |
| 4083 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4084 | i, field->type, |
| 4085 | g_quark_to_string(field->name)); |
| 4086 | goto end; |
| 4087 | } |
| 4088 | |
| 4089 | if (context->field_name->len) { |
| 4090 | g_string_append_printf(context->string, " %s", |
| 4091 | context->field_name->str); |
| 4092 | } |
| 4093 | g_string_append(context->string, ";\n"); |
| 4094 | } |
| 4095 | |
| 4096 | context->current_indentation_level--; |
| 4097 | for (indent = 0; indent < context->current_indentation_level; |
| 4098 | indent++) { |
| 4099 | g_string_append_c(context->string, '\t'); |
| 4100 | } |
| 4101 | |
| 4102 | g_string_append_printf(context->string, "} align(%u)", |
| 4103 | type->alignment); |
| 4104 | end: |
| 4105 | g_string_free(context->field_name, TRUE); |
| 4106 | context->field_name = structure_field_name; |
| 4107 | return ret; |
| 4108 | } |
| 4109 | |
| 4110 | static |
| 4111 | int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type, |
| 4112 | struct metadata_context *context) |
| 4113 | { |
| 4114 | size_t i; |
| 4115 | unsigned int indent; |
| 4116 | int ret = 0; |
| 4117 | struct bt_ctf_field_type_variant *variant = container_of( |
| 4118 | type, struct bt_ctf_field_type_variant, parent); |
| 4119 | GString *variant_field_name = context->field_name; |
| 4120 | |
| 4121 | BT_LOGD("Serializing variant field type's metadata: " |
| 4122 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 4123 | context->field_name = g_string_new(""); |
| 4124 | if (variant->tag_name->len > 0) { |
| 4125 | g_string_append_printf(context->string, |
| 4126 | "variant <%s> {\n", variant->tag_name->str); |
| 4127 | } else { |
| 4128 | g_string_append(context->string, "variant {\n"); |
| 4129 | } |
| 4130 | |
| 4131 | context->current_indentation_level++; |
| 4132 | for (i = 0; i < variant->fields->len; i++) { |
| 4133 | struct structure_field *field = variant->fields->pdata[i]; |
| 4134 | |
| 4135 | BT_LOGD("Serializing variant field type's field metadata: " |
| 4136 | "index=%" PRId64 ", " |
| 4137 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4138 | i, field, g_quark_to_string(field->name)); |
| 4139 | |
| 4140 | g_string_assign(context->field_name, |
| 4141 | g_quark_to_string(field->name)); |
| 4142 | for (indent = 0; indent < context->current_indentation_level; |
| 4143 | indent++) { |
| 4144 | g_string_append_c(context->string, '\t'); |
| 4145 | } |
| 4146 | |
| 4147 | g_string_assign(context->field_name, |
| 4148 | g_quark_to_string(field->name)); |
| 4149 | ret = bt_ctf_field_type_serialize(field->type, context); |
| 4150 | if (ret) { |
| 4151 | BT_LOGW("Cannot serialize variant field type's field's metadata: " |
| 4152 | "index=%" PRId64 ", " |
| 4153 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4154 | i, field->type, |
| 4155 | g_quark_to_string(field->name)); |
| 4156 | goto end; |
| 4157 | } |
| 4158 | |
| 4159 | if (context->field_name->len) { |
| 4160 | g_string_append_printf(context->string, " %s;", |
| 4161 | context->field_name->str); |
| 4162 | } |
| 4163 | |
| 4164 | g_string_append_c(context->string, '\n'); |
| 4165 | } |
| 4166 | |
| 4167 | context->current_indentation_level--; |
| 4168 | for (indent = 0; indent < context->current_indentation_level; |
| 4169 | indent++) { |
| 4170 | g_string_append_c(context->string, '\t'); |
| 4171 | } |
| 4172 | |
| 4173 | g_string_append(context->string, "}"); |
| 4174 | end: |
| 4175 | g_string_free(context->field_name, TRUE); |
| 4176 | context->field_name = variant_field_name; |
| 4177 | return ret; |
| 4178 | } |
| 4179 | |
| 4180 | static |
| 4181 | int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type, |
| 4182 | struct metadata_context *context) |
| 4183 | { |
| 4184 | int ret = 0; |
| 4185 | struct bt_ctf_field_type_array *array = container_of(type, |
| 4186 | struct bt_ctf_field_type_array, parent); |
| 4187 | |
| 4188 | BT_LOGD("Serializing array field type's metadata: " |
| 4189 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 4190 | BT_LOGD_STR("Serializing array field type's element field type's metadata."); |
| 4191 | ret = bt_ctf_field_type_serialize(array->element_type, context); |
| 4192 | if (ret) { |
| 4193 | BT_LOGW("Cannot serialize array field type's element field type's metadata: " |
| 4194 | "element-ft-addr=%p", array->element_type); |
| 4195 | goto end; |
| 4196 | } |
| 4197 | |
| 4198 | if (context->field_name->len) { |
| 4199 | g_string_append_printf(context->string, " %s[%u]", |
| 4200 | context->field_name->str, array->length); |
| 4201 | g_string_assign(context->field_name, ""); |
| 4202 | } else { |
| 4203 | g_string_append_printf(context->string, "[%u]", array->length); |
| 4204 | } |
| 4205 | end: |
| 4206 | return ret; |
| 4207 | } |
| 4208 | |
| 4209 | static |
| 4210 | int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type, |
| 4211 | struct metadata_context *context) |
| 4212 | { |
| 4213 | int ret = 0; |
| 4214 | struct bt_ctf_field_type_sequence *sequence = container_of( |
| 4215 | type, struct bt_ctf_field_type_sequence, parent); |
| 4216 | |
| 4217 | BT_LOGD("Serializing sequence field type's metadata: " |
| 4218 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 4219 | BT_LOGD_STR("Serializing sequence field type's element field type's metadata."); |
| 4220 | ret = bt_ctf_field_type_serialize(sequence->element_type, context); |
| 4221 | if (ret) { |
| 4222 | BT_LOGW("Cannot serialize sequence field type's element field type's metadata: " |
| 4223 | "element-ft-addr=%p", sequence->element_type); |
| 4224 | goto end; |
| 4225 | } |
| 4226 | |
| 4227 | if (context->field_name->len) { |
| 4228 | g_string_append_printf(context->string, " %s[%s]", |
| 4229 | context->field_name->str, |
| 4230 | sequence->length_field_name->str); |
| 4231 | g_string_assign(context->field_name, ""); |
| 4232 | } else { |
| 4233 | g_string_append_printf(context->string, "[%s]", |
| 4234 | sequence->length_field_name->str); |
| 4235 | } |
| 4236 | end: |
| 4237 | return ret; |
| 4238 | } |
| 4239 | |
| 4240 | static |
| 4241 | int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type, |
| 4242 | struct metadata_context *context) |
| 4243 | { |
| 4244 | struct bt_ctf_field_type_string *string = container_of( |
| 4245 | type, struct bt_ctf_field_type_string, parent); |
| 4246 | |
| 4247 | BT_LOGD("Serializing string field type's metadata: " |
| 4248 | "ft-addr=%p, metadata-context-addr=%p", type, context); |
| 4249 | g_string_append_printf(context->string, |
| 4250 | "string { encoding = %s; }", |
| 4251 | get_encoding_string(string->encoding)); |
| 4252 | return 0; |
| 4253 | } |
| 4254 | |
| 4255 | static |
| 4256 | void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type, |
| 4257 | enum bt_ctf_byte_order byte_order) |
| 4258 | { |
| 4259 | struct bt_ctf_field_type_integer *integer_type = container_of(type, |
| 4260 | struct bt_ctf_field_type_integer, parent); |
| 4261 | |
| 4262 | integer_type->user_byte_order = byte_order; |
| 4263 | } |
| 4264 | |
| 4265 | static |
| 4266 | void bt_ctf_field_type_enumeration_set_byte_order( |
| 4267 | struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order) |
| 4268 | { |
| 4269 | struct bt_ctf_field_type_enumeration *enum_type = container_of(type, |
| 4270 | struct bt_ctf_field_type_enumeration, parent); |
| 4271 | |
| 4272 | /* Safe to assume that container is an integer */ |
| 4273 | bt_ctf_field_type_integer_set_byte_order(enum_type->container, |
| 4274 | byte_order); |
| 4275 | } |
| 4276 | |
| 4277 | static |
| 4278 | void bt_ctf_field_type_floating_point_set_byte_order( |
| 4279 | struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order) |
| 4280 | { |
| 4281 | struct bt_ctf_field_type_floating_point *floating_point_type = |
| 4282 | container_of(type, struct bt_ctf_field_type_floating_point, |
| 4283 | parent); |
| 4284 | |
| 4285 | floating_point_type->user_byte_order = byte_order; |
| 4286 | } |
| 4287 | |
| 4288 | static |
| 4289 | void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type, |
| 4290 | enum bt_ctf_byte_order byte_order) |
| 4291 | { |
| 4292 | int i; |
| 4293 | struct bt_ctf_field_type_structure *structure_type = |
| 4294 | container_of(type, struct bt_ctf_field_type_structure, |
| 4295 | parent); |
| 4296 | |
| 4297 | for (i = 0; i < structure_type->fields->len; i++) { |
| 4298 | struct structure_field *field = g_ptr_array_index( |
| 4299 | structure_type->fields, i); |
| 4300 | struct bt_ctf_field_type *field_type = field->type; |
| 4301 | |
| 4302 | if (set_byte_order_funcs[field_type->id]) { |
| 4303 | set_byte_order_funcs[field_type->id]( |
| 4304 | field_type, byte_order); |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | static |
| 4310 | void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type, |
| 4311 | enum bt_ctf_byte_order byte_order) |
| 4312 | { |
| 4313 | int i; |
| 4314 | struct bt_ctf_field_type_variant *variant_type = |
| 4315 | container_of(type, struct bt_ctf_field_type_variant, |
| 4316 | parent); |
| 4317 | |
| 4318 | for (i = 0; i < variant_type->fields->len; i++) { |
| 4319 | struct structure_field *field = g_ptr_array_index( |
| 4320 | variant_type->fields, i); |
| 4321 | struct bt_ctf_field_type *field_type = field->type; |
| 4322 | |
| 4323 | if (set_byte_order_funcs[field_type->id]) { |
| 4324 | set_byte_order_funcs[field_type->id]( |
| 4325 | field_type, byte_order); |
| 4326 | } |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | static |
| 4331 | void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type, |
| 4332 | enum bt_ctf_byte_order byte_order) |
| 4333 | { |
| 4334 | struct bt_ctf_field_type_array *array_type = |
| 4335 | container_of(type, struct bt_ctf_field_type_array, |
| 4336 | parent); |
| 4337 | |
| 4338 | if (set_byte_order_funcs[array_type->element_type->id]) { |
| 4339 | set_byte_order_funcs[array_type->element_type->id]( |
| 4340 | array_type->element_type, byte_order); |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | static |
| 4345 | void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type, |
| 4346 | enum bt_ctf_byte_order byte_order) |
| 4347 | { |
| 4348 | struct bt_ctf_field_type_sequence *sequence_type = |
| 4349 | container_of(type, struct bt_ctf_field_type_sequence, |
| 4350 | parent); |
| 4351 | |
| 4352 | if (set_byte_order_funcs[ |
| 4353 | sequence_type->element_type->id]) { |
| 4354 | set_byte_order_funcs[ |
| 4355 | sequence_type->element_type->id]( |
| 4356 | sequence_type->element_type, byte_order); |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | static |
| 4361 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( |
| 4362 | struct bt_ctf_field_type *type) |
| 4363 | { |
| 4364 | struct bt_ctf_field_type *copy; |
| 4365 | struct bt_ctf_field_type_integer *integer, *copy_integer; |
| 4366 | |
| 4367 | BT_LOGD("Copying integer field type's: addr=%p", type); |
| 4368 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 4369 | copy = bt_ctf_field_type_integer_create(integer->size); |
| 4370 | if (!copy) { |
| 4371 | BT_LOGE_STR("Cannot create integer field type."); |
| 4372 | goto end; |
| 4373 | } |
| 4374 | |
| 4375 | copy_integer = container_of(copy, struct bt_ctf_field_type_integer, |
| 4376 | parent); |
| 4377 | copy_integer->mapped_clock = bt_get(integer->mapped_clock); |
| 4378 | copy_integer->user_byte_order = integer->user_byte_order; |
| 4379 | copy_integer->is_signed = integer->is_signed; |
| 4380 | copy_integer->size = integer->size; |
| 4381 | copy_integer->base = integer->base; |
| 4382 | copy_integer->encoding = integer->encoding; |
| 4383 | BT_LOGD("Copied integer field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4384 | type, copy); |
| 4385 | |
| 4386 | end: |
| 4387 | return copy; |
| 4388 | } |
| 4389 | |
| 4390 | static |
| 4391 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( |
| 4392 | struct bt_ctf_field_type *type) |
| 4393 | { |
| 4394 | size_t i; |
| 4395 | struct bt_ctf_field_type *copy = NULL, *copy_container; |
| 4396 | struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration; |
| 4397 | |
| 4398 | BT_LOGD("Copying enumeration field type's: addr=%p", type); |
| 4399 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 4400 | parent); |
| 4401 | |
| 4402 | /* Copy the source enumeration's container */ |
| 4403 | BT_LOGD_STR("Copying enumeration field type's container field type."); |
| 4404 | copy_container = bt_ctf_field_type_copy(enumeration->container); |
| 4405 | if (!copy_container) { |
| 4406 | BT_LOGE_STR("Cannot copy enumeration field type's container field type."); |
| 4407 | goto end; |
| 4408 | } |
| 4409 | |
| 4410 | copy = bt_ctf_field_type_enumeration_create(copy_container); |
| 4411 | if (!copy) { |
| 4412 | BT_LOGE_STR("Cannot create enumeration field type."); |
| 4413 | goto end; |
| 4414 | } |
| 4415 | copy_enumeration = container_of(copy, |
| 4416 | struct bt_ctf_field_type_enumeration, parent); |
| 4417 | |
| 4418 | /* Copy all enumaration entries */ |
| 4419 | for (i = 0; i < enumeration->entries->len; i++) { |
| 4420 | struct enumeration_mapping *mapping = g_ptr_array_index( |
| 4421 | enumeration->entries, i); |
| 4422 | struct enumeration_mapping *copy_mapping = g_new0( |
| 4423 | struct enumeration_mapping, 1); |
| 4424 | |
| 4425 | if (!copy_mapping) { |
| 4426 | BT_LOGE_STR("Failed to allocate one enumeration mapping."); |
| 4427 | goto error; |
| 4428 | } |
| 4429 | |
| 4430 | *copy_mapping = *mapping; |
| 4431 | g_ptr_array_add(copy_enumeration->entries, copy_mapping); |
| 4432 | } |
| 4433 | |
| 4434 | BT_LOGD("Copied enumeration field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4435 | type, copy); |
| 4436 | |
| 4437 | end: |
| 4438 | bt_put(copy_container); |
| 4439 | return copy; |
| 4440 | error: |
| 4441 | bt_put(copy_container); |
| 4442 | BT_PUT(copy); |
| 4443 | return copy; |
| 4444 | } |
| 4445 | |
| 4446 | static |
| 4447 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( |
| 4448 | struct bt_ctf_field_type *type) |
| 4449 | { |
| 4450 | struct bt_ctf_field_type *copy; |
| 4451 | struct bt_ctf_field_type_floating_point *floating_point, *copy_float; |
| 4452 | |
| 4453 | BT_LOGD("Copying floating point number field type's: addr=%p", type); |
| 4454 | floating_point = container_of(type, |
| 4455 | struct bt_ctf_field_type_floating_point, parent); |
| 4456 | copy = bt_ctf_field_type_floating_point_create(); |
| 4457 | if (!copy) { |
| 4458 | BT_LOGE_STR("Cannot create floating point number field type."); |
| 4459 | goto end; |
| 4460 | } |
| 4461 | |
| 4462 | copy_float = container_of(copy, |
| 4463 | struct bt_ctf_field_type_floating_point, parent); |
| 4464 | copy_float->user_byte_order = floating_point->user_byte_order; |
| 4465 | copy_float->exp_dig = floating_point->exp_dig; |
| 4466 | copy_float->mant_dig = floating_point->mant_dig; |
| 4467 | BT_LOGD("Copied floating point number field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4468 | type, copy); |
| 4469 | end: |
| 4470 | return copy; |
| 4471 | } |
| 4472 | |
| 4473 | static |
| 4474 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( |
| 4475 | struct bt_ctf_field_type *type) |
| 4476 | { |
| 4477 | int64_t i; |
| 4478 | GHashTableIter iter; |
| 4479 | gpointer key, value; |
| 4480 | struct bt_ctf_field_type *copy; |
| 4481 | struct bt_ctf_field_type_structure *structure, *copy_structure; |
| 4482 | |
| 4483 | BT_LOGD("Copying structure field type's: addr=%p", type); |
| 4484 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 4485 | parent); |
| 4486 | copy = bt_ctf_field_type_structure_create(); |
| 4487 | if (!copy) { |
| 4488 | BT_LOGE_STR("Cannot create structure field type."); |
| 4489 | goto end; |
| 4490 | } |
| 4491 | |
| 4492 | copy_structure = container_of(copy, |
| 4493 | struct bt_ctf_field_type_structure, parent); |
| 4494 | |
| 4495 | /* Copy field_name_to_index */ |
| 4496 | g_hash_table_iter_init(&iter, structure->field_name_to_index); |
| 4497 | while (g_hash_table_iter_next (&iter, &key, &value)) { |
| 4498 | g_hash_table_insert(copy_structure->field_name_to_index, |
| 4499 | key, value); |
| 4500 | } |
| 4501 | |
| 4502 | for (i = 0; i < structure->fields->len; i++) { |
| 4503 | struct structure_field *entry, *copy_entry; |
| 4504 | struct bt_ctf_field_type *copy_field; |
| 4505 | |
| 4506 | entry = g_ptr_array_index(structure->fields, i); |
| 4507 | BT_LOGD("Copying structure field type's field: " |
| 4508 | "index=%" PRId64 ", " |
| 4509 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4510 | i, entry, g_quark_to_string(entry->name)); |
| 4511 | copy_entry = g_new0(struct structure_field, 1); |
| 4512 | if (!copy_entry) { |
| 4513 | BT_LOGE_STR("Failed to allocate one structure field type field."); |
| 4514 | goto error; |
| 4515 | } |
| 4516 | |
| 4517 | copy_field = bt_ctf_field_type_copy(entry->type); |
| 4518 | if (!copy_field) { |
| 4519 | BT_LOGE("Cannot copy structure field type's field: " |
| 4520 | "index=%" PRId64 ", " |
| 4521 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4522 | i, entry, g_quark_to_string(entry->name)); |
| 4523 | g_free(copy_entry); |
| 4524 | goto error; |
| 4525 | } |
| 4526 | |
| 4527 | copy_entry->name = entry->name; |
| 4528 | copy_entry->type = copy_field; |
| 4529 | g_ptr_array_add(copy_structure->fields, copy_entry); |
| 4530 | } |
| 4531 | |
| 4532 | BT_LOGD("Copied structure field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4533 | type, copy); |
| 4534 | |
| 4535 | end: |
| 4536 | return copy; |
| 4537 | error: |
| 4538 | BT_PUT(copy); |
| 4539 | return copy; |
| 4540 | } |
| 4541 | |
| 4542 | static |
| 4543 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( |
| 4544 | struct bt_ctf_field_type *type) |
| 4545 | { |
| 4546 | int64_t i; |
| 4547 | GHashTableIter iter; |
| 4548 | gpointer key, value; |
| 4549 | struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL; |
| 4550 | struct bt_ctf_field_type_variant *variant, *copy_variant; |
| 4551 | |
| 4552 | BT_LOGD("Copying variant field type's: addr=%p", type); |
| 4553 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 4554 | parent); |
| 4555 | if (variant->tag) { |
| 4556 | BT_LOGD_STR("Copying variant field type's tag field type."); |
| 4557 | copy_tag = bt_ctf_field_type_copy(&variant->tag->parent); |
| 4558 | if (!copy_tag) { |
| 4559 | BT_LOGE_STR("Cannot copy variant field type's tag field type."); |
| 4560 | goto end; |
| 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | copy = bt_ctf_field_type_variant_create(copy_tag, |
| 4565 | variant->tag_name->len ? variant->tag_name->str : NULL); |
| 4566 | if (!copy) { |
| 4567 | BT_LOGE_STR("Cannot create variant field type."); |
| 4568 | goto end; |
| 4569 | } |
| 4570 | |
| 4571 | copy_variant = container_of(copy, struct bt_ctf_field_type_variant, |
| 4572 | parent); |
| 4573 | |
| 4574 | /* Copy field_name_to_index */ |
| 4575 | g_hash_table_iter_init(&iter, variant->field_name_to_index); |
| 4576 | while (g_hash_table_iter_next (&iter, &key, &value)) { |
| 4577 | g_hash_table_insert(copy_variant->field_name_to_index, |
| 4578 | key, value); |
| 4579 | } |
| 4580 | |
| 4581 | for (i = 0; i < variant->fields->len; i++) { |
| 4582 | struct structure_field *entry, *copy_entry; |
| 4583 | struct bt_ctf_field_type *copy_field; |
| 4584 | |
| 4585 | entry = g_ptr_array_index(variant->fields, i); |
| 4586 | BT_LOGD("Copying variant field type's field: " |
| 4587 | "index=%" PRId64 ", " |
| 4588 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4589 | i, entry, g_quark_to_string(entry->name)); |
| 4590 | copy_entry = g_new0(struct structure_field, 1); |
| 4591 | if (!copy_entry) { |
| 4592 | BT_LOGE_STR("Failed to allocate one variant field type field."); |
| 4593 | goto error; |
| 4594 | } |
| 4595 | |
| 4596 | copy_field = bt_ctf_field_type_copy(entry->type); |
| 4597 | if (!copy_field) { |
| 4598 | BT_LOGE("Cannot copy variant field type's field: " |
| 4599 | "index=%" PRId64 ", " |
| 4600 | "field-ft-addr=%p, field-name=\"%s\"", |
| 4601 | i, entry, g_quark_to_string(entry->name)); |
| 4602 | g_free(copy_entry); |
| 4603 | goto error; |
| 4604 | } |
| 4605 | |
| 4606 | copy_entry->name = entry->name; |
| 4607 | copy_entry->type = copy_field; |
| 4608 | g_ptr_array_add(copy_variant->fields, copy_entry); |
| 4609 | } |
| 4610 | |
| 4611 | if (variant->tag_field_path) { |
| 4612 | BT_LOGD_STR("Copying variant field type's tag field path."); |
| 4613 | copy_variant->tag_field_path = bt_ctf_field_path_copy( |
| 4614 | variant->tag_field_path); |
| 4615 | if (!copy_variant->tag_field_path) { |
| 4616 | BT_LOGE_STR("Cannot copy variant field type's tag field path."); |
| 4617 | goto error; |
| 4618 | } |
| 4619 | } |
| 4620 | |
| 4621 | BT_LOGD("Copied variant field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4622 | type, copy); |
| 4623 | |
| 4624 | end: |
| 4625 | bt_put(copy_tag); |
| 4626 | return copy; |
| 4627 | error: |
| 4628 | bt_put(copy_tag); |
| 4629 | BT_PUT(copy); |
| 4630 | return copy; |
| 4631 | } |
| 4632 | |
| 4633 | static |
| 4634 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy( |
| 4635 | struct bt_ctf_field_type *type) |
| 4636 | { |
| 4637 | struct bt_ctf_field_type *copy = NULL, *copy_element; |
| 4638 | struct bt_ctf_field_type_array *array; |
| 4639 | |
| 4640 | BT_LOGD("Copying array field type's: addr=%p", type); |
| 4641 | array = container_of(type, struct bt_ctf_field_type_array, |
| 4642 | parent); |
| 4643 | BT_LOGD_STR("Copying array field type's element field type."); |
| 4644 | copy_element = bt_ctf_field_type_copy(array->element_type); |
| 4645 | if (!copy_element) { |
| 4646 | BT_LOGE_STR("Cannot copy array field type's element field type."); |
| 4647 | goto end; |
| 4648 | } |
| 4649 | |
| 4650 | copy = bt_ctf_field_type_array_create(copy_element, array->length); |
| 4651 | if (!copy) { |
| 4652 | BT_LOGE_STR("Cannot create array field type."); |
| 4653 | goto end; |
| 4654 | } |
| 4655 | |
| 4656 | BT_LOGD("Copied array field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4657 | type, copy); |
| 4658 | |
| 4659 | end: |
| 4660 | bt_put(copy_element); |
| 4661 | return copy; |
| 4662 | } |
| 4663 | |
| 4664 | static |
| 4665 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( |
| 4666 | struct bt_ctf_field_type *type) |
| 4667 | { |
| 4668 | struct bt_ctf_field_type *copy = NULL, *copy_element; |
| 4669 | struct bt_ctf_field_type_sequence *sequence, *copy_sequence; |
| 4670 | |
| 4671 | BT_LOGD("Copying sequence field type's: addr=%p", type); |
| 4672 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 4673 | parent); |
| 4674 | BT_LOGD_STR("Copying sequence field type's element field type."); |
| 4675 | copy_element = bt_ctf_field_type_copy(sequence->element_type); |
| 4676 | if (!copy_element) { |
| 4677 | BT_LOGE_STR("Cannot copy sequence field type's element field type."); |
| 4678 | goto end; |
| 4679 | } |
| 4680 | |
| 4681 | copy = bt_ctf_field_type_sequence_create(copy_element, |
| 4682 | sequence->length_field_name->len ? |
| 4683 | sequence->length_field_name->str : NULL); |
| 4684 | if (!copy) { |
| 4685 | BT_LOGE_STR("Cannot create sequence field type."); |
| 4686 | goto end; |
| 4687 | } |
| 4688 | |
| 4689 | copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence, |
| 4690 | parent); |
| 4691 | if (sequence->length_field_path) { |
| 4692 | BT_LOGD_STR("Copying sequence field type's length field path."); |
| 4693 | copy_sequence->length_field_path = bt_ctf_field_path_copy( |
| 4694 | sequence->length_field_path); |
| 4695 | if (!copy_sequence->length_field_path) { |
| 4696 | BT_LOGE_STR("Cannot copy sequence field type's length field path."); |
| 4697 | goto error; |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | BT_LOGD("Copied sequence field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4702 | type, copy); |
| 4703 | |
| 4704 | end: |
| 4705 | bt_put(copy_element); |
| 4706 | return copy; |
| 4707 | error: |
| 4708 | BT_PUT(copy); |
| 4709 | goto end; |
| 4710 | } |
| 4711 | |
| 4712 | static |
| 4713 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( |
| 4714 | struct bt_ctf_field_type *type) |
| 4715 | { |
| 4716 | struct bt_ctf_field_type *copy; |
| 4717 | struct bt_ctf_field_type_string *string; |
| 4718 | |
| 4719 | BT_LOGD("Copying string field type's: addr=%p", type); |
| 4720 | copy = bt_ctf_field_type_string_create(); |
| 4721 | if (!copy) { |
| 4722 | BT_LOGE_STR("Cannot create string field type."); |
| 4723 | goto end; |
| 4724 | } |
| 4725 | |
| 4726 | string = container_of(type, struct bt_ctf_field_type_string, |
| 4727 | parent); |
| 4728 | BT_LOGD("Copied string field type: original-ft-addr=%p, copy-ft-addr=%p", |
| 4729 | type, copy); |
| 4730 | end: |
| 4731 | return copy; |
| 4732 | } |
| 4733 | |
| 4734 | static |
| 4735 | int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *type_a, |
| 4736 | struct bt_ctf_field_type *type_b) |
| 4737 | { |
| 4738 | int ret = 1; |
| 4739 | struct bt_ctf_field_type_integer *int_type_a; |
| 4740 | struct bt_ctf_field_type_integer *int_type_b; |
| 4741 | |
| 4742 | int_type_a = container_of(type_a, struct bt_ctf_field_type_integer, |
| 4743 | parent); |
| 4744 | int_type_b = container_of(type_b, struct bt_ctf_field_type_integer, |
| 4745 | parent); |
| 4746 | |
| 4747 | /* Length */ |
| 4748 | if (int_type_a->size != int_type_b->size) { |
| 4749 | BT_LOGV("Integer field types differ: different sizes: " |
| 4750 | "ft-a-size=%u, ft-b-size=%u", |
| 4751 | int_type_a->size, int_type_b->size); |
| 4752 | goto end; |
| 4753 | } |
| 4754 | |
| 4755 | /* Byte order */ |
| 4756 | if (int_type_a->user_byte_order != int_type_b->user_byte_order) { |
| 4757 | BT_LOGV("Integer field types differ: different byte orders: " |
| 4758 | "ft-a-bo=%s, ft-b-bo=%s", |
| 4759 | bt_ctf_byte_order_string(int_type_a->user_byte_order), |
| 4760 | bt_ctf_byte_order_string(int_type_b->user_byte_order)); |
| 4761 | goto end; |
| 4762 | } |
| 4763 | |
| 4764 | /* Signedness */ |
| 4765 | if (int_type_a->is_signed != int_type_b->is_signed) { |
| 4766 | BT_LOGV("Integer field types differ: different signedness: " |
| 4767 | "ft-a-is-signed=%d, ft-b-is-signed=%d", |
| 4768 | int_type_a->is_signed, |
| 4769 | int_type_b->is_signed); |
| 4770 | goto end; |
| 4771 | } |
| 4772 | |
| 4773 | /* Base */ |
| 4774 | if (int_type_a->base != int_type_b->base) { |
| 4775 | BT_LOGV("Integer field types differ: different bases: " |
| 4776 | "ft-a-base=%s, ft-b-base=%s", |
| 4777 | bt_ctf_integer_base_string(int_type_a->base), |
| 4778 | bt_ctf_integer_base_string(int_type_b->base)); |
| 4779 | goto end; |
| 4780 | } |
| 4781 | |
| 4782 | /* Encoding */ |
| 4783 | if (int_type_a->encoding != int_type_b->encoding) { |
| 4784 | BT_LOGV("Integer field types differ: different encodings: " |
| 4785 | "ft-a-encoding=%s, ft-b-encoding=%s", |
| 4786 | bt_ctf_string_encoding_string(int_type_a->encoding), |
| 4787 | bt_ctf_string_encoding_string(int_type_b->encoding)); |
| 4788 | goto end; |
| 4789 | } |
| 4790 | |
| 4791 | /* Mapped clock */ |
| 4792 | if (int_type_a->mapped_clock != int_type_b->mapped_clock) { |
| 4793 | BT_LOGV("Integer field types differ: different mapped clock classes: " |
| 4794 | "ft-a-mapped-clock-class-addr=%p, " |
| 4795 | "ft-b-mapped-clock-class-addr=%p, " |
| 4796 | "ft-a-mapped-clock-class-name=\"%s\", " |
| 4797 | "ft-b-mapped-clock-class-name=\"%s\"", |
| 4798 | int_type_a->mapped_clock, int_type_b->mapped_clock, |
| 4799 | int_type_a->mapped_clock ? bt_ctf_clock_class_get_name(int_type_a->mapped_clock) : "", |
| 4800 | int_type_b->mapped_clock ? bt_ctf_clock_class_get_name(int_type_b->mapped_clock) : ""); |
| 4801 | goto end; |
| 4802 | } |
| 4803 | |
| 4804 | /* Equal */ |
| 4805 | ret = 0; |
| 4806 | |
| 4807 | end: |
| 4808 | return ret; |
| 4809 | } |
| 4810 | |
| 4811 | static |
| 4812 | int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *type_a, |
| 4813 | struct bt_ctf_field_type *type_b) |
| 4814 | { |
| 4815 | int ret = 1; |
| 4816 | struct bt_ctf_field_type_floating_point *float_a; |
| 4817 | struct bt_ctf_field_type_floating_point *float_b; |
| 4818 | |
| 4819 | float_a = container_of(type_a, |
| 4820 | struct bt_ctf_field_type_floating_point, parent); |
| 4821 | float_b = container_of(type_b, |
| 4822 | struct bt_ctf_field_type_floating_point, parent); |
| 4823 | |
| 4824 | /* Byte order */ |
| 4825 | if (float_a->user_byte_order != float_b->user_byte_order) { |
| 4826 | BT_LOGV("Floating point number field types differ: different byte orders: " |
| 4827 | "ft-a-bo=%s, ft-b-bo=%s", |
| 4828 | bt_ctf_byte_order_string(float_a->user_byte_order), |
| 4829 | bt_ctf_byte_order_string(float_b->user_byte_order)); |
| 4830 | goto end; |
| 4831 | } |
| 4832 | |
| 4833 | /* Exponent length */ |
| 4834 | if (float_a->exp_dig != float_b->exp_dig) { |
| 4835 | BT_LOGV("Floating point number field types differ: different exponent sizes: " |
| 4836 | "ft-a-exp-size=%u, ft-b-exp-size=%u", |
| 4837 | float_a->exp_dig, float_b->exp_dig); |
| 4838 | goto end; |
| 4839 | } |
| 4840 | |
| 4841 | /* Mantissa length */ |
| 4842 | if (float_a->mant_dig != float_b->mant_dig) { |
| 4843 | BT_LOGV("Floating point number field types differ: different mantissa sizes: " |
| 4844 | "ft-a-mant-size=%u, ft-b-mant-size=%u", |
| 4845 | float_a->mant_dig, float_b->mant_dig); |
| 4846 | goto end; |
| 4847 | } |
| 4848 | |
| 4849 | /* Equal */ |
| 4850 | ret = 0; |
| 4851 | |
| 4852 | end: |
| 4853 | return ret; |
| 4854 | } |
| 4855 | |
| 4856 | static |
| 4857 | int compare_enumeration_mappings(struct enumeration_mapping *mapping_a, |
| 4858 | struct enumeration_mapping *mapping_b) |
| 4859 | { |
| 4860 | int ret = 1; |
| 4861 | |
| 4862 | /* Label */ |
| 4863 | if (mapping_a->string != mapping_b->string) { |
| 4864 | BT_LOGV("Enumeration field type mappings differ: different names: " |
| 4865 | "mapping-a-name=\"%s\", mapping-b-name=\"%s\"", |
| 4866 | g_quark_to_string(mapping_a->string), |
| 4867 | g_quark_to_string(mapping_b->string)); |
| 4868 | goto end; |
| 4869 | } |
| 4870 | |
| 4871 | /* Range start */ |
| 4872 | if (mapping_a->range_start._unsigned != |
| 4873 | mapping_b->range_start._unsigned) { |
| 4874 | BT_LOGV("Enumeration field type mappings differ: different starts of range: " |
| 4875 | "mapping-a-range-start-unsigned=%" PRIu64 ", " |
| 4876 | "mapping-b-range-start-unsigned=%" PRIu64, |
| 4877 | mapping_a->range_start._unsigned, |
| 4878 | mapping_b->range_start._unsigned); |
| 4879 | goto end; |
| 4880 | } |
| 4881 | |
| 4882 | /* Range end */ |
| 4883 | if (mapping_a->range_end._unsigned != |
| 4884 | mapping_b->range_end._unsigned) { |
| 4885 | BT_LOGV("Enumeration field type mappings differ: different ends of range: " |
| 4886 | "mapping-a-range-end-unsigned=%" PRIu64 ", " |
| 4887 | "mapping-b-range-end-unsigned=%" PRIu64, |
| 4888 | mapping_a->range_end._unsigned, |
| 4889 | mapping_b->range_end._unsigned); |
| 4890 | goto end; |
| 4891 | } |
| 4892 | |
| 4893 | /* Equal */ |
| 4894 | ret = 0; |
| 4895 | |
| 4896 | end: |
| 4897 | return ret; |
| 4898 | } |
| 4899 | |
| 4900 | static |
| 4901 | int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *type_a, |
| 4902 | struct bt_ctf_field_type *type_b) |
| 4903 | { |
| 4904 | int ret = 1; |
| 4905 | int i; |
| 4906 | struct bt_ctf_field_type_enumeration *enum_a; |
| 4907 | struct bt_ctf_field_type_enumeration *enum_b; |
| 4908 | |
| 4909 | enum_a = container_of(type_a, |
| 4910 | struct bt_ctf_field_type_enumeration, parent); |
| 4911 | enum_b = container_of(type_b, |
| 4912 | struct bt_ctf_field_type_enumeration, parent); |
| 4913 | |
| 4914 | /* Container field type */ |
| 4915 | ret = bt_ctf_field_type_compare(enum_a->container, enum_b->container); |
| 4916 | if (ret) { |
| 4917 | BT_LOGV("Enumeration field types differ: different container field types: " |
| 4918 | "ft-a-container-ft-addr=%p, ft-b-container-ft-addr=%p", |
| 4919 | enum_a->container, enum_b->container); |
| 4920 | goto end; |
| 4921 | } |
| 4922 | |
| 4923 | ret = 1; |
| 4924 | |
| 4925 | /* Entries */ |
| 4926 | if (enum_a->entries->len != enum_b->entries->len) { |
| 4927 | goto end; |
| 4928 | } |
| 4929 | |
| 4930 | for (i = 0; i < enum_a->entries->len; ++i) { |
| 4931 | struct enumeration_mapping *mapping_a = |
| 4932 | g_ptr_array_index(enum_a->entries, i); |
| 4933 | struct enumeration_mapping *mapping_b = |
| 4934 | g_ptr_array_index(enum_b->entries, i); |
| 4935 | |
| 4936 | if (compare_enumeration_mappings(mapping_a, mapping_b)) { |
| 4937 | BT_LOGV("Enumeration field types differ: different mappings: " |
| 4938 | "ft-a-mapping-addr=%p, ft-b-mapping-addr=%p, " |
| 4939 | "ft-a-mapping-name=\"%s\", ft-b-mapping-name=\"%s\"", |
| 4940 | mapping_a, mapping_b, |
| 4941 | g_quark_to_string(mapping_a->string), |
| 4942 | g_quark_to_string(mapping_b->string)); |
| 4943 | goto end; |
| 4944 | } |
| 4945 | } |
| 4946 | |
| 4947 | /* Equal */ |
| 4948 | ret = 0; |
| 4949 | |
| 4950 | end: |
| 4951 | return ret; |
| 4952 | } |
| 4953 | |
| 4954 | static |
| 4955 | int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *type_a, |
| 4956 | struct bt_ctf_field_type *type_b) |
| 4957 | { |
| 4958 | int ret = 1; |
| 4959 | struct bt_ctf_field_type_string *string_a; |
| 4960 | struct bt_ctf_field_type_string *string_b; |
| 4961 | |
| 4962 | string_a = container_of(type_a, |
| 4963 | struct bt_ctf_field_type_string, parent); |
| 4964 | string_b = container_of(type_b, |
| 4965 | struct bt_ctf_field_type_string, parent); |
| 4966 | |
| 4967 | /* Encoding */ |
| 4968 | if (string_a->encoding != string_b->encoding) { |
| 4969 | BT_LOGV("String field types differ: different encodings: " |
| 4970 | "ft-a-encoding=%s, ft-b-encoding=%s", |
| 4971 | bt_ctf_string_encoding_string(string_a->encoding), |
| 4972 | bt_ctf_string_encoding_string(string_b->encoding)); |
| 4973 | goto end; |
| 4974 | } |
| 4975 | |
| 4976 | /* Equal */ |
| 4977 | ret = 0; |
| 4978 | |
| 4979 | end: |
| 4980 | return ret; |
| 4981 | } |
| 4982 | |
| 4983 | static |
| 4984 | int compare_structure_fields(struct structure_field *field_a, |
| 4985 | struct structure_field *field_b) |
| 4986 | { |
| 4987 | int ret = 1; |
| 4988 | |
| 4989 | /* Label */ |
| 4990 | if (field_a->name != field_b->name) { |
| 4991 | BT_LOGV("Structure/variant field type fields differ: different names: " |
| 4992 | "field-a-name=%s, field-b-name=%s", |
| 4993 | g_quark_to_string(field_a->name), |
| 4994 | g_quark_to_string(field_b->name)); |
| 4995 | goto end; |
| 4996 | } |
| 4997 | |
| 4998 | /* Type */ |
| 4999 | ret = bt_ctf_field_type_compare(field_a->type, field_b->type); |
| 5000 | if (ret == 1) { |
| 5001 | BT_LOGV("Structure/variant field type fields differ: different field types: " |
| 5002 | "field-name=\"%s\", field-a-ft-addr=%p, field-b-ft-addr=%p", |
| 5003 | g_quark_to_string(field_a->name), |
| 5004 | field_a->type, field_b->type); |
| 5005 | } |
| 5006 | |
| 5007 | end: |
| 5008 | return ret; |
| 5009 | } |
| 5010 | |
| 5011 | static |
| 5012 | int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *type_a, |
| 5013 | struct bt_ctf_field_type *type_b) |
| 5014 | { |
| 5015 | int ret = 1; |
| 5016 | int i; |
| 5017 | struct bt_ctf_field_type_structure *struct_a; |
| 5018 | struct bt_ctf_field_type_structure *struct_b; |
| 5019 | |
| 5020 | struct_a = container_of(type_a, |
| 5021 | struct bt_ctf_field_type_structure, parent); |
| 5022 | struct_b = container_of(type_b, |
| 5023 | struct bt_ctf_field_type_structure, parent); |
| 5024 | |
| 5025 | /* Alignment */ |
| 5026 | if (bt_ctf_field_type_get_alignment(type_a) != |
| 5027 | bt_ctf_field_type_get_alignment(type_b)) { |
| 5028 | BT_LOGV("Structure field types differ: different alignments: " |
| 5029 | "ft-a-align=%u, ft-b-align=%u", |
| 5030 | bt_ctf_field_type_get_alignment(type_a), |
| 5031 | bt_ctf_field_type_get_alignment(type_b)); |
| 5032 | goto end; |
| 5033 | } |
| 5034 | |
| 5035 | /* Fields */ |
| 5036 | if (struct_a->fields->len != struct_b->fields->len) { |
| 5037 | BT_LOGV("Structure field types differ: different field counts: " |
| 5038 | "ft-a-field-count=%u, ft-b-field-count=%u", |
| 5039 | struct_a->fields->len, struct_b->fields->len); |
| 5040 | goto end; |
| 5041 | } |
| 5042 | |
| 5043 | for (i = 0; i < struct_a->fields->len; ++i) { |
| 5044 | struct structure_field *field_a = |
| 5045 | g_ptr_array_index(struct_a->fields, i); |
| 5046 | struct structure_field *field_b = |
| 5047 | g_ptr_array_index(struct_b->fields, i); |
| 5048 | |
| 5049 | ret = compare_structure_fields(field_a, field_b); |
| 5050 | if (ret) { |
| 5051 | /* compare_structure_fields() logs what differs */ |
| 5052 | BT_LOGV_STR("Structure field types differ: different fields."); |
| 5053 | goto end; |
| 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | /* Equal */ |
| 5058 | ret = 0; |
| 5059 | |
| 5060 | end: |
| 5061 | return ret; |
| 5062 | } |
| 5063 | |
| 5064 | static |
| 5065 | int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *type_a, |
| 5066 | struct bt_ctf_field_type *type_b) |
| 5067 | { |
| 5068 | int ret = 1; |
| 5069 | int i; |
| 5070 | struct bt_ctf_field_type_variant *variant_a; |
| 5071 | struct bt_ctf_field_type_variant *variant_b; |
| 5072 | |
| 5073 | variant_a = container_of(type_a, |
| 5074 | struct bt_ctf_field_type_variant, parent); |
| 5075 | variant_b = container_of(type_b, |
| 5076 | struct bt_ctf_field_type_variant, parent); |
| 5077 | |
| 5078 | /* Tag name */ |
| 5079 | if (strcmp(variant_a->tag_name->str, variant_b->tag_name->str)) { |
| 5080 | BT_LOGV("Variant field types differ: different tag field names: " |
| 5081 | "ft-a-tag-field-name=\"%s\", ft-b-tag-field-name=\"%s\"", |
| 5082 | variant_a->tag_name->str, variant_b->tag_name->str); |
| 5083 | goto end; |
| 5084 | } |
| 5085 | |
| 5086 | /* Tag type */ |
| 5087 | ret = bt_ctf_field_type_compare( |
| 5088 | (struct bt_ctf_field_type *) variant_a->tag, |
| 5089 | (struct bt_ctf_field_type *) variant_b->tag); |
| 5090 | if (ret) { |
| 5091 | BT_LOGV("Variant field types differ: different tag field types: " |
| 5092 | "ft-a-tag-ft-addr=%p, ft-b-tag-ft-addr=%p", |
| 5093 | variant_a->tag, variant_b->tag); |
| 5094 | goto end; |
| 5095 | } |
| 5096 | |
| 5097 | ret = 1; |
| 5098 | |
| 5099 | /* Fields */ |
| 5100 | if (variant_a->fields->len != variant_b->fields->len) { |
| 5101 | BT_LOGV("Structure field types differ: different field counts: " |
| 5102 | "ft-a-field-count=%u, ft-b-field-count=%u", |
| 5103 | variant_a->fields->len, variant_b->fields->len); |
| 5104 | goto end; |
| 5105 | } |
| 5106 | |
| 5107 | for (i = 0; i < variant_a->fields->len; ++i) { |
| 5108 | struct structure_field *field_a = |
| 5109 | g_ptr_array_index(variant_a->fields, i); |
| 5110 | struct structure_field *field_b = |
| 5111 | g_ptr_array_index(variant_b->fields, i); |
| 5112 | |
| 5113 | ret = compare_structure_fields(field_a, field_b); |
| 5114 | if (ret) { |
| 5115 | /* compare_structure_fields() logs what differs */ |
| 5116 | BT_LOGV_STR("Variant field types differ: different fields."); |
| 5117 | goto end; |
| 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | /* Equal */ |
| 5122 | ret = 0; |
| 5123 | |
| 5124 | end: |
| 5125 | return ret; |
| 5126 | } |
| 5127 | |
| 5128 | static |
| 5129 | int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *type_a, |
| 5130 | struct bt_ctf_field_type *type_b) |
| 5131 | { |
| 5132 | int ret = 1; |
| 5133 | struct bt_ctf_field_type_array *array_a; |
| 5134 | struct bt_ctf_field_type_array *array_b; |
| 5135 | |
| 5136 | array_a = container_of(type_a, |
| 5137 | struct bt_ctf_field_type_array, parent); |
| 5138 | array_b = container_of(type_b, |
| 5139 | struct bt_ctf_field_type_array, parent); |
| 5140 | |
| 5141 | /* Length */ |
| 5142 | if (array_a->length != array_b->length) { |
| 5143 | BT_LOGV("Structure field types differ: different lengths: " |
| 5144 | "ft-a-length=%u, ft-b-length=%u", |
| 5145 | array_a->length, array_b->length); |
| 5146 | goto end; |
| 5147 | } |
| 5148 | |
| 5149 | /* Element type */ |
| 5150 | ret = bt_ctf_field_type_compare(array_a->element_type, |
| 5151 | array_b->element_type); |
| 5152 | if (ret == 1) { |
| 5153 | BT_LOGV("Array field types differ: different element field types: " |
| 5154 | "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p", |
| 5155 | array_a->element_type, array_b->element_type); |
| 5156 | } |
| 5157 | |
| 5158 | end: |
| 5159 | return ret; |
| 5160 | } |
| 5161 | |
| 5162 | static |
| 5163 | int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *type_a, |
| 5164 | struct bt_ctf_field_type *type_b) |
| 5165 | { |
| 5166 | int ret = -1; |
| 5167 | struct bt_ctf_field_type_sequence *sequence_a; |
| 5168 | struct bt_ctf_field_type_sequence *sequence_b; |
| 5169 | |
| 5170 | sequence_a = container_of(type_a, |
| 5171 | struct bt_ctf_field_type_sequence, parent); |
| 5172 | sequence_b = container_of(type_b, |
| 5173 | struct bt_ctf_field_type_sequence, parent); |
| 5174 | |
| 5175 | /* Length name */ |
| 5176 | if (strcmp(sequence_a->length_field_name->str, |
| 5177 | sequence_b->length_field_name->str)) { |
| 5178 | BT_LOGV("Sequence field types differ: different length field names: " |
| 5179 | "ft-a-length-field-name=\"%s\", " |
| 5180 | "ft-b-length-field-name=\"%s\"", |
| 5181 | sequence_a->length_field_name->str, |
| 5182 | sequence_b->length_field_name->str); |
| 5183 | goto end; |
| 5184 | } |
| 5185 | |
| 5186 | /* Element type */ |
| 5187 | ret = bt_ctf_field_type_compare(sequence_a->element_type, |
| 5188 | sequence_b->element_type); |
| 5189 | if (ret == 1) { |
| 5190 | BT_LOGV("Sequence field types differ: different element field types: " |
| 5191 | "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p", |
| 5192 | sequence_a->element_type, sequence_b->element_type); |
| 5193 | } |
| 5194 | |
| 5195 | end: |
| 5196 | return ret; |
| 5197 | } |
| 5198 | |
| 5199 | int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a, |
| 5200 | struct bt_ctf_field_type *type_b) |
| 5201 | { |
| 5202 | int ret = 1; |
| 5203 | |
| 5204 | if (type_a == type_b) { |
| 5205 | /* Same reference: equal (even if both are NULL) */ |
| 5206 | ret = 0; |
| 5207 | goto end; |
| 5208 | } |
| 5209 | |
| 5210 | if (!type_a) { |
| 5211 | BT_LOGW_STR("Invalid parameter: field type A is NULL."); |
| 5212 | ret = -1; |
| 5213 | goto end; |
| 5214 | } |
| 5215 | |
| 5216 | if (!type_b) { |
| 5217 | BT_LOGW_STR("Invalid parameter: field type B is NULL."); |
| 5218 | ret = -1; |
| 5219 | goto end; |
| 5220 | } |
| 5221 | |
| 5222 | if (type_a->id != type_b->id) { |
| 5223 | /* Different type IDs */ |
| 5224 | BT_LOGV("Field types differ: different IDs: " |
| 5225 | "ft-a-addr=%p, ft-b-addr=%p, " |
| 5226 | "ft-a-id=%s, ft-b-id=%s", |
| 5227 | type_a, type_b, |
| 5228 | bt_ctf_field_type_id_string(type_a->id), |
| 5229 | bt_ctf_field_type_id_string(type_b->id)); |
| 5230 | goto end; |
| 5231 | } |
| 5232 | |
| 5233 | if (type_a->id == BT_CTF_FIELD_TYPE_ID_UNKNOWN) { |
| 5234 | /* Both have unknown type IDs */ |
| 5235 | BT_LOGW_STR("Invalid parameter: field type IDs are unknown."); |
| 5236 | goto end; |
| 5237 | } |
| 5238 | |
| 5239 | ret = type_compare_funcs[type_a->id](type_a, type_b); |
| 5240 | if (ret == 1) { |
| 5241 | BT_LOGV("Field types differ: ft-a-addr=%p, ft-b-addr=%p", |
| 5242 | type_a, type_b); |
| 5243 | } |
| 5244 | |
| 5245 | end: |
| 5246 | return ret; |
| 5247 | } |
| 5248 | |
| 5249 | BT_HIDDEN |
| 5250 | int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *field_type) |
| 5251 | { |
| 5252 | int64_t field_count = -1; |
| 5253 | enum bt_ctf_field_type_id type_id = bt_ctf_field_type_get_type_id(field_type); |
| 5254 | |
| 5255 | switch (type_id) { |
| 5256 | case CTF_TYPE_STRUCT: |
| 5257 | field_count = |
| 5258 | bt_ctf_field_type_structure_get_field_count(field_type); |
| 5259 | break; |
| 5260 | case CTF_TYPE_VARIANT: |
| 5261 | field_count = |
| 5262 | bt_ctf_field_type_variant_get_field_count(field_type); |
| 5263 | break; |
| 5264 | case CTF_TYPE_ARRAY: |
| 5265 | case CTF_TYPE_SEQUENCE: |
| 5266 | /* |
| 5267 | * Array and sequence types always contain a single member |
| 5268 | * (the element type). |
| 5269 | */ |
| 5270 | field_count = 1; |
| 5271 | break; |
| 5272 | default: |
| 5273 | break; |
| 5274 | } |
| 5275 | |
| 5276 | return field_count; |
| 5277 | } |
| 5278 | |
| 5279 | BT_HIDDEN |
| 5280 | struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index( |
| 5281 | struct bt_ctf_field_type *field_type, int index) |
| 5282 | { |
| 5283 | struct bt_ctf_field_type *field = NULL; |
| 5284 | enum bt_ctf_field_type_id type_id = bt_ctf_field_type_get_type_id(field_type); |
| 5285 | |
| 5286 | switch (type_id) { |
| 5287 | case CTF_TYPE_STRUCT: |
| 5288 | bt_ctf_field_type_structure_get_field_by_index(field_type, |
| 5289 | NULL, &field, index); |
| 5290 | break; |
| 5291 | case CTF_TYPE_VARIANT: |
| 5292 | { |
| 5293 | int ret = bt_ctf_field_type_variant_get_field_by_index( |
| 5294 | field_type, NULL, &field, index); |
| 5295 | if (ret) { |
| 5296 | field = NULL; |
| 5297 | goto end; |
| 5298 | } |
| 5299 | break; |
| 5300 | } |
| 5301 | case CTF_TYPE_ARRAY: |
| 5302 | field = bt_ctf_field_type_array_get_element_type(field_type); |
| 5303 | break; |
| 5304 | case CTF_TYPE_SEQUENCE: |
| 5305 | field = bt_ctf_field_type_sequence_get_element_type(field_type); |
| 5306 | break; |
| 5307 | default: |
| 5308 | break; |
| 5309 | } |
| 5310 | end: |
| 5311 | return field; |
| 5312 | } |
| 5313 | |
| 5314 | BT_HIDDEN |
| 5315 | int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *field_type, |
| 5316 | const char *name) |
| 5317 | { |
| 5318 | int field_index = -1; |
| 5319 | enum bt_ctf_field_type_id type_id = bt_ctf_field_type_get_type_id(field_type); |
| 5320 | |
| 5321 | switch (type_id) { |
| 5322 | case CTF_TYPE_STRUCT: |
| 5323 | field_index = bt_ctf_field_type_structure_get_field_name_index( |
| 5324 | field_type, name); |
| 5325 | break; |
| 5326 | case CTF_TYPE_VARIANT: |
| 5327 | field_index = bt_ctf_field_type_variant_get_field_name_index( |
| 5328 | field_type, name); |
| 5329 | break; |
| 5330 | default: |
| 5331 | break; |
| 5332 | } |
| 5333 | |
| 5334 | return field_index; |
| 5335 | } |
| 5336 | |
| 5337 | struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path( |
| 5338 | struct bt_ctf_field_type *type) |
| 5339 | { |
| 5340 | struct bt_ctf_field_path *field_path = NULL; |
| 5341 | struct bt_ctf_field_type_variant *variant; |
| 5342 | |
| 5343 | if (!type) { |
| 5344 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 5345 | goto end; |
| 5346 | } |
| 5347 | |
| 5348 | if (!bt_ctf_field_type_is_variant(type)) { |
| 5349 | BT_LOGW("Invalid parameter: field type is not a variant field type: " |
| 5350 | "addr=%p, ft-id=%s", type, |
| 5351 | bt_ctf_field_type_id_string(type->id)); |
| 5352 | goto end; |
| 5353 | } |
| 5354 | |
| 5355 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 5356 | parent); |
| 5357 | field_path = bt_get(variant->tag_field_path); |
| 5358 | end: |
| 5359 | return field_path; |
| 5360 | } |
| 5361 | |
| 5362 | struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path( |
| 5363 | struct bt_ctf_field_type *type) |
| 5364 | { |
| 5365 | struct bt_ctf_field_path *field_path = NULL; |
| 5366 | struct bt_ctf_field_type_sequence *sequence; |
| 5367 | |
| 5368 | if (!type) { |
| 5369 | BT_LOGW_STR("Invalid parameter: field type is NULL."); |
| 5370 | goto end; |
| 5371 | } |
| 5372 | |
| 5373 | if (!bt_ctf_field_type_is_sequence(type)) { |
| 5374 | BT_LOGW("Invalid parameter: field type is not a sequence field type: " |
| 5375 | "addr=%p, ft-id=%s", type, |
| 5376 | bt_ctf_field_type_id_string(type->id)); |
| 5377 | goto end; |
| 5378 | } |
| 5379 | |
| 5380 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 5381 | parent); |
| 5382 | field_path = bt_get(sequence->length_field_path); |
| 5383 | end: |
| 5384 | return field_path; |
| 5385 | } |