| 1 | /* |
| 2 | * event-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 | #include <babeltrace/ctf-writer/event-types.h> |
| 30 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 31 | #include <babeltrace/ctf-ir/utils.h> |
| 32 | #include <babeltrace/ref.h> |
| 33 | #include <babeltrace/ctf-ir/clock.h> |
| 34 | #include <babeltrace/ctf-writer/writer-internal.h> |
| 35 | #include <babeltrace/object-internal.h> |
| 36 | #include <babeltrace/ref.h> |
| 37 | #include <babeltrace/compiler.h> |
| 38 | #include <babeltrace/endian.h> |
| 39 | #include <float.h> |
| 40 | #include <inttypes.h> |
| 41 | #include <stdlib.h> |
| 42 | |
| 43 | struct range_overlap_query { |
| 44 | union { |
| 45 | uint64_t _unsigned; |
| 46 | int64_t _signed; |
| 47 | } range_start; |
| 48 | |
| 49 | union { |
| 50 | uint64_t _unsigned; |
| 51 | int64_t _signed; |
| 52 | } range_end; |
| 53 | int overlaps; |
| 54 | GQuark mapping_name; |
| 55 | }; |
| 56 | |
| 57 | static |
| 58 | void bt_ctf_field_type_destroy(struct bt_object *); |
| 59 | static |
| 60 | void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *); |
| 61 | static |
| 62 | void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *); |
| 63 | static |
| 64 | void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *); |
| 65 | static |
| 66 | void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *); |
| 67 | static |
| 68 | void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *); |
| 69 | static |
| 70 | void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *); |
| 71 | static |
| 72 | void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *); |
| 73 | static |
| 74 | void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *); |
| 75 | |
| 76 | static |
| 77 | void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = { |
| 78 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy, |
| 79 | [CTF_TYPE_ENUM] = |
| 80 | bt_ctf_field_type_enumeration_destroy, |
| 81 | [CTF_TYPE_FLOAT] = |
| 82 | bt_ctf_field_type_floating_point_destroy, |
| 83 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy, |
| 84 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy, |
| 85 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy, |
| 86 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy, |
| 87 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_destroy, |
| 88 | }; |
| 89 | |
| 90 | static |
| 91 | void generic_field_type_freeze(struct bt_ctf_field_type *); |
| 92 | static |
| 93 | void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *); |
| 94 | static |
| 95 | void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *); |
| 96 | static |
| 97 | void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *); |
| 98 | static |
| 99 | void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *); |
| 100 | static |
| 101 | void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *); |
| 102 | |
| 103 | static |
| 104 | type_freeze_func const type_freeze_funcs[] = { |
| 105 | [CTF_TYPE_INTEGER] = generic_field_type_freeze, |
| 106 | [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze, |
| 107 | [CTF_TYPE_FLOAT] = generic_field_type_freeze, |
| 108 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze, |
| 109 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze, |
| 110 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze, |
| 111 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze, |
| 112 | [CTF_TYPE_STRING] = generic_field_type_freeze, |
| 113 | }; |
| 114 | |
| 115 | static |
| 116 | int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *, |
| 117 | struct metadata_context *); |
| 118 | static |
| 119 | int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *, |
| 120 | struct metadata_context *); |
| 121 | static |
| 122 | int bt_ctf_field_type_floating_point_serialize( |
| 123 | struct bt_ctf_field_type *, struct metadata_context *); |
| 124 | static |
| 125 | int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *, |
| 126 | struct metadata_context *); |
| 127 | static |
| 128 | int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *, |
| 129 | struct metadata_context *); |
| 130 | static |
| 131 | int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *, |
| 132 | struct metadata_context *); |
| 133 | static |
| 134 | int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *, |
| 135 | struct metadata_context *); |
| 136 | static |
| 137 | int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *, |
| 138 | struct metadata_context *); |
| 139 | |
| 140 | static |
| 141 | type_serialize_func const type_serialize_funcs[] = { |
| 142 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize, |
| 143 | [CTF_TYPE_ENUM] = |
| 144 | bt_ctf_field_type_enumeration_serialize, |
| 145 | [CTF_TYPE_FLOAT] = |
| 146 | bt_ctf_field_type_floating_point_serialize, |
| 147 | [CTF_TYPE_STRUCT] = |
| 148 | bt_ctf_field_type_structure_serialize, |
| 149 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize, |
| 150 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize, |
| 151 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize, |
| 152 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_serialize, |
| 153 | }; |
| 154 | |
| 155 | static |
| 156 | void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *, |
| 157 | int byte_order, int set_native); |
| 158 | static |
| 159 | void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *, |
| 160 | int byte_order, int set_native); |
| 161 | static |
| 162 | void bt_ctf_field_type_floating_point_set_byte_order( |
| 163 | struct bt_ctf_field_type *, int byte_order, int set_native); |
| 164 | static |
| 165 | void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *, |
| 166 | int byte_order, int set_native); |
| 167 | static |
| 168 | void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *, |
| 169 | int byte_order, int set_native); |
| 170 | static |
| 171 | void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *, |
| 172 | int byte_order, int set_native); |
| 173 | static |
| 174 | void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *, |
| 175 | int byte_order, int set_native); |
| 176 | |
| 177 | /* The set_native flag only set the byte order if it is set to native */ |
| 178 | static |
| 179 | void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *, |
| 180 | int byte_order, int set_native) = { |
| 181 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_set_byte_order, |
| 182 | [CTF_TYPE_ENUM] = |
| 183 | bt_ctf_field_type_enumeration_set_byte_order, |
| 184 | [CTF_TYPE_FLOAT] = |
| 185 | bt_ctf_field_type_floating_point_set_byte_order, |
| 186 | [CTF_TYPE_STRUCT] = |
| 187 | bt_ctf_field_type_structure_set_byte_order, |
| 188 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_set_byte_order, |
| 189 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_set_byte_order, |
| 190 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order, |
| 191 | [CTF_TYPE_STRING] = NULL, |
| 192 | }; |
| 193 | |
| 194 | static |
| 195 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( |
| 196 | struct bt_ctf_field_type *); |
| 197 | static |
| 198 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( |
| 199 | struct bt_ctf_field_type *); |
| 200 | static |
| 201 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( |
| 202 | struct bt_ctf_field_type *); |
| 203 | static |
| 204 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( |
| 205 | struct bt_ctf_field_type *); |
| 206 | static |
| 207 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( |
| 208 | struct bt_ctf_field_type *); |
| 209 | static |
| 210 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy( |
| 211 | struct bt_ctf_field_type *); |
| 212 | static |
| 213 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( |
| 214 | struct bt_ctf_field_type *); |
| 215 | static |
| 216 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( |
| 217 | struct bt_ctf_field_type *); |
| 218 | |
| 219 | static |
| 220 | struct bt_ctf_field_type *(* const type_copy_funcs[])( |
| 221 | struct bt_ctf_field_type *) = { |
| 222 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_copy, |
| 223 | [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_copy, |
| 224 | [CTF_TYPE_FLOAT] = bt_ctf_field_type_floating_point_copy, |
| 225 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_copy, |
| 226 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_copy, |
| 227 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_copy, |
| 228 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_copy, |
| 229 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_copy, |
| 230 | }; |
| 231 | |
| 232 | static |
| 233 | int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *, |
| 234 | struct bt_ctf_field_type *); |
| 235 | static |
| 236 | int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *, |
| 237 | struct bt_ctf_field_type *); |
| 238 | static |
| 239 | int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *, |
| 240 | struct bt_ctf_field_type *); |
| 241 | static |
| 242 | int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *, |
| 243 | struct bt_ctf_field_type *); |
| 244 | static |
| 245 | int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *, |
| 246 | struct bt_ctf_field_type *); |
| 247 | static |
| 248 | int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *, |
| 249 | struct bt_ctf_field_type *); |
| 250 | static |
| 251 | int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *, |
| 252 | struct bt_ctf_field_type *); |
| 253 | static |
| 254 | int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *, |
| 255 | struct bt_ctf_field_type *); |
| 256 | |
| 257 | static |
| 258 | int (* const type_compare_funcs[])(struct bt_ctf_field_type *, |
| 259 | struct bt_ctf_field_type *) = { |
| 260 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_compare, |
| 261 | [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_compare, |
| 262 | [CTF_TYPE_FLOAT] = bt_ctf_field_type_floating_point_compare, |
| 263 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_compare, |
| 264 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_compare, |
| 265 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_compare, |
| 266 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_compare, |
| 267 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_compare, |
| 268 | }; |
| 269 | |
| 270 | static |
| 271 | void destroy_enumeration_mapping(struct enumeration_mapping *mapping) |
| 272 | { |
| 273 | g_free(mapping); |
| 274 | } |
| 275 | |
| 276 | static |
| 277 | void destroy_structure_field(struct structure_field *field) |
| 278 | { |
| 279 | bt_put(field->type); |
| 280 | g_free(field); |
| 281 | } |
| 282 | |
| 283 | static |
| 284 | void check_ranges_overlap(gpointer element, gpointer query) |
| 285 | { |
| 286 | struct enumeration_mapping *mapping = element; |
| 287 | struct range_overlap_query *overlap_query = query; |
| 288 | |
| 289 | if (mapping->range_start._signed <= overlap_query->range_end._signed |
| 290 | && overlap_query->range_start._signed <= |
| 291 | mapping->range_end._signed) { |
| 292 | overlap_query->overlaps = 1; |
| 293 | overlap_query->mapping_name = mapping->string; |
| 294 | } |
| 295 | |
| 296 | overlap_query->overlaps |= |
| 297 | mapping->string == overlap_query->mapping_name; |
| 298 | } |
| 299 | |
| 300 | static |
| 301 | void check_ranges_overlap_unsigned(gpointer element, gpointer query) |
| 302 | { |
| 303 | struct enumeration_mapping *mapping = element; |
| 304 | struct range_overlap_query *overlap_query = query; |
| 305 | |
| 306 | if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned |
| 307 | && overlap_query->range_start._unsigned <= |
| 308 | mapping->range_end._unsigned) { |
| 309 | overlap_query->overlaps = 1; |
| 310 | overlap_query->mapping_name = mapping->string; |
| 311 | } |
| 312 | |
| 313 | overlap_query->overlaps |= |
| 314 | mapping->string == overlap_query->mapping_name; |
| 315 | } |
| 316 | |
| 317 | static |
| 318 | gint compare_enumeration_mappings_signed(struct enumeration_mapping **a, |
| 319 | struct enumeration_mapping **b) |
| 320 | { |
| 321 | return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1; |
| 322 | } |
| 323 | |
| 324 | static |
| 325 | gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a, |
| 326 | struct enumeration_mapping **b) |
| 327 | { |
| 328 | return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1; |
| 329 | } |
| 330 | |
| 331 | static |
| 332 | void bt_ctf_field_type_init(struct bt_ctf_field_type *type, int init_bo) |
| 333 | { |
| 334 | enum ctf_type_id type_id = type->declaration->id; |
| 335 | |
| 336 | assert(type && (type_id > CTF_TYPE_UNKNOWN) && |
| 337 | (type_id < NR_CTF_TYPES)); |
| 338 | |
| 339 | bt_object_init(type, bt_ctf_field_type_destroy); |
| 340 | type->freeze = type_freeze_funcs[type_id]; |
| 341 | type->serialize = type_serialize_funcs[type_id]; |
| 342 | |
| 343 | if (init_bo) { |
| 344 | int ret = bt_ctf_field_type_set_byte_order(type, |
| 345 | BT_CTF_BYTE_ORDER_NATIVE); |
| 346 | assert(!ret); |
| 347 | } |
| 348 | |
| 349 | type->declaration->alignment = 1; |
| 350 | } |
| 351 | |
| 352 | static |
| 353 | int add_structure_field(GPtrArray *fields, |
| 354 | GHashTable *field_name_to_index, |
| 355 | struct bt_ctf_field_type *field_type, |
| 356 | const char *field_name) |
| 357 | { |
| 358 | int ret = 0; |
| 359 | GQuark name_quark = g_quark_from_string(field_name); |
| 360 | struct structure_field *field; |
| 361 | |
| 362 | /* Make sure structure does not contain a field of the same name */ |
| 363 | if (g_hash_table_lookup_extended(field_name_to_index, |
| 364 | GUINT_TO_POINTER(name_quark), NULL, NULL)) { |
| 365 | ret = -1; |
| 366 | goto end; |
| 367 | } |
| 368 | |
| 369 | field = g_new0(struct structure_field, 1); |
| 370 | if (!field) { |
| 371 | ret = -1; |
| 372 | goto end; |
| 373 | } |
| 374 | |
| 375 | bt_get(field_type); |
| 376 | field->name = name_quark; |
| 377 | field->type = field_type; |
| 378 | g_hash_table_insert(field_name_to_index, |
| 379 | (gpointer) (unsigned long) name_quark, |
| 380 | (gpointer) (unsigned long) fields->len); |
| 381 | g_ptr_array_add(fields, field); |
| 382 | end: |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static |
| 387 | void bt_ctf_field_type_destroy(struct bt_object *obj) |
| 388 | { |
| 389 | struct bt_ctf_field_type *type; |
| 390 | enum ctf_type_id type_id; |
| 391 | |
| 392 | type = container_of(obj, struct bt_ctf_field_type, base); |
| 393 | type_id = type->declaration->id; |
| 394 | if (type_id <= CTF_TYPE_UNKNOWN || |
| 395 | type_id >= NR_CTF_TYPES) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | type_destroy_funcs[type_id](type); |
| 400 | } |
| 401 | |
| 402 | BT_HIDDEN |
| 403 | int bt_ctf_field_type_validate(struct bt_ctf_field_type *type) |
| 404 | { |
| 405 | int ret = 0; |
| 406 | |
| 407 | if (!type) { |
| 408 | ret = -1; |
| 409 | goto end; |
| 410 | } |
| 411 | |
| 412 | switch (type->declaration->id) { |
| 413 | case CTF_TYPE_ENUM: |
| 414 | { |
| 415 | struct bt_ctf_field_type_enumeration *enumeration = |
| 416 | container_of(type, struct bt_ctf_field_type_enumeration, |
| 417 | parent); |
| 418 | |
| 419 | /* Ensure enum has entries */ |
| 420 | ret = enumeration->entries->len ? 0 : -1; |
| 421 | break; |
| 422 | } |
| 423 | case CTF_TYPE_SEQUENCE: |
| 424 | { |
| 425 | struct bt_ctf_field_type_sequence *sequence = |
| 426 | container_of(type, struct bt_ctf_field_type_sequence, |
| 427 | parent); |
| 428 | |
| 429 | /* length field name should be set at this point */ |
| 430 | ret = sequence->length_field_name->len ? 0 : -1; |
| 431 | break; |
| 432 | } |
| 433 | case CTF_TYPE_VARIANT: |
| 434 | { |
| 435 | struct bt_ctf_field_type_variant *variant = |
| 436 | container_of(type, struct bt_ctf_field_type_variant, |
| 437 | parent); |
| 438 | |
| 439 | if (variant->tag_name->len == 0 || !variant->tag) { |
| 440 | ret = -1; |
| 441 | } |
| 442 | break; |
| 443 | } |
| 444 | default: |
| 445 | break; |
| 446 | } |
| 447 | end: |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size) |
| 452 | { |
| 453 | struct bt_ctf_field_type_integer *integer = |
| 454 | g_new0(struct bt_ctf_field_type_integer, 1); |
| 455 | |
| 456 | if (!integer || size == 0 || size > 64) { |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | integer->parent.declaration = &integer->declaration.p; |
| 461 | integer->parent.declaration->id = CTF_TYPE_INTEGER; |
| 462 | integer->declaration.len = size; |
| 463 | integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL; |
| 464 | integer->declaration.encoding = CTF_STRING_NONE; |
| 465 | bt_ctf_field_type_init(&integer->parent, TRUE); |
| 466 | return &integer->parent; |
| 467 | } |
| 468 | |
| 469 | int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type) |
| 470 | { |
| 471 | int ret = 0; |
| 472 | struct bt_ctf_field_type_integer *integer; |
| 473 | |
| 474 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { |
| 475 | ret = -1; |
| 476 | goto end; |
| 477 | } |
| 478 | |
| 479 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 480 | ret = (int) integer->declaration.len; |
| 481 | end: |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type) |
| 486 | { |
| 487 | int ret = 0; |
| 488 | struct bt_ctf_field_type_integer *integer; |
| 489 | |
| 490 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { |
| 491 | ret = -1; |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 496 | ret = integer->declaration.signedness; |
| 497 | end: |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type, |
| 502 | int is_signed) |
| 503 | { |
| 504 | int ret = 0; |
| 505 | struct bt_ctf_field_type_integer *integer; |
| 506 | |
| 507 | if (!type || type->frozen || |
| 508 | type->declaration->id != CTF_TYPE_INTEGER) { |
| 509 | ret = -1; |
| 510 | goto end; |
| 511 | } |
| 512 | |
| 513 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 514 | integer->declaration.signedness = !!is_signed; |
| 515 | end: |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( |
| 520 | struct bt_ctf_field_type *type) |
| 521 | { |
| 522 | enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN; |
| 523 | struct bt_ctf_field_type_integer *integer; |
| 524 | |
| 525 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { |
| 526 | goto end; |
| 527 | } |
| 528 | |
| 529 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 530 | ret = integer->declaration.base; |
| 531 | end: |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type, |
| 536 | enum bt_ctf_integer_base base) |
| 537 | { |
| 538 | int ret = 0; |
| 539 | |
| 540 | if (!type || type->frozen || |
| 541 | type->declaration->id != CTF_TYPE_INTEGER) { |
| 542 | ret = -1; |
| 543 | goto end; |
| 544 | } |
| 545 | |
| 546 | switch (base) { |
| 547 | case BT_CTF_INTEGER_BASE_BINARY: |
| 548 | case BT_CTF_INTEGER_BASE_OCTAL: |
| 549 | case BT_CTF_INTEGER_BASE_DECIMAL: |
| 550 | case BT_CTF_INTEGER_BASE_HEXADECIMAL: |
| 551 | { |
| 552 | struct bt_ctf_field_type_integer *integer = container_of(type, |
| 553 | struct bt_ctf_field_type_integer, parent); |
| 554 | integer->declaration.base = base; |
| 555 | break; |
| 556 | } |
| 557 | default: |
| 558 | ret = -1; |
| 559 | } |
| 560 | end: |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | enum ctf_string_encoding bt_ctf_field_type_integer_get_encoding( |
| 565 | struct bt_ctf_field_type *type) |
| 566 | { |
| 567 | enum ctf_string_encoding ret = CTF_STRING_UNKNOWN; |
| 568 | struct bt_ctf_field_type_integer *integer; |
| 569 | |
| 570 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { |
| 571 | goto end; |
| 572 | } |
| 573 | |
| 574 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 575 | ret = integer->declaration.encoding; |
| 576 | end: |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type, |
| 581 | enum ctf_string_encoding encoding) |
| 582 | { |
| 583 | int ret = 0; |
| 584 | struct bt_ctf_field_type_integer *integer; |
| 585 | |
| 586 | if (!type || type->frozen || |
| 587 | (type->declaration->id != CTF_TYPE_INTEGER) || |
| 588 | (encoding < CTF_STRING_NONE) || |
| 589 | (encoding >= CTF_STRING_UNKNOWN)) { |
| 590 | ret = -1; |
| 591 | goto end; |
| 592 | } |
| 593 | |
| 594 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 595 | integer->declaration.encoding = encoding; |
| 596 | end: |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock( |
| 601 | struct bt_ctf_field_type *type) |
| 602 | { |
| 603 | struct bt_ctf_field_type_integer *integer; |
| 604 | struct bt_ctf_clock *clock = NULL; |
| 605 | |
| 606 | if (!type) { |
| 607 | goto end; |
| 608 | } |
| 609 | |
| 610 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 611 | clock = integer->mapped_clock; |
| 612 | bt_get(clock); |
| 613 | end: |
| 614 | return clock; |
| 615 | } |
| 616 | |
| 617 | int bt_ctf_field_type_integer_set_mapped_clock( |
| 618 | struct bt_ctf_field_type *type, |
| 619 | struct bt_ctf_clock *clock) |
| 620 | { |
| 621 | struct bt_ctf_field_type_integer *integer; |
| 622 | int ret = 0; |
| 623 | |
| 624 | if (!type || type->frozen) { |
| 625 | ret = -1; |
| 626 | goto end; |
| 627 | } |
| 628 | |
| 629 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 630 | bt_put(integer->mapped_clock); |
| 631 | bt_get(clock); |
| 632 | integer->mapped_clock = clock; |
| 633 | end: |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( |
| 638 | struct bt_ctf_field_type *integer_container_type) |
| 639 | { |
| 640 | struct bt_ctf_field_type_enumeration *enumeration = NULL; |
| 641 | |
| 642 | if (!integer_container_type) { |
| 643 | goto error; |
| 644 | } |
| 645 | |
| 646 | if (integer_container_type->declaration->id != CTF_TYPE_INTEGER) { |
| 647 | goto error; |
| 648 | } |
| 649 | |
| 650 | enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1); |
| 651 | if (!enumeration) { |
| 652 | goto error; |
| 653 | } |
| 654 | |
| 655 | enumeration->parent.declaration = &enumeration->declaration.p; |
| 656 | enumeration->parent.declaration->id = CTF_TYPE_ENUM; |
| 657 | bt_get(integer_container_type); |
| 658 | enumeration->container = integer_container_type; |
| 659 | enumeration->entries = g_ptr_array_new_with_free_func( |
| 660 | (GDestroyNotify)destroy_enumeration_mapping); |
| 661 | bt_ctf_field_type_init(&enumeration->parent, FALSE); |
| 662 | return &enumeration->parent; |
| 663 | error: |
| 664 | g_free(enumeration); |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( |
| 669 | struct bt_ctf_field_type *type) |
| 670 | { |
| 671 | struct bt_ctf_field_type *container_type = NULL; |
| 672 | struct bt_ctf_field_type_enumeration *enumeration_type; |
| 673 | |
| 674 | if (!type) { |
| 675 | goto end; |
| 676 | } |
| 677 | |
| 678 | if (type->declaration->id != CTF_TYPE_ENUM) { |
| 679 | goto end; |
| 680 | } |
| 681 | |
| 682 | enumeration_type = container_of(type, |
| 683 | struct bt_ctf_field_type_enumeration, parent); |
| 684 | container_type = enumeration_type->container; |
| 685 | bt_get(container_type); |
| 686 | end: |
| 687 | return container_type; |
| 688 | } |
| 689 | |
| 690 | int bt_ctf_field_type_enumeration_add_mapping( |
| 691 | struct bt_ctf_field_type *type, const char *string, |
| 692 | int64_t range_start, int64_t range_end) |
| 693 | { |
| 694 | int ret = 0; |
| 695 | GQuark mapping_name; |
| 696 | struct enumeration_mapping *mapping; |
| 697 | struct bt_ctf_field_type_enumeration *enumeration; |
| 698 | struct range_overlap_query query; |
| 699 | char *escaped_string; |
| 700 | |
| 701 | if (!type || (type->declaration->id != CTF_TYPE_ENUM) || |
| 702 | type->frozen || |
| 703 | (range_end < range_start)) { |
| 704 | ret = -1; |
| 705 | goto end; |
| 706 | } |
| 707 | |
| 708 | if (!string || strlen(string) == 0) { |
| 709 | ret = -1; |
| 710 | goto end; |
| 711 | } |
| 712 | |
| 713 | escaped_string = g_strescape(string, NULL); |
| 714 | if (!escaped_string) { |
| 715 | ret = -1; |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | mapping_name = g_quark_from_string(escaped_string); |
| 720 | query = (struct range_overlap_query) { |
| 721 | .range_start._signed = range_start, |
| 722 | .range_end._signed = range_end, |
| 723 | .mapping_name = mapping_name, |
| 724 | .overlaps = 0 }; |
| 725 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 726 | parent); |
| 727 | |
| 728 | /* Check that the range does not overlap with one already present */ |
| 729 | g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query); |
| 730 | if (query.overlaps) { |
| 731 | ret = -1; |
| 732 | goto error_free; |
| 733 | } |
| 734 | |
| 735 | mapping = g_new(struct enumeration_mapping, 1); |
| 736 | if (!mapping) { |
| 737 | ret = -1; |
| 738 | goto error_free; |
| 739 | } |
| 740 | |
| 741 | *mapping = (struct enumeration_mapping) { |
| 742 | .range_start._signed = range_start, |
| 743 | .range_end._signed = range_end, .string = mapping_name}; |
| 744 | g_ptr_array_add(enumeration->entries, mapping); |
| 745 | g_ptr_array_sort(enumeration->entries, |
| 746 | (GCompareFunc)compare_enumeration_mappings_signed); |
| 747 | error_free: |
| 748 | free(escaped_string); |
| 749 | end: |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | int bt_ctf_field_type_enumeration_add_mapping_unsigned( |
| 754 | struct bt_ctf_field_type *type, const char *string, |
| 755 | uint64_t range_start, uint64_t range_end) |
| 756 | { |
| 757 | int ret = 0; |
| 758 | GQuark mapping_name; |
| 759 | struct enumeration_mapping *mapping; |
| 760 | struct bt_ctf_field_type_enumeration *enumeration; |
| 761 | struct range_overlap_query query; |
| 762 | char *escaped_string; |
| 763 | |
| 764 | if (!type || (type->declaration->id != CTF_TYPE_ENUM) || |
| 765 | type->frozen || |
| 766 | (range_end < range_start)) { |
| 767 | ret = -1; |
| 768 | goto end; |
| 769 | } |
| 770 | |
| 771 | if (!string || strlen(string) == 0) { |
| 772 | ret = -1; |
| 773 | goto end; |
| 774 | } |
| 775 | |
| 776 | escaped_string = g_strescape(string, NULL); |
| 777 | if (!escaped_string) { |
| 778 | ret = -1; |
| 779 | goto end; |
| 780 | } |
| 781 | |
| 782 | mapping_name = g_quark_from_string(escaped_string); |
| 783 | query = (struct range_overlap_query) { |
| 784 | .range_start._unsigned = range_start, |
| 785 | .range_end._unsigned = range_end, |
| 786 | .mapping_name = mapping_name, |
| 787 | .overlaps = 0 }; |
| 788 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 789 | parent); |
| 790 | |
| 791 | /* Check that the range does not overlap with one already present */ |
| 792 | g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned, |
| 793 | &query); |
| 794 | if (query.overlaps) { |
| 795 | ret = -1; |
| 796 | goto error_free; |
| 797 | } |
| 798 | |
| 799 | mapping = g_new(struct enumeration_mapping, 1); |
| 800 | if (!mapping) { |
| 801 | ret = -1; |
| 802 | goto error_free; |
| 803 | } |
| 804 | |
| 805 | *mapping = (struct enumeration_mapping) { |
| 806 | .range_start._unsigned = range_start, |
| 807 | .range_end._unsigned = range_end, .string = mapping_name}; |
| 808 | g_ptr_array_add(enumeration->entries, mapping); |
| 809 | g_ptr_array_sort(enumeration->entries, |
| 810 | (GCompareFunc)compare_enumeration_mappings_unsigned); |
| 811 | error_free: |
| 812 | free(escaped_string); |
| 813 | end: |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned( |
| 818 | struct bt_ctf_field_type_enumeration *enumeration_type, |
| 819 | uint64_t value) |
| 820 | { |
| 821 | const char *name = NULL; |
| 822 | struct range_overlap_query query = |
| 823 | (struct range_overlap_query) { |
| 824 | .range_start._unsigned = value, |
| 825 | .range_end._unsigned = value, |
| 826 | .overlaps = 0 }; |
| 827 | |
| 828 | g_ptr_array_foreach(enumeration_type->entries, |
| 829 | check_ranges_overlap_unsigned, |
| 830 | &query); |
| 831 | if (!query.overlaps) { |
| 832 | goto end; |
| 833 | } |
| 834 | |
| 835 | name = g_quark_to_string(query.mapping_name); |
| 836 | end: |
| 837 | return name; |
| 838 | } |
| 839 | |
| 840 | const char *bt_ctf_field_type_enumeration_get_mapping_name_signed( |
| 841 | struct bt_ctf_field_type_enumeration *enumeration_type, |
| 842 | int64_t value) |
| 843 | { |
| 844 | const char *name = NULL; |
| 845 | struct range_overlap_query query = |
| 846 | (struct range_overlap_query) { |
| 847 | .range_start._signed = value, |
| 848 | .range_end._signed = value, |
| 849 | .overlaps = 0 }; |
| 850 | |
| 851 | g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap, |
| 852 | &query); |
| 853 | if (!query.overlaps) { |
| 854 | goto end; |
| 855 | } |
| 856 | |
| 857 | name = g_quark_to_string(query.mapping_name); |
| 858 | end: |
| 859 | return name; |
| 860 | } |
| 861 | |
| 862 | int bt_ctf_field_type_enumeration_get_mapping_count( |
| 863 | struct bt_ctf_field_type *type) |
| 864 | { |
| 865 | int ret = 0; |
| 866 | struct bt_ctf_field_type_enumeration *enumeration; |
| 867 | |
| 868 | if (!type || (type->declaration->id != CTF_TYPE_ENUM)) { |
| 869 | ret = -1; |
| 870 | goto end; |
| 871 | } |
| 872 | |
| 873 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 874 | parent); |
| 875 | ret = (int) enumeration->entries->len; |
| 876 | end: |
| 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | static inline |
| 881 | struct enumeration_mapping *get_enumeration_mapping( |
| 882 | struct bt_ctf_field_type *type, int index) |
| 883 | { |
| 884 | struct enumeration_mapping *mapping = NULL; |
| 885 | struct bt_ctf_field_type_enumeration *enumeration; |
| 886 | |
| 887 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 888 | parent); |
| 889 | if (index >= enumeration->entries->len) { |
| 890 | goto end; |
| 891 | } |
| 892 | |
| 893 | mapping = g_ptr_array_index(enumeration->entries, index); |
| 894 | end: |
| 895 | return mapping; |
| 896 | } |
| 897 | |
| 898 | int bt_ctf_field_type_enumeration_get_mapping( |
| 899 | struct bt_ctf_field_type *type, int index, |
| 900 | const char **string, int64_t *range_start, int64_t *range_end) |
| 901 | { |
| 902 | struct enumeration_mapping *mapping; |
| 903 | int ret = 0; |
| 904 | |
| 905 | if (!type || index < 0 || !string || !range_start || !range_end || |
| 906 | (type->declaration->id != CTF_TYPE_ENUM)) { |
| 907 | ret = -1; |
| 908 | goto end; |
| 909 | } |
| 910 | |
| 911 | mapping = get_enumeration_mapping(type, index); |
| 912 | if (!mapping) { |
| 913 | ret = -1; |
| 914 | goto end; |
| 915 | } |
| 916 | |
| 917 | *string = g_quark_to_string(mapping->string); |
| 918 | *range_start = mapping->range_start._signed; |
| 919 | *range_end = mapping->range_end._signed; |
| 920 | end: |
| 921 | return ret; |
| 922 | } |
| 923 | |
| 924 | int bt_ctf_field_type_enumeration_get_mapping_unsigned( |
| 925 | struct bt_ctf_field_type *type, int index, |
| 926 | const char **string, uint64_t *range_start, uint64_t *range_end) |
| 927 | { |
| 928 | struct enumeration_mapping *mapping; |
| 929 | int ret = 0; |
| 930 | |
| 931 | if (!type || index < 0 || !string || !range_start || !range_end || |
| 932 | (type->declaration->id != CTF_TYPE_ENUM)) { |
| 933 | ret = -1; |
| 934 | goto end; |
| 935 | } |
| 936 | |
| 937 | mapping = get_enumeration_mapping(type, index); |
| 938 | if (!mapping) { |
| 939 | ret = -1; |
| 940 | goto end; |
| 941 | } |
| 942 | |
| 943 | *string = g_quark_to_string(mapping->string); |
| 944 | *range_start = mapping->range_start._unsigned; |
| 945 | *range_end = mapping->range_end._unsigned; |
| 946 | end: |
| 947 | return ret; |
| 948 | } |
| 949 | |
| 950 | int bt_ctf_field_type_enumeration_get_mapping_index_by_name( |
| 951 | struct bt_ctf_field_type *type, const char *name) |
| 952 | { |
| 953 | GQuark name_quark; |
| 954 | struct bt_ctf_field_type_enumeration *enumeration; |
| 955 | int i, ret = 0; |
| 956 | |
| 957 | if (!type || !name || |
| 958 | (type->declaration->id != CTF_TYPE_ENUM)) { |
| 959 | ret = -1; |
| 960 | goto end; |
| 961 | } |
| 962 | |
| 963 | name_quark = g_quark_try_string(name); |
| 964 | if (!name_quark) { |
| 965 | ret = -1; |
| 966 | goto end; |
| 967 | } |
| 968 | |
| 969 | enumeration = container_of(type, |
| 970 | struct bt_ctf_field_type_enumeration, parent); |
| 971 | for (i = 0; i < enumeration->entries->len; i++) { |
| 972 | struct enumeration_mapping *mapping = |
| 973 | get_enumeration_mapping(type, i); |
| 974 | |
| 975 | if (mapping->string == name_quark) { |
| 976 | ret = i; |
| 977 | goto end; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | ret = -1; |
| 982 | end: |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | int bt_ctf_field_type_enumeration_get_mapping_index_by_value( |
| 987 | struct bt_ctf_field_type *type, int64_t value) |
| 988 | { |
| 989 | struct bt_ctf_field_type_enumeration *enumeration; |
| 990 | int i, ret = 0; |
| 991 | |
| 992 | if (!type || (type->declaration->id != CTF_TYPE_ENUM)) { |
| 993 | ret = -1; |
| 994 | goto end; |
| 995 | } |
| 996 | |
| 997 | enumeration = container_of(type, |
| 998 | struct bt_ctf_field_type_enumeration, parent); |
| 999 | for (i = 0; i < enumeration->entries->len; i++) { |
| 1000 | struct enumeration_mapping *mapping = |
| 1001 | get_enumeration_mapping(type, i); |
| 1002 | |
| 1003 | if (value >= mapping->range_start._signed && |
| 1004 | value <= mapping->range_end._signed) { |
| 1005 | ret = i; |
| 1006 | goto end; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | ret = -1; |
| 1011 | end: |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
| 1015 | int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value( |
| 1016 | struct bt_ctf_field_type *type, uint64_t value) |
| 1017 | { |
| 1018 | struct bt_ctf_field_type_enumeration *enumeration; |
| 1019 | int i, ret = 0; |
| 1020 | |
| 1021 | if (!type || (type->declaration->id != CTF_TYPE_ENUM)) { |
| 1022 | ret = -1; |
| 1023 | goto end; |
| 1024 | } |
| 1025 | |
| 1026 | enumeration = container_of(type, |
| 1027 | struct bt_ctf_field_type_enumeration, parent); |
| 1028 | for (i = 0; i < enumeration->entries->len; i++) { |
| 1029 | struct enumeration_mapping *mapping = |
| 1030 | get_enumeration_mapping(type, i); |
| 1031 | |
| 1032 | if (value >= mapping->range_start._unsigned && |
| 1033 | value <= mapping->range_end._unsigned) { |
| 1034 | ret = i; |
| 1035 | goto end; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | ret = -1; |
| 1040 | end: |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void) |
| 1045 | { |
| 1046 | struct bt_ctf_field_type_floating_point *floating_point = |
| 1047 | g_new0(struct bt_ctf_field_type_floating_point, 1); |
| 1048 | |
| 1049 | if (!floating_point) { |
| 1050 | goto end; |
| 1051 | } |
| 1052 | |
| 1053 | floating_point->declaration.sign = &floating_point->sign; |
| 1054 | floating_point->declaration.mantissa = &floating_point->mantissa; |
| 1055 | floating_point->declaration.exp = &floating_point->exp; |
| 1056 | floating_point->sign.len = 1; |
| 1057 | floating_point->parent.declaration = &floating_point->declaration.p; |
| 1058 | floating_point->parent.declaration->id = CTF_TYPE_FLOAT; |
| 1059 | floating_point->declaration.exp->len = |
| 1060 | sizeof(float) * CHAR_BIT - FLT_MANT_DIG; |
| 1061 | floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1; |
| 1062 | floating_point->sign.p.alignment = 1; |
| 1063 | floating_point->mantissa.p.alignment = 1; |
| 1064 | floating_point->exp.p.alignment = 1; |
| 1065 | |
| 1066 | bt_ctf_field_type_init(&floating_point->parent, TRUE); |
| 1067 | end: |
| 1068 | return floating_point ? &floating_point->parent : NULL; |
| 1069 | } |
| 1070 | |
| 1071 | int bt_ctf_field_type_floating_point_get_exponent_digits( |
| 1072 | struct bt_ctf_field_type *type) |
| 1073 | { |
| 1074 | int ret = 0; |
| 1075 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1076 | |
| 1077 | if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) { |
| 1078 | ret = -1; |
| 1079 | goto end; |
| 1080 | } |
| 1081 | |
| 1082 | floating_point = container_of(type, |
| 1083 | struct bt_ctf_field_type_floating_point, parent); |
| 1084 | ret = (int) floating_point->declaration.exp->len; |
| 1085 | end: |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
| 1089 | int bt_ctf_field_type_floating_point_set_exponent_digits( |
| 1090 | struct bt_ctf_field_type *type, |
| 1091 | unsigned int exponent_digits) |
| 1092 | { |
| 1093 | int ret = 0; |
| 1094 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1095 | |
| 1096 | if (!type || type->frozen || |
| 1097 | (type->declaration->id != CTF_TYPE_FLOAT)) { |
| 1098 | ret = -1; |
| 1099 | goto end; |
| 1100 | } |
| 1101 | |
| 1102 | floating_point = container_of(type, |
| 1103 | struct bt_ctf_field_type_floating_point, parent); |
| 1104 | if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) && |
| 1105 | (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) && |
| 1106 | (exponent_digits != |
| 1107 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) { |
| 1108 | ret = -1; |
| 1109 | goto end; |
| 1110 | } |
| 1111 | |
| 1112 | floating_point->declaration.exp->len = exponent_digits; |
| 1113 | end: |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | int bt_ctf_field_type_floating_point_get_mantissa_digits( |
| 1118 | struct bt_ctf_field_type *type) |
| 1119 | { |
| 1120 | int ret = 0; |
| 1121 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1122 | |
| 1123 | if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) { |
| 1124 | ret = -1; |
| 1125 | goto end; |
| 1126 | } |
| 1127 | |
| 1128 | floating_point = container_of(type, |
| 1129 | struct bt_ctf_field_type_floating_point, parent); |
| 1130 | ret = (int) floating_point->mantissa.len + 1; |
| 1131 | end: |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
| 1135 | int bt_ctf_field_type_floating_point_set_mantissa_digits( |
| 1136 | struct bt_ctf_field_type *type, |
| 1137 | unsigned int mantissa_digits) |
| 1138 | { |
| 1139 | int ret = 0; |
| 1140 | struct bt_ctf_field_type_floating_point *floating_point; |
| 1141 | |
| 1142 | if (!type || type->frozen || |
| 1143 | (type->declaration->id != CTF_TYPE_FLOAT)) { |
| 1144 | ret = -1; |
| 1145 | goto end; |
| 1146 | } |
| 1147 | |
| 1148 | floating_point = container_of(type, |
| 1149 | struct bt_ctf_field_type_floating_point, parent); |
| 1150 | |
| 1151 | if ((mantissa_digits != FLT_MANT_DIG) && |
| 1152 | (mantissa_digits != DBL_MANT_DIG) && |
| 1153 | (mantissa_digits != LDBL_MANT_DIG)) { |
| 1154 | ret = -1; |
| 1155 | goto end; |
| 1156 | } |
| 1157 | |
| 1158 | floating_point->declaration.mantissa->len = mantissa_digits - 1; |
| 1159 | end: |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void) |
| 1164 | { |
| 1165 | struct bt_ctf_field_type_structure *structure = |
| 1166 | g_new0(struct bt_ctf_field_type_structure, 1); |
| 1167 | |
| 1168 | if (!structure) { |
| 1169 | goto error; |
| 1170 | } |
| 1171 | |
| 1172 | structure->parent.declaration = &structure->declaration.p; |
| 1173 | structure->parent.declaration->id = CTF_TYPE_STRUCT; |
| 1174 | structure->fields = g_ptr_array_new_with_free_func( |
| 1175 | (GDestroyNotify)destroy_structure_field); |
| 1176 | structure->field_name_to_index = g_hash_table_new(NULL, NULL); |
| 1177 | bt_ctf_field_type_init(&structure->parent, TRUE); |
| 1178 | return &structure->parent; |
| 1179 | error: |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | |
| 1183 | int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type, |
| 1184 | struct bt_ctf_field_type *field_type, |
| 1185 | const char *field_name) |
| 1186 | { |
| 1187 | int ret = 0; |
| 1188 | struct bt_ctf_field_type_structure *structure; |
| 1189 | |
| 1190 | if (!type || !field_type || type->frozen || |
| 1191 | bt_ctf_validate_identifier(field_name) || |
| 1192 | (type->declaration->id != CTF_TYPE_STRUCT) || |
| 1193 | bt_ctf_field_type_validate(field_type)) { |
| 1194 | ret = -1; |
| 1195 | goto end; |
| 1196 | } |
| 1197 | |
| 1198 | structure = container_of(type, |
| 1199 | struct bt_ctf_field_type_structure, parent); |
| 1200 | if (add_structure_field(structure->fields, |
| 1201 | structure->field_name_to_index, field_type, field_name)) { |
| 1202 | ret = -1; |
| 1203 | goto end; |
| 1204 | } |
| 1205 | end: |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | int bt_ctf_field_type_structure_get_field_count( |
| 1210 | struct bt_ctf_field_type *type) |
| 1211 | { |
| 1212 | int ret = 0; |
| 1213 | struct bt_ctf_field_type_structure *structure; |
| 1214 | |
| 1215 | if (!type || (type->declaration->id != CTF_TYPE_STRUCT)) { |
| 1216 | ret = -1; |
| 1217 | goto end; |
| 1218 | } |
| 1219 | |
| 1220 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 1221 | parent); |
| 1222 | ret = (int) structure->fields->len; |
| 1223 | end: |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
| 1227 | int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *type, |
| 1228 | const char **field_name, struct bt_ctf_field_type **field_type, |
| 1229 | int index) |
| 1230 | { |
| 1231 | struct bt_ctf_field_type_structure *structure; |
| 1232 | struct structure_field *field; |
| 1233 | int ret = 0; |
| 1234 | |
| 1235 | if (!type || index < 0 || (type->declaration->id != CTF_TYPE_STRUCT)) { |
| 1236 | ret = -1; |
| 1237 | goto end; |
| 1238 | } |
| 1239 | |
| 1240 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 1241 | parent); |
| 1242 | if (index >= structure->fields->len) { |
| 1243 | ret = -1; |
| 1244 | goto end; |
| 1245 | } |
| 1246 | |
| 1247 | field = g_ptr_array_index(structure->fields, index); |
| 1248 | if (field_type) { |
| 1249 | *field_type = field->type; |
| 1250 | bt_get(field->type); |
| 1251 | } |
| 1252 | if (field_name) { |
| 1253 | *field_name = g_quark_to_string(field->name); |
| 1254 | } |
| 1255 | end: |
| 1256 | return ret; |
| 1257 | } |
| 1258 | |
| 1259 | struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( |
| 1260 | struct bt_ctf_field_type *type, |
| 1261 | const char *name) |
| 1262 | { |
| 1263 | size_t index; |
| 1264 | GQuark name_quark; |
| 1265 | struct structure_field *field; |
| 1266 | struct bt_ctf_field_type_structure *structure; |
| 1267 | struct bt_ctf_field_type *field_type = NULL; |
| 1268 | |
| 1269 | if (!type || !name) { |
| 1270 | goto end; |
| 1271 | } |
| 1272 | |
| 1273 | name_quark = g_quark_try_string(name); |
| 1274 | if (!name_quark) { |
| 1275 | goto end; |
| 1276 | } |
| 1277 | |
| 1278 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 1279 | parent); |
| 1280 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 1281 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { |
| 1282 | goto end; |
| 1283 | } |
| 1284 | |
| 1285 | field = structure->fields->pdata[index]; |
| 1286 | field_type = field->type; |
| 1287 | bt_get(field_type); |
| 1288 | end: |
| 1289 | return field_type; |
| 1290 | } |
| 1291 | |
| 1292 | struct bt_ctf_field_type *bt_ctf_field_type_variant_create( |
| 1293 | struct bt_ctf_field_type *enum_tag, const char *tag_name) |
| 1294 | { |
| 1295 | struct bt_ctf_field_type_variant *variant = NULL; |
| 1296 | |
| 1297 | if (tag_name && bt_ctf_validate_identifier(tag_name)) { |
| 1298 | goto error; |
| 1299 | } |
| 1300 | |
| 1301 | variant = g_new0(struct bt_ctf_field_type_variant, 1); |
| 1302 | if (!variant) { |
| 1303 | goto error; |
| 1304 | } |
| 1305 | |
| 1306 | variant->parent.declaration = &variant->declaration.p; |
| 1307 | variant->parent.declaration->id = CTF_TYPE_VARIANT; |
| 1308 | variant->tag_name = g_string_new(tag_name); |
| 1309 | variant->field_name_to_index = g_hash_table_new(NULL, NULL); |
| 1310 | variant->fields = g_ptr_array_new_with_free_func( |
| 1311 | (GDestroyNotify) destroy_structure_field); |
| 1312 | if (enum_tag) { |
| 1313 | bt_get(enum_tag); |
| 1314 | variant->tag = container_of(enum_tag, |
| 1315 | struct bt_ctf_field_type_enumeration, parent); |
| 1316 | } |
| 1317 | |
| 1318 | bt_ctf_field_type_init(&variant->parent, TRUE); |
| 1319 | /* A variant's alignment is undefined */ |
| 1320 | variant->parent.declaration->alignment = 0; |
| 1321 | return &variant->parent; |
| 1322 | error: |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | |
| 1326 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( |
| 1327 | struct bt_ctf_field_type *type) |
| 1328 | { |
| 1329 | struct bt_ctf_field_type_variant *variant; |
| 1330 | struct bt_ctf_field_type *tag_type = NULL; |
| 1331 | |
| 1332 | if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) { |
| 1333 | goto end; |
| 1334 | } |
| 1335 | |
| 1336 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 1337 | if (!variant->tag) { |
| 1338 | goto end; |
| 1339 | } |
| 1340 | |
| 1341 | tag_type = &variant->tag->parent; |
| 1342 | bt_get(tag_type); |
| 1343 | end: |
| 1344 | return tag_type; |
| 1345 | } |
| 1346 | |
| 1347 | const char *bt_ctf_field_type_variant_get_tag_name( |
| 1348 | struct bt_ctf_field_type *type) |
| 1349 | { |
| 1350 | struct bt_ctf_field_type_variant *variant; |
| 1351 | const char *tag_name = NULL; |
| 1352 | |
| 1353 | if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) { |
| 1354 | goto end; |
| 1355 | } |
| 1356 | |
| 1357 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 1358 | if (variant->tag_name->len == 0) { |
| 1359 | goto end; |
| 1360 | } |
| 1361 | |
| 1362 | tag_name = variant->tag_name->str; |
| 1363 | end: |
| 1364 | return tag_name; |
| 1365 | } |
| 1366 | |
| 1367 | int bt_ctf_field_type_variant_set_tag_name( |
| 1368 | struct bt_ctf_field_type *type, const char *name) |
| 1369 | { |
| 1370 | int ret = 0; |
| 1371 | struct bt_ctf_field_type_variant *variant; |
| 1372 | |
| 1373 | if (!type || type->frozen || |
| 1374 | (type->declaration->id != CTF_TYPE_VARIANT) || |
| 1375 | bt_ctf_validate_identifier(name)) { |
| 1376 | ret = -1; |
| 1377 | goto end; |
| 1378 | } |
| 1379 | |
| 1380 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 1381 | g_string_assign(variant->tag_name, name); |
| 1382 | end: |
| 1383 | return ret; |
| 1384 | } |
| 1385 | |
| 1386 | int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type, |
| 1387 | struct bt_ctf_field_type *field_type, |
| 1388 | const char *field_name) |
| 1389 | { |
| 1390 | size_t i; |
| 1391 | int ret = 0; |
| 1392 | struct bt_ctf_field_type_variant *variant; |
| 1393 | GQuark field_name_quark = g_quark_from_string(field_name); |
| 1394 | |
| 1395 | if (!type || !field_type || type->frozen || |
| 1396 | bt_ctf_validate_identifier(field_name) || |
| 1397 | (type->declaration->id != CTF_TYPE_VARIANT) || |
| 1398 | bt_ctf_field_type_validate(field_type)) { |
| 1399 | ret = -1; |
| 1400 | goto end; |
| 1401 | } |
| 1402 | |
| 1403 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 1404 | |
| 1405 | /* The user has explicitly provided a tag; validate against it. */ |
| 1406 | if (variant->tag) { |
| 1407 | int name_found = 0; |
| 1408 | |
| 1409 | /* Make sure this name is present in the enum tag */ |
| 1410 | for (i = 0; i < variant->tag->entries->len; i++) { |
| 1411 | struct enumeration_mapping *mapping = |
| 1412 | g_ptr_array_index(variant->tag->entries, i); |
| 1413 | |
| 1414 | if (mapping->string == field_name_quark) { |
| 1415 | name_found = 1; |
| 1416 | break; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | if (!name_found) { |
| 1421 | /* Validation failed */ |
| 1422 | ret = -1; |
| 1423 | goto end; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | if (add_structure_field(variant->fields, variant->field_name_to_index, |
| 1428 | field_type, field_name)) { |
| 1429 | ret = -1; |
| 1430 | goto end; |
| 1431 | } |
| 1432 | end: |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( |
| 1437 | struct bt_ctf_field_type *type, |
| 1438 | const char *field_name) |
| 1439 | { |
| 1440 | size_t index; |
| 1441 | GQuark name_quark; |
| 1442 | struct structure_field *field; |
| 1443 | struct bt_ctf_field_type_variant *variant; |
| 1444 | struct bt_ctf_field_type *field_type = NULL; |
| 1445 | |
| 1446 | if (!type || !field_name) { |
| 1447 | goto end; |
| 1448 | } |
| 1449 | |
| 1450 | name_quark = g_quark_try_string(field_name); |
| 1451 | if (!name_quark) { |
| 1452 | goto end; |
| 1453 | } |
| 1454 | |
| 1455 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); |
| 1456 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 1457 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { |
| 1458 | goto end; |
| 1459 | } |
| 1460 | |
| 1461 | field = g_ptr_array_index(variant->fields, index); |
| 1462 | field_type = field->type; |
| 1463 | bt_get(field_type); |
| 1464 | end: |
| 1465 | return field_type; |
| 1466 | } |
| 1467 | |
| 1468 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( |
| 1469 | struct bt_ctf_field_type *type, |
| 1470 | struct bt_ctf_field *tag) |
| 1471 | { |
| 1472 | const char *enum_value; |
| 1473 | struct bt_ctf_field_type *field_type = NULL; |
| 1474 | |
| 1475 | if (!type || !tag || type->declaration->id != CTF_TYPE_VARIANT) { |
| 1476 | goto end; |
| 1477 | } |
| 1478 | |
| 1479 | enum_value = bt_ctf_field_enumeration_get_mapping_name(tag); |
| 1480 | if (!enum_value) { |
| 1481 | goto end; |
| 1482 | } |
| 1483 | |
| 1484 | /* Already increments field_type's reference count */ |
| 1485 | field_type = bt_ctf_field_type_variant_get_field_type_by_name( |
| 1486 | type, enum_value); |
| 1487 | end: |
| 1488 | return field_type; |
| 1489 | } |
| 1490 | |
| 1491 | int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type) |
| 1492 | { |
| 1493 | int ret = 0; |
| 1494 | struct bt_ctf_field_type_variant *variant; |
| 1495 | |
| 1496 | if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) { |
| 1497 | ret = -1; |
| 1498 | goto end; |
| 1499 | } |
| 1500 | |
| 1501 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 1502 | parent); |
| 1503 | ret = (int) variant->fields->len; |
| 1504 | end: |
| 1505 | return ret; |
| 1506 | |
| 1507 | } |
| 1508 | |
| 1509 | int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *type, |
| 1510 | const char **field_name, struct bt_ctf_field_type **field_type, |
| 1511 | int index) |
| 1512 | { |
| 1513 | struct bt_ctf_field_type_variant *variant; |
| 1514 | struct structure_field *field; |
| 1515 | int ret = 0; |
| 1516 | |
| 1517 | if (!type || index < 0 || (type->declaration->id != CTF_TYPE_VARIANT)) { |
| 1518 | ret = -1; |
| 1519 | goto end; |
| 1520 | } |
| 1521 | |
| 1522 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 1523 | parent); |
| 1524 | if (index >= variant->fields->len) { |
| 1525 | ret = -1; |
| 1526 | goto end; |
| 1527 | } |
| 1528 | |
| 1529 | field = g_ptr_array_index(variant->fields, index); |
| 1530 | if (field_type) { |
| 1531 | *field_type = field->type; |
| 1532 | bt_get(field->type); |
| 1533 | } |
| 1534 | if (field_name) { |
| 1535 | *field_name = g_quark_to_string(field->name); |
| 1536 | } |
| 1537 | end: |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | struct bt_ctf_field_type *bt_ctf_field_type_array_create( |
| 1542 | struct bt_ctf_field_type *element_type, |
| 1543 | unsigned int length) |
| 1544 | { |
| 1545 | struct bt_ctf_field_type_array *array = NULL; |
| 1546 | |
| 1547 | if (!element_type || length == 0 || |
| 1548 | bt_ctf_field_type_validate(element_type)) { |
| 1549 | goto error; |
| 1550 | } |
| 1551 | |
| 1552 | array = g_new0(struct bt_ctf_field_type_array, 1); |
| 1553 | if (!array) { |
| 1554 | goto error; |
| 1555 | } |
| 1556 | |
| 1557 | array->parent.declaration = &array->declaration.p; |
| 1558 | array->parent.declaration->id = CTF_TYPE_ARRAY; |
| 1559 | |
| 1560 | bt_get(element_type); |
| 1561 | array->element_type = element_type; |
| 1562 | array->length = length; |
| 1563 | bt_ctf_field_type_init(&array->parent, FALSE); |
| 1564 | return &array->parent; |
| 1565 | error: |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | |
| 1569 | struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( |
| 1570 | struct bt_ctf_field_type *type) |
| 1571 | { |
| 1572 | struct bt_ctf_field_type *ret = NULL; |
| 1573 | struct bt_ctf_field_type_array *array; |
| 1574 | |
| 1575 | if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) { |
| 1576 | goto end; |
| 1577 | } |
| 1578 | |
| 1579 | array = container_of(type, struct bt_ctf_field_type_array, parent); |
| 1580 | ret = array->element_type; |
| 1581 | bt_get(ret); |
| 1582 | end: |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
| 1586 | BT_HIDDEN |
| 1587 | int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *type, |
| 1588 | struct bt_ctf_field_type *element_type) |
| 1589 | { |
| 1590 | int ret = 0; |
| 1591 | struct bt_ctf_field_type_array *array; |
| 1592 | |
| 1593 | if (!type || !element_type || |
| 1594 | (type->declaration->id != CTF_TYPE_ARRAY)) { |
| 1595 | ret = -1; |
| 1596 | goto end; |
| 1597 | } |
| 1598 | |
| 1599 | array = container_of(type, struct bt_ctf_field_type_array, parent); |
| 1600 | |
| 1601 | if (array->element_type) { |
| 1602 | BT_PUT(array->element_type); |
| 1603 | } |
| 1604 | |
| 1605 | array->element_type = element_type; |
| 1606 | bt_get(array->element_type); |
| 1607 | |
| 1608 | end: |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
| 1612 | int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type) |
| 1613 | { |
| 1614 | int64_t ret; |
| 1615 | struct bt_ctf_field_type_array *array; |
| 1616 | |
| 1617 | if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) { |
| 1618 | ret = -1; |
| 1619 | goto end; |
| 1620 | } |
| 1621 | |
| 1622 | array = container_of(type, struct bt_ctf_field_type_array, parent); |
| 1623 | ret = (int64_t) array->length; |
| 1624 | end: |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
| 1628 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( |
| 1629 | struct bt_ctf_field_type *element_type, |
| 1630 | const char *length_field_name) |
| 1631 | { |
| 1632 | struct bt_ctf_field_type_sequence *sequence = NULL; |
| 1633 | |
| 1634 | if (!element_type || bt_ctf_validate_identifier(length_field_name) || |
| 1635 | bt_ctf_field_type_validate(element_type)) { |
| 1636 | goto error; |
| 1637 | } |
| 1638 | |
| 1639 | sequence = g_new0(struct bt_ctf_field_type_sequence, 1); |
| 1640 | if (!sequence) { |
| 1641 | goto error; |
| 1642 | } |
| 1643 | |
| 1644 | sequence->parent.declaration = &sequence->declaration.p; |
| 1645 | sequence->parent.declaration->id = CTF_TYPE_SEQUENCE; |
| 1646 | bt_get(element_type); |
| 1647 | sequence->element_type = element_type; |
| 1648 | sequence->length_field_name = g_string_new(length_field_name); |
| 1649 | bt_ctf_field_type_init(&sequence->parent, FALSE); |
| 1650 | return &sequence->parent; |
| 1651 | error: |
| 1652 | return NULL; |
| 1653 | } |
| 1654 | |
| 1655 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( |
| 1656 | struct bt_ctf_field_type *type) |
| 1657 | { |
| 1658 | struct bt_ctf_field_type *ret = NULL; |
| 1659 | struct bt_ctf_field_type_sequence *sequence; |
| 1660 | |
| 1661 | if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) { |
| 1662 | goto end; |
| 1663 | } |
| 1664 | |
| 1665 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 1666 | parent); |
| 1667 | ret = sequence->element_type; |
| 1668 | bt_get(ret); |
| 1669 | end: |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | BT_HIDDEN |
| 1674 | int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *type, |
| 1675 | struct bt_ctf_field_type *element_type) |
| 1676 | { |
| 1677 | int ret = 0; |
| 1678 | struct bt_ctf_field_type_sequence *sequence; |
| 1679 | |
| 1680 | if (!type || !element_type || |
| 1681 | (type->declaration->id != CTF_TYPE_SEQUENCE)) { |
| 1682 | ret = -1; |
| 1683 | goto end; |
| 1684 | } |
| 1685 | |
| 1686 | sequence = container_of(type, struct bt_ctf_field_type_sequence, parent); |
| 1687 | |
| 1688 | if (sequence->element_type) { |
| 1689 | BT_PUT(sequence->element_type); |
| 1690 | } |
| 1691 | |
| 1692 | sequence->element_type = element_type; |
| 1693 | bt_get(sequence->element_type); |
| 1694 | |
| 1695 | end: |
| 1696 | return ret; |
| 1697 | } |
| 1698 | |
| 1699 | const char *bt_ctf_field_type_sequence_get_length_field_name( |
| 1700 | struct bt_ctf_field_type *type) |
| 1701 | { |
| 1702 | const char *ret = NULL; |
| 1703 | struct bt_ctf_field_type_sequence *sequence; |
| 1704 | |
| 1705 | if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) { |
| 1706 | goto end; |
| 1707 | } |
| 1708 | |
| 1709 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 1710 | parent); |
| 1711 | ret = sequence->length_field_name->str; |
| 1712 | end: |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | struct bt_ctf_field_type *bt_ctf_field_type_string_create(void) |
| 1717 | { |
| 1718 | struct bt_ctf_field_type_string *string = |
| 1719 | g_new0(struct bt_ctf_field_type_string, 1); |
| 1720 | |
| 1721 | if (!string) { |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | |
| 1725 | string->parent.declaration = &string->declaration.p; |
| 1726 | string->parent.declaration->id = CTF_TYPE_STRING; |
| 1727 | bt_ctf_field_type_init(&string->parent, TRUE); |
| 1728 | string->declaration.encoding = CTF_STRING_UTF8; |
| 1729 | string->parent.declaration->alignment = CHAR_BIT; |
| 1730 | return &string->parent; |
| 1731 | } |
| 1732 | |
| 1733 | enum ctf_string_encoding bt_ctf_field_type_string_get_encoding( |
| 1734 | struct bt_ctf_field_type *type) |
| 1735 | { |
| 1736 | struct bt_ctf_field_type_string *string; |
| 1737 | enum ctf_string_encoding ret = CTF_STRING_UNKNOWN; |
| 1738 | |
| 1739 | if (!type || (type->declaration->id != CTF_TYPE_STRING)) { |
| 1740 | goto end; |
| 1741 | } |
| 1742 | |
| 1743 | string = container_of(type, struct bt_ctf_field_type_string, |
| 1744 | parent); |
| 1745 | ret = string->declaration.encoding; |
| 1746 | end: |
| 1747 | return ret; |
| 1748 | } |
| 1749 | |
| 1750 | int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type, |
| 1751 | enum ctf_string_encoding encoding) |
| 1752 | { |
| 1753 | int ret = 0; |
| 1754 | struct bt_ctf_field_type_string *string; |
| 1755 | |
| 1756 | if (!type || type->declaration->id != CTF_TYPE_STRING || |
| 1757 | (encoding != CTF_STRING_UTF8 && |
| 1758 | encoding != CTF_STRING_ASCII)) { |
| 1759 | ret = -1; |
| 1760 | goto end; |
| 1761 | } |
| 1762 | |
| 1763 | string = container_of(type, struct bt_ctf_field_type_string, parent); |
| 1764 | string->declaration.encoding = encoding; |
| 1765 | end: |
| 1766 | return ret; |
| 1767 | } |
| 1768 | |
| 1769 | int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type) |
| 1770 | { |
| 1771 | int ret; |
| 1772 | enum ctf_type_id type_id; |
| 1773 | |
| 1774 | if (!type) { |
| 1775 | ret = -1; |
| 1776 | goto end; |
| 1777 | } |
| 1778 | |
| 1779 | if (type->frozen) { |
| 1780 | ret = (int) type->declaration->alignment; |
| 1781 | goto end; |
| 1782 | } |
| 1783 | |
| 1784 | type_id = bt_ctf_field_type_get_type_id(type); |
| 1785 | switch (type_id) { |
| 1786 | case CTF_TYPE_SEQUENCE: |
| 1787 | { |
| 1788 | struct bt_ctf_field_type *element = |
| 1789 | bt_ctf_field_type_sequence_get_element_type(type); |
| 1790 | |
| 1791 | if (!element) { |
| 1792 | ret = -1; |
| 1793 | goto end; |
| 1794 | } |
| 1795 | |
| 1796 | ret = bt_ctf_field_type_get_alignment(element); |
| 1797 | bt_put(element); |
| 1798 | break; |
| 1799 | } |
| 1800 | case CTF_TYPE_ARRAY: |
| 1801 | { |
| 1802 | struct bt_ctf_field_type *element = |
| 1803 | bt_ctf_field_type_array_get_element_type(type); |
| 1804 | |
| 1805 | if (!element) { |
| 1806 | ret = -1; |
| 1807 | goto end; |
| 1808 | } |
| 1809 | |
| 1810 | ret = bt_ctf_field_type_get_alignment(element); |
| 1811 | bt_put(element); |
| 1812 | break; |
| 1813 | } |
| 1814 | case CTF_TYPE_STRUCT: |
| 1815 | { |
| 1816 | int i, element_count; |
| 1817 | |
| 1818 | element_count = bt_ctf_field_type_structure_get_field_count( |
| 1819 | type); |
| 1820 | if (element_count < 0) { |
| 1821 | ret = element_count; |
| 1822 | goto end; |
| 1823 | } |
| 1824 | |
| 1825 | for (i = 0; i < element_count; i++) { |
| 1826 | struct bt_ctf_field_type *field; |
| 1827 | int field_alignment; |
| 1828 | |
| 1829 | ret = bt_ctf_field_type_structure_get_field(type, NULL, |
| 1830 | &field, i); |
| 1831 | if (ret) { |
| 1832 | goto end; |
| 1833 | } |
| 1834 | |
| 1835 | assert(field); |
| 1836 | field_alignment = bt_ctf_field_type_get_alignment( |
| 1837 | field); |
| 1838 | bt_put(field); |
| 1839 | if (field_alignment < 0) { |
| 1840 | ret = field_alignment; |
| 1841 | goto end; |
| 1842 | } |
| 1843 | |
| 1844 | type->declaration->alignment = MAX(field_alignment, |
| 1845 | type->declaration->alignment); |
| 1846 | } |
| 1847 | ret = (int) type->declaration->alignment; |
| 1848 | break; |
| 1849 | } |
| 1850 | case CTF_TYPE_UNKNOWN: |
| 1851 | ret = -1; |
| 1852 | break; |
| 1853 | default: |
| 1854 | ret = (int) type->declaration->alignment; |
| 1855 | break; |
| 1856 | } |
| 1857 | end: |
| 1858 | return ret; |
| 1859 | } |
| 1860 | |
| 1861 | static inline |
| 1862 | int is_power_of_two(unsigned int value) |
| 1863 | { |
| 1864 | return ((value & (value - 1)) == 0) && value > 0; |
| 1865 | } |
| 1866 | |
| 1867 | int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, |
| 1868 | unsigned int alignment) |
| 1869 | { |
| 1870 | int ret = 0; |
| 1871 | enum ctf_type_id type_id; |
| 1872 | |
| 1873 | /* Alignment must be a power of two */ |
| 1874 | if (!type || type->frozen || !is_power_of_two(alignment)) { |
| 1875 | ret = -1; |
| 1876 | goto end; |
| 1877 | } |
| 1878 | |
| 1879 | type_id = bt_ctf_field_type_get_type_id(type); |
| 1880 | if (type_id == CTF_TYPE_UNKNOWN) { |
| 1881 | ret = -1; |
| 1882 | goto end; |
| 1883 | } |
| 1884 | |
| 1885 | if (type->declaration->id == CTF_TYPE_STRING && |
| 1886 | alignment != CHAR_BIT) { |
| 1887 | ret = -1; |
| 1888 | goto end; |
| 1889 | } |
| 1890 | |
| 1891 | if (type_id == CTF_TYPE_VARIANT || type_id == CTF_TYPE_SEQUENCE || |
| 1892 | type_id == CTF_TYPE_ARRAY) { |
| 1893 | /* Setting an alignment on these types makes no sense */ |
| 1894 | ret = -1; |
| 1895 | goto end; |
| 1896 | } |
| 1897 | |
| 1898 | type->declaration->alignment = alignment; |
| 1899 | ret = 0; |
| 1900 | end: |
| 1901 | return ret; |
| 1902 | } |
| 1903 | |
| 1904 | enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( |
| 1905 | struct bt_ctf_field_type *type) |
| 1906 | { |
| 1907 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; |
| 1908 | |
| 1909 | if (!type) { |
| 1910 | goto end; |
| 1911 | } |
| 1912 | |
| 1913 | switch (type->declaration->id) { |
| 1914 | case CTF_TYPE_INTEGER: |
| 1915 | { |
| 1916 | struct bt_ctf_field_type_integer *integer = container_of( |
| 1917 | type, struct bt_ctf_field_type_integer, parent); |
| 1918 | ret = integer->user_byte_order; |
| 1919 | break; |
| 1920 | } |
| 1921 | case CTF_TYPE_FLOAT: |
| 1922 | { |
| 1923 | struct bt_ctf_field_type_floating_point *floating_point = |
| 1924 | container_of(type, |
| 1925 | struct bt_ctf_field_type_floating_point, |
| 1926 | parent); |
| 1927 | ret = floating_point->user_byte_order; |
| 1928 | break; |
| 1929 | } |
| 1930 | default: |
| 1931 | goto end; |
| 1932 | } |
| 1933 | |
| 1934 | assert(ret == BT_CTF_BYTE_ORDER_NATIVE || |
| 1935 | ret == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || |
| 1936 | ret == BT_CTF_BYTE_ORDER_BIG_ENDIAN || |
| 1937 | ret == BT_CTF_BYTE_ORDER_NETWORK); |
| 1938 | |
| 1939 | end: |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
| 1943 | int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, |
| 1944 | enum bt_ctf_byte_order byte_order) |
| 1945 | { |
| 1946 | int ret = 0; |
| 1947 | int internal_byte_order; |
| 1948 | enum ctf_type_id type_id; |
| 1949 | |
| 1950 | if (!type || type->frozen) { |
| 1951 | ret = -1; |
| 1952 | goto end; |
| 1953 | } |
| 1954 | |
| 1955 | switch (byte_order) { |
| 1956 | case BT_CTF_BYTE_ORDER_NATIVE: |
| 1957 | /* Leave unset. Will be initialized by parent. */ |
| 1958 | internal_byte_order = 0; |
| 1959 | break; |
| 1960 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 1961 | internal_byte_order = LITTLE_ENDIAN; |
| 1962 | break; |
| 1963 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 1964 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 1965 | internal_byte_order = BIG_ENDIAN; |
| 1966 | break; |
| 1967 | default: |
| 1968 | ret = -1; |
| 1969 | goto end; |
| 1970 | } |
| 1971 | |
| 1972 | type_id = type->declaration->id; |
| 1973 | if (set_byte_order_funcs[type_id]) { |
| 1974 | set_byte_order_funcs[type_id](type, internal_byte_order, 0); |
| 1975 | } |
| 1976 | end: |
| 1977 | return ret; |
| 1978 | } |
| 1979 | |
| 1980 | enum ctf_type_id bt_ctf_field_type_get_type_id( |
| 1981 | struct bt_ctf_field_type *type) |
| 1982 | { |
| 1983 | if (!type) { |
| 1984 | return CTF_TYPE_UNKNOWN; |
| 1985 | } |
| 1986 | |
| 1987 | return type->declaration->id; |
| 1988 | } |
| 1989 | |
| 1990 | int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type) |
| 1991 | { |
| 1992 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_INTEGER; |
| 1993 | } |
| 1994 | |
| 1995 | int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type) |
| 1996 | { |
| 1997 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_FLOAT; |
| 1998 | } |
| 1999 | |
| 2000 | int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type) |
| 2001 | { |
| 2002 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_ENUM; |
| 2003 | } |
| 2004 | |
| 2005 | int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type) |
| 2006 | { |
| 2007 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_STRING; |
| 2008 | } |
| 2009 | |
| 2010 | int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type) |
| 2011 | { |
| 2012 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_STRUCT; |
| 2013 | } |
| 2014 | |
| 2015 | int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type) |
| 2016 | { |
| 2017 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_ARRAY; |
| 2018 | } |
| 2019 | |
| 2020 | int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type) |
| 2021 | { |
| 2022 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_SEQUENCE; |
| 2023 | } |
| 2024 | |
| 2025 | int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type) |
| 2026 | { |
| 2027 | return bt_ctf_field_type_get_type_id(type) == CTF_TYPE_VARIANT; |
| 2028 | } |
| 2029 | |
| 2030 | void bt_ctf_field_type_get(struct bt_ctf_field_type *type) |
| 2031 | { |
| 2032 | bt_get(type); |
| 2033 | } |
| 2034 | |
| 2035 | void bt_ctf_field_type_put(struct bt_ctf_field_type *type) |
| 2036 | { |
| 2037 | bt_put(type); |
| 2038 | } |
| 2039 | |
| 2040 | BT_HIDDEN |
| 2041 | void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type) |
| 2042 | { |
| 2043 | if (!type) { |
| 2044 | return; |
| 2045 | } |
| 2046 | |
| 2047 | type->freeze(type); |
| 2048 | } |
| 2049 | |
| 2050 | BT_HIDDEN |
| 2051 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( |
| 2052 | struct bt_ctf_field_type_variant *variant, |
| 2053 | int64_t tag_value) |
| 2054 | { |
| 2055 | struct bt_ctf_field_type *type = NULL; |
| 2056 | GQuark field_name_quark; |
| 2057 | gpointer index; |
| 2058 | struct structure_field *field_entry; |
| 2059 | struct range_overlap_query query = { |
| 2060 | .range_start._signed = tag_value, |
| 2061 | .range_end._signed = tag_value, |
| 2062 | .mapping_name = 0, .overlaps = 0}; |
| 2063 | |
| 2064 | g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap, |
| 2065 | &query); |
| 2066 | if (!query.overlaps) { |
| 2067 | goto end; |
| 2068 | } |
| 2069 | |
| 2070 | field_name_quark = query.mapping_name; |
| 2071 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 2072 | GUINT_TO_POINTER(field_name_quark), NULL, &index)) { |
| 2073 | goto end; |
| 2074 | } |
| 2075 | |
| 2076 | field_entry = g_ptr_array_index(variant->fields, (size_t) index); |
| 2077 | type = field_entry->type; |
| 2078 | end: |
| 2079 | return type; |
| 2080 | } |
| 2081 | |
| 2082 | BT_HIDDEN |
| 2083 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( |
| 2084 | struct bt_ctf_field_type_variant *variant, |
| 2085 | uint64_t tag_value) |
| 2086 | { |
| 2087 | struct bt_ctf_field_type *type = NULL; |
| 2088 | GQuark field_name_quark; |
| 2089 | gpointer index; |
| 2090 | struct structure_field *field_entry; |
| 2091 | struct range_overlap_query query = { |
| 2092 | .range_start._unsigned = tag_value, |
| 2093 | .range_end._unsigned = tag_value, |
| 2094 | .mapping_name = 0, .overlaps = 0}; |
| 2095 | |
| 2096 | g_ptr_array_foreach(variant->tag->entries, |
| 2097 | check_ranges_overlap_unsigned, |
| 2098 | &query); |
| 2099 | if (!query.overlaps) { |
| 2100 | goto end; |
| 2101 | } |
| 2102 | |
| 2103 | field_name_quark = query.mapping_name; |
| 2104 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 2105 | GUINT_TO_POINTER(field_name_quark), NULL, &index)) { |
| 2106 | goto end; |
| 2107 | } |
| 2108 | |
| 2109 | field_entry = g_ptr_array_index(variant->fields, (size_t)index); |
| 2110 | type = field_entry->type; |
| 2111 | end: |
| 2112 | return type; |
| 2113 | } |
| 2114 | |
| 2115 | BT_HIDDEN |
| 2116 | int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, |
| 2117 | struct metadata_context *context) |
| 2118 | { |
| 2119 | int ret; |
| 2120 | |
| 2121 | if (!type || !context) { |
| 2122 | ret = -1; |
| 2123 | goto end; |
| 2124 | } |
| 2125 | |
| 2126 | ret = type->serialize(type, context); |
| 2127 | end: |
| 2128 | return ret; |
| 2129 | } |
| 2130 | |
| 2131 | BT_HIDDEN |
| 2132 | void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type, |
| 2133 | int byte_order) |
| 2134 | { |
| 2135 | if (!type) { |
| 2136 | return; |
| 2137 | } |
| 2138 | |
| 2139 | assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN); |
| 2140 | if (set_byte_order_funcs[type->declaration->id]) { |
| 2141 | set_byte_order_funcs[type->declaration->id](type, |
| 2142 | byte_order, 1); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | BT_HIDDEN |
| 2147 | struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type) |
| 2148 | { |
| 2149 | struct bt_ctf_field_type *copy = NULL; |
| 2150 | |
| 2151 | if (!type) { |
| 2152 | goto end; |
| 2153 | } |
| 2154 | |
| 2155 | copy = type_copy_funcs[type->declaration->id](type); |
| 2156 | end: |
| 2157 | return copy; |
| 2158 | } |
| 2159 | |
| 2160 | BT_HIDDEN |
| 2161 | struct bt_ctf_field_path *bt_ctf_field_path_create(void) |
| 2162 | { |
| 2163 | struct bt_ctf_field_path *field_path = NULL; |
| 2164 | |
| 2165 | field_path = g_new0(struct bt_ctf_field_path, 1); |
| 2166 | if (!field_path) { |
| 2167 | goto end; |
| 2168 | } |
| 2169 | |
| 2170 | field_path->root = CTF_NODE_UNKNOWN; |
| 2171 | field_path->path_indexes = g_array_new(TRUE, FALSE, sizeof(int)); |
| 2172 | if (!field_path->path_indexes) { |
| 2173 | bt_ctf_field_path_destroy(field_path); |
| 2174 | field_path = NULL; |
| 2175 | } |
| 2176 | end: |
| 2177 | return field_path; |
| 2178 | } |
| 2179 | |
| 2180 | |
| 2181 | BT_HIDDEN |
| 2182 | struct bt_ctf_field_path *bt_ctf_field_path_copy( |
| 2183 | struct bt_ctf_field_path *path) |
| 2184 | { |
| 2185 | struct bt_ctf_field_path *new_path = bt_ctf_field_path_create(); |
| 2186 | |
| 2187 | if (!new_path) { |
| 2188 | goto end; |
| 2189 | } |
| 2190 | |
| 2191 | new_path->root = path->root; |
| 2192 | g_array_insert_vals(new_path->path_indexes, 0, |
| 2193 | path->path_indexes->data, path->path_indexes->len); |
| 2194 | end: |
| 2195 | return new_path; |
| 2196 | } |
| 2197 | |
| 2198 | BT_HIDDEN |
| 2199 | void bt_ctf_field_path_destroy(struct bt_ctf_field_path *path) |
| 2200 | { |
| 2201 | if (!path) { |
| 2202 | return; |
| 2203 | } |
| 2204 | |
| 2205 | if (path->path_indexes) { |
| 2206 | g_array_free(path->path_indexes, TRUE); |
| 2207 | } |
| 2208 | g_free(path); |
| 2209 | } |
| 2210 | |
| 2211 | BT_HIDDEN |
| 2212 | int bt_ctf_field_type_structure_get_field_name_index( |
| 2213 | struct bt_ctf_field_type *type, const char *name) |
| 2214 | { |
| 2215 | int ret; |
| 2216 | size_t index; |
| 2217 | GQuark name_quark; |
| 2218 | struct bt_ctf_field_type_structure *structure; |
| 2219 | |
| 2220 | if (!type || !name || |
| 2221 | bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) { |
| 2222 | ret = -1; |
| 2223 | goto end; |
| 2224 | } |
| 2225 | |
| 2226 | name_quark = g_quark_try_string(name); |
| 2227 | if (!name_quark) { |
| 2228 | ret = -1; |
| 2229 | goto end; |
| 2230 | } |
| 2231 | |
| 2232 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 2233 | parent); |
| 2234 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, |
| 2235 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { |
| 2236 | ret = -1; |
| 2237 | goto end; |
| 2238 | } |
| 2239 | ret = (int) index; |
| 2240 | end: |
| 2241 | return ret; |
| 2242 | } |
| 2243 | |
| 2244 | BT_HIDDEN |
| 2245 | int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type *type, |
| 2246 | struct bt_ctf_field_type *field, int index) |
| 2247 | { |
| 2248 | int ret = 0; |
| 2249 | struct bt_ctf_field_type_structure *structure; |
| 2250 | |
| 2251 | if (!type || !field || |
| 2252 | bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) { |
| 2253 | ret = -1; |
| 2254 | goto end; |
| 2255 | } |
| 2256 | |
| 2257 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 2258 | parent); |
| 2259 | if (index < 0 || index >= structure->fields->len) { |
| 2260 | ret = -1; |
| 2261 | goto end; |
| 2262 | } |
| 2263 | |
| 2264 | bt_get(field); |
| 2265 | bt_put(((struct structure_field *) |
| 2266 | g_ptr_array_index(structure->fields, index))->type); |
| 2267 | ((struct structure_field *) structure->fields->pdata[index])->type = |
| 2268 | field; |
| 2269 | end: |
| 2270 | return ret; |
| 2271 | } |
| 2272 | |
| 2273 | BT_HIDDEN |
| 2274 | int bt_ctf_field_type_variant_get_field_name_index( |
| 2275 | struct bt_ctf_field_type *type, const char *name) |
| 2276 | { |
| 2277 | int ret; |
| 2278 | size_t index; |
| 2279 | GQuark name_quark; |
| 2280 | struct bt_ctf_field_type_variant *variant; |
| 2281 | |
| 2282 | if (!type || !name || |
| 2283 | bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) { |
| 2284 | ret = -1; |
| 2285 | goto end; |
| 2286 | } |
| 2287 | |
| 2288 | name_quark = g_quark_try_string(name); |
| 2289 | if (!name_quark) { |
| 2290 | ret = -1; |
| 2291 | goto end; |
| 2292 | } |
| 2293 | |
| 2294 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2295 | parent); |
| 2296 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, |
| 2297 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { |
| 2298 | ret = -1; |
| 2299 | goto end; |
| 2300 | } |
| 2301 | ret = (int) index; |
| 2302 | end: |
| 2303 | return ret; |
| 2304 | } |
| 2305 | |
| 2306 | BT_HIDDEN |
| 2307 | int bt_ctf_field_type_sequence_set_length_field_path( |
| 2308 | struct bt_ctf_field_type *type, |
| 2309 | struct bt_ctf_field_path *path) |
| 2310 | { |
| 2311 | int ret = 0; |
| 2312 | struct bt_ctf_field_type_sequence *sequence; |
| 2313 | |
| 2314 | if (!type || bt_ctf_field_type_get_type_id(type) != CTF_TYPE_SEQUENCE) { |
| 2315 | ret = -1; |
| 2316 | goto end; |
| 2317 | } |
| 2318 | |
| 2319 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 2320 | parent); |
| 2321 | if (sequence->length_field_path) { |
| 2322 | bt_ctf_field_path_destroy(sequence->length_field_path); |
| 2323 | } |
| 2324 | sequence->length_field_path = path; |
| 2325 | end: |
| 2326 | return ret; |
| 2327 | } |
| 2328 | |
| 2329 | BT_HIDDEN |
| 2330 | struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path( |
| 2331 | struct bt_ctf_field_type *type) |
| 2332 | { |
| 2333 | struct bt_ctf_field_type_sequence *sequence; |
| 2334 | |
| 2335 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 2336 | parent); |
| 2337 | |
| 2338 | return sequence->length_field_path; |
| 2339 | } |
| 2340 | |
| 2341 | BT_HIDDEN |
| 2342 | int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, |
| 2343 | struct bt_ctf_field_path *path) |
| 2344 | { |
| 2345 | int ret = 0; |
| 2346 | struct bt_ctf_field_type_variant *variant; |
| 2347 | |
| 2348 | if (!type || bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) { |
| 2349 | ret = -1; |
| 2350 | goto end; |
| 2351 | } |
| 2352 | |
| 2353 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2354 | parent); |
| 2355 | if (variant->tag_path) { |
| 2356 | bt_ctf_field_path_destroy(variant->tag_path); |
| 2357 | } |
| 2358 | variant->tag_path = path; |
| 2359 | end: |
| 2360 | return ret; |
| 2361 | } |
| 2362 | |
| 2363 | BT_HIDDEN |
| 2364 | struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path( |
| 2365 | struct bt_ctf_field_type *type) |
| 2366 | { |
| 2367 | struct bt_ctf_field_type_variant *variant; |
| 2368 | |
| 2369 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2370 | parent); |
| 2371 | |
| 2372 | return variant->tag_path; |
| 2373 | } |
| 2374 | |
| 2375 | BT_HIDDEN |
| 2376 | int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type *type, |
| 2377 | struct bt_ctf_field_type *tag) |
| 2378 | { |
| 2379 | int ret = 0; |
| 2380 | struct bt_ctf_field_type_variant *variant; |
| 2381 | |
| 2382 | if (!type || !tag || type->frozen || |
| 2383 | bt_ctf_field_type_get_type_id(tag) != CTF_TYPE_ENUM) { |
| 2384 | ret = -1; |
| 2385 | goto end; |
| 2386 | } |
| 2387 | |
| 2388 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2389 | parent); |
| 2390 | bt_get(tag); |
| 2391 | if (variant->tag) { |
| 2392 | bt_put(&variant->tag->parent); |
| 2393 | } |
| 2394 | variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration, |
| 2395 | parent); |
| 2396 | end: |
| 2397 | return ret; |
| 2398 | } |
| 2399 | |
| 2400 | BT_HIDDEN |
| 2401 | int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type *type, |
| 2402 | struct bt_ctf_field_type *field, int index) |
| 2403 | { |
| 2404 | int ret = 0; |
| 2405 | struct bt_ctf_field_type_variant *variant; |
| 2406 | |
| 2407 | if (!type || !field || |
| 2408 | bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) { |
| 2409 | ret = -1; |
| 2410 | goto end; |
| 2411 | } |
| 2412 | |
| 2413 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 2414 | parent); |
| 2415 | if (index < 0 || index >= variant->fields->len) { |
| 2416 | ret = -1; |
| 2417 | goto end; |
| 2418 | } |
| 2419 | |
| 2420 | bt_get(field); |
| 2421 | bt_put(((struct structure_field *) |
| 2422 | g_ptr_array_index(variant->fields, index))->type); |
| 2423 | ((struct structure_field *) variant->fields->pdata[index])->type = |
| 2424 | field; |
| 2425 | end: |
| 2426 | return ret; |
| 2427 | } |
| 2428 | |
| 2429 | static |
| 2430 | void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type) |
| 2431 | { |
| 2432 | struct bt_ctf_field_type_integer *integer = |
| 2433 | (struct bt_ctf_field_type_integer *) type; |
| 2434 | |
| 2435 | if (!type) { |
| 2436 | return; |
| 2437 | } |
| 2438 | |
| 2439 | bt_put(integer->mapped_clock); |
| 2440 | g_free(integer); |
| 2441 | } |
| 2442 | |
| 2443 | static |
| 2444 | void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type) |
| 2445 | { |
| 2446 | struct bt_ctf_field_type_enumeration *enumeration = |
| 2447 | (struct bt_ctf_field_type_enumeration *) type; |
| 2448 | |
| 2449 | if (!type) { |
| 2450 | return; |
| 2451 | } |
| 2452 | |
| 2453 | g_ptr_array_free(enumeration->entries, TRUE); |
| 2454 | bt_put(enumeration->container); |
| 2455 | g_free(enumeration); |
| 2456 | } |
| 2457 | |
| 2458 | static |
| 2459 | void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type) |
| 2460 | { |
| 2461 | struct bt_ctf_field_type_floating_point *floating_point = |
| 2462 | (struct bt_ctf_field_type_floating_point *) type; |
| 2463 | |
| 2464 | if (!type) { |
| 2465 | return; |
| 2466 | } |
| 2467 | |
| 2468 | g_free(floating_point); |
| 2469 | } |
| 2470 | |
| 2471 | static |
| 2472 | void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type) |
| 2473 | { |
| 2474 | struct bt_ctf_field_type_structure *structure = |
| 2475 | (struct bt_ctf_field_type_structure *) type; |
| 2476 | |
| 2477 | if (!type) { |
| 2478 | return; |
| 2479 | } |
| 2480 | |
| 2481 | g_ptr_array_free(structure->fields, TRUE); |
| 2482 | g_hash_table_destroy(structure->field_name_to_index); |
| 2483 | g_free(structure); |
| 2484 | } |
| 2485 | |
| 2486 | static |
| 2487 | void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type) |
| 2488 | { |
| 2489 | struct bt_ctf_field_type_variant *variant = |
| 2490 | (struct bt_ctf_field_type_variant *) type; |
| 2491 | |
| 2492 | if (!type) { |
| 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | g_ptr_array_free(variant->fields, TRUE); |
| 2497 | g_hash_table_destroy(variant->field_name_to_index); |
| 2498 | g_string_free(variant->tag_name, TRUE); |
| 2499 | bt_put(&variant->tag->parent); |
| 2500 | bt_ctf_field_path_destroy(variant->tag_path); |
| 2501 | g_free(variant); |
| 2502 | } |
| 2503 | |
| 2504 | static |
| 2505 | void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type) |
| 2506 | { |
| 2507 | struct bt_ctf_field_type_array *array = |
| 2508 | (struct bt_ctf_field_type_array *) type; |
| 2509 | |
| 2510 | if (!type) { |
| 2511 | return; |
| 2512 | } |
| 2513 | |
| 2514 | bt_put(array->element_type); |
| 2515 | g_free(array); |
| 2516 | } |
| 2517 | |
| 2518 | static |
| 2519 | void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type) |
| 2520 | { |
| 2521 | struct bt_ctf_field_type_sequence *sequence = |
| 2522 | (struct bt_ctf_field_type_sequence *) type; |
| 2523 | |
| 2524 | if (!type) { |
| 2525 | return; |
| 2526 | } |
| 2527 | |
| 2528 | bt_put(sequence->element_type); |
| 2529 | g_string_free(sequence->length_field_name, TRUE); |
| 2530 | bt_ctf_field_path_destroy(sequence->length_field_path); |
| 2531 | g_free(sequence); |
| 2532 | } |
| 2533 | |
| 2534 | static |
| 2535 | void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type) |
| 2536 | { |
| 2537 | struct bt_ctf_field_type_string *string = |
| 2538 | (struct bt_ctf_field_type_string *) type; |
| 2539 | |
| 2540 | if (!type) { |
| 2541 | return; |
| 2542 | } |
| 2543 | |
| 2544 | g_free(string); |
| 2545 | } |
| 2546 | |
| 2547 | static |
| 2548 | void generic_field_type_freeze(struct bt_ctf_field_type *type) |
| 2549 | { |
| 2550 | type->frozen = 1; |
| 2551 | } |
| 2552 | |
| 2553 | static |
| 2554 | void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type) |
| 2555 | { |
| 2556 | struct bt_ctf_field_type_enumeration *enumeration_type = container_of( |
| 2557 | type, struct bt_ctf_field_type_enumeration, parent); |
| 2558 | |
| 2559 | generic_field_type_freeze(type); |
| 2560 | bt_ctf_field_type_freeze(enumeration_type->container); |
| 2561 | } |
| 2562 | |
| 2563 | static |
| 2564 | void freeze_structure_field(struct structure_field *field) |
| 2565 | { |
| 2566 | bt_ctf_field_type_freeze(field->type); |
| 2567 | } |
| 2568 | |
| 2569 | static |
| 2570 | void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type) |
| 2571 | { |
| 2572 | struct bt_ctf_field_type_structure *structure_type = container_of( |
| 2573 | type, struct bt_ctf_field_type_structure, parent); |
| 2574 | |
| 2575 | /* Cache the alignment */ |
| 2576 | type->declaration->alignment = bt_ctf_field_type_get_alignment(type); |
| 2577 | generic_field_type_freeze(type); |
| 2578 | g_ptr_array_foreach(structure_type->fields, |
| 2579 | (GFunc) freeze_structure_field, NULL); |
| 2580 | } |
| 2581 | |
| 2582 | static |
| 2583 | void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type) |
| 2584 | { |
| 2585 | struct bt_ctf_field_type_variant *variant_type = container_of( |
| 2586 | type, struct bt_ctf_field_type_variant, parent); |
| 2587 | |
| 2588 | generic_field_type_freeze(type); |
| 2589 | g_ptr_array_foreach(variant_type->fields, |
| 2590 | (GFunc) freeze_structure_field, NULL); |
| 2591 | } |
| 2592 | |
| 2593 | static |
| 2594 | void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type) |
| 2595 | { |
| 2596 | struct bt_ctf_field_type_array *array_type = container_of( |
| 2597 | type, struct bt_ctf_field_type_array, parent); |
| 2598 | |
| 2599 | /* Cache the alignment */ |
| 2600 | type->declaration->alignment = bt_ctf_field_type_get_alignment(type); |
| 2601 | generic_field_type_freeze(type); |
| 2602 | bt_ctf_field_type_freeze(array_type->element_type); |
| 2603 | } |
| 2604 | |
| 2605 | static |
| 2606 | void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type) |
| 2607 | { |
| 2608 | struct bt_ctf_field_type_sequence *sequence_type = container_of( |
| 2609 | type, struct bt_ctf_field_type_sequence, parent); |
| 2610 | |
| 2611 | /* Cache the alignment */ |
| 2612 | type->declaration->alignment = bt_ctf_field_type_get_alignment(type); |
| 2613 | generic_field_type_freeze(type); |
| 2614 | bt_ctf_field_type_freeze(sequence_type->element_type); |
| 2615 | } |
| 2616 | |
| 2617 | static |
| 2618 | const char *get_encoding_string(enum ctf_string_encoding encoding) |
| 2619 | { |
| 2620 | const char *encoding_string; |
| 2621 | |
| 2622 | switch (encoding) { |
| 2623 | case CTF_STRING_NONE: |
| 2624 | encoding_string = "none"; |
| 2625 | break; |
| 2626 | case CTF_STRING_ASCII: |
| 2627 | encoding_string = "ASCII"; |
| 2628 | break; |
| 2629 | case CTF_STRING_UTF8: |
| 2630 | encoding_string = "UTF8"; |
| 2631 | break; |
| 2632 | default: |
| 2633 | encoding_string = "unknown"; |
| 2634 | break; |
| 2635 | } |
| 2636 | |
| 2637 | return encoding_string; |
| 2638 | } |
| 2639 | |
| 2640 | static |
| 2641 | const char *get_integer_base_string(enum bt_ctf_integer_base base) |
| 2642 | { |
| 2643 | const char *base_string; |
| 2644 | |
| 2645 | switch (base) { |
| 2646 | case BT_CTF_INTEGER_BASE_DECIMAL: |
| 2647 | base_string = "decimal"; |
| 2648 | break; |
| 2649 | case BT_CTF_INTEGER_BASE_HEXADECIMAL: |
| 2650 | base_string = "hexadecimal"; |
| 2651 | break; |
| 2652 | case BT_CTF_INTEGER_BASE_OCTAL: |
| 2653 | base_string = "octal"; |
| 2654 | break; |
| 2655 | case BT_CTF_INTEGER_BASE_BINARY: |
| 2656 | base_string = "binary"; |
| 2657 | break; |
| 2658 | default: |
| 2659 | base_string = "unknown"; |
| 2660 | break; |
| 2661 | } |
| 2662 | |
| 2663 | return base_string; |
| 2664 | } |
| 2665 | |
| 2666 | static |
| 2667 | int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type, |
| 2668 | struct metadata_context *context) |
| 2669 | { |
| 2670 | struct bt_ctf_field_type_integer *integer = container_of(type, |
| 2671 | struct bt_ctf_field_type_integer, parent); |
| 2672 | int ret = 0; |
| 2673 | |
| 2674 | g_string_append_printf(context->string, |
| 2675 | "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s", |
| 2676 | integer->declaration.len, type->declaration->alignment, |
| 2677 | (integer->declaration.signedness ? "true" : "false"), |
| 2678 | get_encoding_string(integer->declaration.encoding), |
| 2679 | get_integer_base_string(integer->declaration.base), |
| 2680 | get_byte_order_string(integer->declaration.byte_order)); |
| 2681 | if (integer->mapped_clock) { |
| 2682 | const char *clock_name = bt_ctf_clock_get_name( |
| 2683 | integer->mapped_clock); |
| 2684 | |
| 2685 | if (!clock_name) { |
| 2686 | ret = -1; |
| 2687 | goto end; |
| 2688 | } |
| 2689 | |
| 2690 | g_string_append_printf(context->string, |
| 2691 | "; map = clock.%s.value", clock_name); |
| 2692 | } |
| 2693 | |
| 2694 | g_string_append(context->string, "; }"); |
| 2695 | end: |
| 2696 | return ret; |
| 2697 | } |
| 2698 | |
| 2699 | static |
| 2700 | int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type, |
| 2701 | struct metadata_context *context) |
| 2702 | { |
| 2703 | size_t entry; |
| 2704 | int ret; |
| 2705 | struct bt_ctf_field_type_enumeration *enumeration = container_of(type, |
| 2706 | struct bt_ctf_field_type_enumeration, parent); |
| 2707 | struct bt_ctf_field_type *container_type; |
| 2708 | int container_signed; |
| 2709 | |
| 2710 | ret = bt_ctf_field_type_validate(type); |
| 2711 | if (ret) { |
| 2712 | goto end; |
| 2713 | } |
| 2714 | |
| 2715 | container_type = bt_ctf_field_type_enumeration_get_container_type(type); |
| 2716 | if (!container_type) { |
| 2717 | ret = -1; |
| 2718 | goto end; |
| 2719 | } |
| 2720 | |
| 2721 | container_signed = bt_ctf_field_type_integer_get_signed(container_type); |
| 2722 | if (container_signed < 0) { |
| 2723 | ret = container_signed; |
| 2724 | goto error_put_container_type; |
| 2725 | } |
| 2726 | |
| 2727 | g_string_append(context->string, "enum : "); |
| 2728 | ret = bt_ctf_field_type_serialize(enumeration->container, context); |
| 2729 | if (ret) { |
| 2730 | goto error_put_container_type; |
| 2731 | } |
| 2732 | |
| 2733 | g_string_append(context->string, " { "); |
| 2734 | for (entry = 0; entry < enumeration->entries->len; entry++) { |
| 2735 | struct enumeration_mapping *mapping = |
| 2736 | enumeration->entries->pdata[entry]; |
| 2737 | |
| 2738 | if (container_signed) { |
| 2739 | if (mapping->range_start._signed == |
| 2740 | mapping->range_end._signed) { |
| 2741 | g_string_append_printf(context->string, |
| 2742 | "\"%s\" = %" PRId64, |
| 2743 | g_quark_to_string(mapping->string), |
| 2744 | mapping->range_start._signed); |
| 2745 | } else { |
| 2746 | g_string_append_printf(context->string, |
| 2747 | "\"%s\" = %" PRId64 " ... %" PRId64, |
| 2748 | g_quark_to_string(mapping->string), |
| 2749 | mapping->range_start._signed, |
| 2750 | mapping->range_end._signed); |
| 2751 | } |
| 2752 | } else { |
| 2753 | if (mapping->range_start._unsigned == |
| 2754 | mapping->range_end._unsigned) { |
| 2755 | g_string_append_printf(context->string, |
| 2756 | "\"%s\" = %" PRIu64, |
| 2757 | g_quark_to_string(mapping->string), |
| 2758 | mapping->range_start._unsigned); |
| 2759 | } else { |
| 2760 | g_string_append_printf(context->string, |
| 2761 | "\"%s\" = %" PRIu64 " ... %" PRIu64, |
| 2762 | g_quark_to_string(mapping->string), |
| 2763 | mapping->range_start._unsigned, |
| 2764 | mapping->range_end._unsigned); |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | g_string_append(context->string, |
| 2769 | ((entry != (enumeration->entries->len - 1)) ? |
| 2770 | ", " : " }")); |
| 2771 | } |
| 2772 | |
| 2773 | if (context->field_name->len) { |
| 2774 | g_string_append_printf(context->string, " %s", |
| 2775 | context->field_name->str); |
| 2776 | g_string_assign(context->field_name, ""); |
| 2777 | } |
| 2778 | error_put_container_type: |
| 2779 | bt_put(container_type); |
| 2780 | end: |
| 2781 | return ret; |
| 2782 | } |
| 2783 | |
| 2784 | static |
| 2785 | int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type, |
| 2786 | struct metadata_context *context) |
| 2787 | { |
| 2788 | struct bt_ctf_field_type_floating_point *floating_point = container_of( |
| 2789 | type, struct bt_ctf_field_type_floating_point, parent); |
| 2790 | |
| 2791 | g_string_append_printf(context->string, |
| 2792 | "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }", |
| 2793 | floating_point->declaration.exp->len, |
| 2794 | floating_point->declaration.mantissa->len + 1, |
| 2795 | get_byte_order_string(floating_point->declaration.byte_order), |
| 2796 | type->declaration->alignment); |
| 2797 | return 0; |
| 2798 | } |
| 2799 | |
| 2800 | static |
| 2801 | int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type, |
| 2802 | struct metadata_context *context) |
| 2803 | { |
| 2804 | size_t i; |
| 2805 | unsigned int indent; |
| 2806 | int ret = 0; |
| 2807 | struct bt_ctf_field_type_structure *structure = container_of(type, |
| 2808 | struct bt_ctf_field_type_structure, parent); |
| 2809 | GString *structure_field_name = context->field_name; |
| 2810 | |
| 2811 | context->field_name = g_string_new(""); |
| 2812 | |
| 2813 | context->current_indentation_level++; |
| 2814 | g_string_append(context->string, "struct {\n"); |
| 2815 | |
| 2816 | for (i = 0; i < structure->fields->len; i++) { |
| 2817 | struct structure_field *field; |
| 2818 | |
| 2819 | for (indent = 0; indent < context->current_indentation_level; |
| 2820 | indent++) { |
| 2821 | g_string_append_c(context->string, '\t'); |
| 2822 | } |
| 2823 | |
| 2824 | field = structure->fields->pdata[i]; |
| 2825 | g_string_assign(context->field_name, |
| 2826 | g_quark_to_string(field->name)); |
| 2827 | ret = bt_ctf_field_type_serialize(field->type, context); |
| 2828 | if (ret) { |
| 2829 | goto end; |
| 2830 | } |
| 2831 | |
| 2832 | if (context->field_name->len) { |
| 2833 | g_string_append_printf(context->string, " %s", |
| 2834 | context->field_name->str); |
| 2835 | } |
| 2836 | g_string_append(context->string, ";\n"); |
| 2837 | } |
| 2838 | |
| 2839 | context->current_indentation_level--; |
| 2840 | for (indent = 0; indent < context->current_indentation_level; |
| 2841 | indent++) { |
| 2842 | g_string_append_c(context->string, '\t'); |
| 2843 | } |
| 2844 | |
| 2845 | g_string_append_printf(context->string, "} align(%zu)", |
| 2846 | type->declaration->alignment); |
| 2847 | end: |
| 2848 | g_string_free(context->field_name, TRUE); |
| 2849 | context->field_name = structure_field_name; |
| 2850 | return ret; |
| 2851 | } |
| 2852 | |
| 2853 | static |
| 2854 | int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type, |
| 2855 | struct metadata_context *context) |
| 2856 | { |
| 2857 | size_t i; |
| 2858 | unsigned int indent; |
| 2859 | int ret = 0; |
| 2860 | struct bt_ctf_field_type_variant *variant = container_of( |
| 2861 | type, struct bt_ctf_field_type_variant, parent); |
| 2862 | GString *variant_field_name = context->field_name; |
| 2863 | |
| 2864 | context->field_name = g_string_new(""); |
| 2865 | if (variant->tag_name->len > 0) { |
| 2866 | g_string_append_printf(context->string, |
| 2867 | "variant <%s> {\n", variant->tag_name->str); |
| 2868 | } else { |
| 2869 | g_string_append(context->string, "variant {\n"); |
| 2870 | } |
| 2871 | |
| 2872 | context->current_indentation_level++; |
| 2873 | for (i = 0; i < variant->fields->len; i++) { |
| 2874 | struct structure_field *field = variant->fields->pdata[i]; |
| 2875 | |
| 2876 | g_string_assign(context->field_name, |
| 2877 | g_quark_to_string(field->name)); |
| 2878 | for (indent = 0; indent < context->current_indentation_level; |
| 2879 | indent++) { |
| 2880 | g_string_append_c(context->string, '\t'); |
| 2881 | } |
| 2882 | |
| 2883 | g_string_assign(context->field_name, |
| 2884 | g_quark_to_string(field->name)); |
| 2885 | ret = bt_ctf_field_type_serialize(field->type, context); |
| 2886 | if (ret) { |
| 2887 | goto end; |
| 2888 | } |
| 2889 | |
| 2890 | if (context->field_name->len) { |
| 2891 | g_string_append_printf(context->string, " %s;", |
| 2892 | context->field_name->str); |
| 2893 | } |
| 2894 | |
| 2895 | g_string_append_c(context->string, '\n'); |
| 2896 | } |
| 2897 | |
| 2898 | context->current_indentation_level--; |
| 2899 | for (indent = 0; indent < context->current_indentation_level; |
| 2900 | indent++) { |
| 2901 | g_string_append_c(context->string, '\t'); |
| 2902 | } |
| 2903 | |
| 2904 | g_string_append(context->string, "}"); |
| 2905 | end: |
| 2906 | g_string_free(context->field_name, TRUE); |
| 2907 | context->field_name = variant_field_name; |
| 2908 | return ret; |
| 2909 | } |
| 2910 | |
| 2911 | static |
| 2912 | int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type, |
| 2913 | struct metadata_context *context) |
| 2914 | { |
| 2915 | int ret = 0; |
| 2916 | struct bt_ctf_field_type_array *array = container_of(type, |
| 2917 | struct bt_ctf_field_type_array, parent); |
| 2918 | |
| 2919 | ret = bt_ctf_field_type_serialize(array->element_type, context); |
| 2920 | if (ret) { |
| 2921 | goto end; |
| 2922 | } |
| 2923 | |
| 2924 | if (context->field_name->len) { |
| 2925 | g_string_append_printf(context->string, " %s[%u]", |
| 2926 | context->field_name->str, array->length); |
| 2927 | g_string_assign(context->field_name, ""); |
| 2928 | } else { |
| 2929 | g_string_append_printf(context->string, "[%u]", array->length); |
| 2930 | } |
| 2931 | end: |
| 2932 | return ret; |
| 2933 | } |
| 2934 | |
| 2935 | static |
| 2936 | int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type, |
| 2937 | struct metadata_context *context) |
| 2938 | { |
| 2939 | int ret = 0; |
| 2940 | struct bt_ctf_field_type_sequence *sequence = container_of( |
| 2941 | type, struct bt_ctf_field_type_sequence, parent); |
| 2942 | |
| 2943 | ret = bt_ctf_field_type_serialize(sequence->element_type, context); |
| 2944 | if (ret) { |
| 2945 | goto end; |
| 2946 | } |
| 2947 | |
| 2948 | if (context->field_name->len) { |
| 2949 | g_string_append_printf(context->string, " %s[%s]", |
| 2950 | context->field_name->str, |
| 2951 | sequence->length_field_name->str); |
| 2952 | g_string_assign(context->field_name, ""); |
| 2953 | } else { |
| 2954 | g_string_append_printf(context->string, "[%s]", |
| 2955 | sequence->length_field_name->str); |
| 2956 | } |
| 2957 | end: |
| 2958 | return ret; |
| 2959 | } |
| 2960 | |
| 2961 | static |
| 2962 | int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type, |
| 2963 | struct metadata_context *context) |
| 2964 | { |
| 2965 | struct bt_ctf_field_type_string *string = container_of( |
| 2966 | type, struct bt_ctf_field_type_string, parent); |
| 2967 | |
| 2968 | g_string_append_printf(context->string, |
| 2969 | "string { encoding = %s; }", |
| 2970 | get_encoding_string(string->declaration.encoding)); |
| 2971 | return 0; |
| 2972 | } |
| 2973 | |
| 2974 | static |
| 2975 | enum bt_ctf_byte_order get_ctf_ir_byte_order(int byte_order) { |
| 2976 | enum bt_ctf_byte_order ret; |
| 2977 | |
| 2978 | switch (byte_order) { |
| 2979 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 2980 | case LITTLE_ENDIAN: |
| 2981 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; |
| 2982 | break; |
| 2983 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 2984 | case BIG_ENDIAN: |
| 2985 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; |
| 2986 | break; |
| 2987 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 2988 | ret = BT_CTF_BYTE_ORDER_NETWORK; |
| 2989 | break; |
| 2990 | case BT_CTF_BYTE_ORDER_NATIVE: |
| 2991 | ret = BT_CTF_BYTE_ORDER_NATIVE; |
| 2992 | break; |
| 2993 | default: |
| 2994 | ret = BT_CTF_BYTE_ORDER_UNKNOWN; |
| 2995 | break; |
| 2996 | } |
| 2997 | |
| 2998 | return ret; |
| 2999 | } |
| 3000 | |
| 3001 | static |
| 3002 | void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type, |
| 3003 | int byte_order, int set_native) |
| 3004 | { |
| 3005 | struct bt_ctf_field_type_integer *integer_type = container_of(type, |
| 3006 | struct bt_ctf_field_type_integer, parent); |
| 3007 | |
| 3008 | if (set_native) { |
| 3009 | if (integer_type->user_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
| 3010 | /* |
| 3011 | * User byte order is native, so we can set |
| 3012 | * the real byte order. |
| 3013 | */ |
| 3014 | integer_type->declaration.byte_order = |
| 3015 | byte_order; |
| 3016 | } |
| 3017 | } else { |
| 3018 | integer_type->user_byte_order = |
| 3019 | get_ctf_ir_byte_order(byte_order); |
| 3020 | integer_type->declaration.byte_order = byte_order; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | static |
| 3025 | void bt_ctf_field_type_enumeration_set_byte_order( |
| 3026 | struct bt_ctf_field_type *type, int byte_order, int set_native) |
| 3027 | { |
| 3028 | struct bt_ctf_field_type_enumeration *enum_type = container_of(type, |
| 3029 | struct bt_ctf_field_type_enumeration, parent); |
| 3030 | |
| 3031 | /* Safe to assume that container is an integer */ |
| 3032 | bt_ctf_field_type_integer_set_byte_order(enum_type->container, |
| 3033 | byte_order, set_native); |
| 3034 | } |
| 3035 | |
| 3036 | static |
| 3037 | void bt_ctf_field_type_floating_point_set_byte_order( |
| 3038 | struct bt_ctf_field_type *type, int byte_order, int set_native) |
| 3039 | { |
| 3040 | struct bt_ctf_field_type_floating_point *floating_point_type = |
| 3041 | container_of(type, struct bt_ctf_field_type_floating_point, |
| 3042 | parent); |
| 3043 | |
| 3044 | if (set_native) { |
| 3045 | if (floating_point_type->user_byte_order == |
| 3046 | BT_CTF_BYTE_ORDER_NATIVE) { |
| 3047 | /* |
| 3048 | * User byte order is native, so we can set |
| 3049 | * the real byte order. |
| 3050 | */ |
| 3051 | floating_point_type->declaration.byte_order = |
| 3052 | byte_order; |
| 3053 | floating_point_type->sign.byte_order = |
| 3054 | byte_order; |
| 3055 | floating_point_type->mantissa.byte_order = |
| 3056 | byte_order; |
| 3057 | floating_point_type->exp.byte_order = |
| 3058 | byte_order; |
| 3059 | } |
| 3060 | } else { |
| 3061 | floating_point_type->user_byte_order = |
| 3062 | get_ctf_ir_byte_order(byte_order); |
| 3063 | floating_point_type->declaration.byte_order = byte_order; |
| 3064 | floating_point_type->sign.byte_order = byte_order; |
| 3065 | floating_point_type->mantissa.byte_order = byte_order; |
| 3066 | floating_point_type->exp.byte_order = byte_order; |
| 3067 | } |
| 3068 | } |
| 3069 | |
| 3070 | static |
| 3071 | void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type, |
| 3072 | int byte_order, int set_native) |
| 3073 | { |
| 3074 | int i; |
| 3075 | struct bt_ctf_field_type_structure *structure_type = |
| 3076 | container_of(type, struct bt_ctf_field_type_structure, |
| 3077 | parent); |
| 3078 | |
| 3079 | for (i = 0; i < structure_type->fields->len; i++) { |
| 3080 | struct structure_field *field = g_ptr_array_index( |
| 3081 | structure_type->fields, i); |
| 3082 | struct bt_ctf_field_type *field_type = field->type; |
| 3083 | |
| 3084 | if (set_byte_order_funcs[field_type->declaration->id]) { |
| 3085 | set_byte_order_funcs[field_type->declaration->id]( |
| 3086 | field_type, byte_order, set_native); |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | |
| 3091 | static |
| 3092 | void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type, |
| 3093 | int byte_order, int set_native) |
| 3094 | { |
| 3095 | int i; |
| 3096 | struct bt_ctf_field_type_variant *variant_type = |
| 3097 | container_of(type, struct bt_ctf_field_type_variant, |
| 3098 | parent); |
| 3099 | |
| 3100 | for (i = 0; i < variant_type->fields->len; i++) { |
| 3101 | struct structure_field *field = g_ptr_array_index( |
| 3102 | variant_type->fields, i); |
| 3103 | struct bt_ctf_field_type *field_type = field->type; |
| 3104 | |
| 3105 | if (set_byte_order_funcs[field_type->declaration->id]) { |
| 3106 | set_byte_order_funcs[field_type->declaration->id]( |
| 3107 | field_type, byte_order, set_native); |
| 3108 | } |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | static |
| 3113 | void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type, |
| 3114 | int byte_order, int set_native) |
| 3115 | { |
| 3116 | struct bt_ctf_field_type_array *array_type = |
| 3117 | container_of(type, struct bt_ctf_field_type_array, |
| 3118 | parent); |
| 3119 | |
| 3120 | if (set_byte_order_funcs[array_type->element_type->declaration->id]) { |
| 3121 | set_byte_order_funcs[array_type->element_type->declaration->id]( |
| 3122 | array_type->element_type, byte_order, set_native); |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | static |
| 3127 | void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type, |
| 3128 | int byte_order, int set_native) |
| 3129 | { |
| 3130 | struct bt_ctf_field_type_sequence *sequence_type = |
| 3131 | container_of(type, struct bt_ctf_field_type_sequence, |
| 3132 | parent); |
| 3133 | |
| 3134 | if (set_byte_order_funcs[ |
| 3135 | sequence_type->element_type->declaration->id]) { |
| 3136 | set_byte_order_funcs[ |
| 3137 | sequence_type->element_type->declaration->id]( |
| 3138 | sequence_type->element_type, byte_order, set_native); |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | static |
| 3143 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( |
| 3144 | struct bt_ctf_field_type *type) |
| 3145 | { |
| 3146 | struct bt_ctf_field_type *copy; |
| 3147 | struct bt_ctf_field_type_integer *integer, *copy_integer; |
| 3148 | |
| 3149 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); |
| 3150 | copy = bt_ctf_field_type_integer_create(integer->declaration.len); |
| 3151 | if (!copy) { |
| 3152 | goto end; |
| 3153 | } |
| 3154 | |
| 3155 | copy_integer = container_of(copy, struct bt_ctf_field_type_integer, |
| 3156 | parent); |
| 3157 | copy_integer->declaration = integer->declaration; |
| 3158 | if (integer->mapped_clock) { |
| 3159 | bt_get(integer->mapped_clock); |
| 3160 | copy_integer->mapped_clock = integer->mapped_clock; |
| 3161 | } |
| 3162 | |
| 3163 | copy_integer->user_byte_order = integer->user_byte_order; |
| 3164 | |
| 3165 | end: |
| 3166 | return copy; |
| 3167 | } |
| 3168 | |
| 3169 | static |
| 3170 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( |
| 3171 | struct bt_ctf_field_type *type) |
| 3172 | { |
| 3173 | size_t i; |
| 3174 | struct bt_ctf_field_type *copy = NULL, *copy_container; |
| 3175 | struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration; |
| 3176 | |
| 3177 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, |
| 3178 | parent); |
| 3179 | |
| 3180 | /* Copy the source enumeration's container */ |
| 3181 | copy_container = bt_ctf_field_type_copy(enumeration->container); |
| 3182 | if (!copy_container) { |
| 3183 | goto end; |
| 3184 | } |
| 3185 | |
| 3186 | copy = bt_ctf_field_type_enumeration_create(copy_container); |
| 3187 | if (!copy) { |
| 3188 | goto end; |
| 3189 | } |
| 3190 | copy_enumeration = container_of(copy, |
| 3191 | struct bt_ctf_field_type_enumeration, parent); |
| 3192 | |
| 3193 | /* Copy all enumaration entries */ |
| 3194 | for (i = 0; i < enumeration->entries->len; i++) { |
| 3195 | struct enumeration_mapping *mapping = g_ptr_array_index( |
| 3196 | enumeration->entries, i); |
| 3197 | struct enumeration_mapping* copy_mapping = g_new0( |
| 3198 | struct enumeration_mapping, 1); |
| 3199 | |
| 3200 | if (!copy_mapping) { |
| 3201 | goto error; |
| 3202 | } |
| 3203 | |
| 3204 | *copy_mapping = *mapping; |
| 3205 | g_ptr_array_add(copy_enumeration->entries, copy_mapping); |
| 3206 | } |
| 3207 | |
| 3208 | copy_enumeration->declaration = enumeration->declaration; |
| 3209 | end: |
| 3210 | bt_put(copy_container); |
| 3211 | return copy; |
| 3212 | error: |
| 3213 | bt_put(copy_container); |
| 3214 | BT_PUT(copy); |
| 3215 | return copy; |
| 3216 | } |
| 3217 | |
| 3218 | static |
| 3219 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( |
| 3220 | struct bt_ctf_field_type *type) |
| 3221 | { |
| 3222 | struct bt_ctf_field_type *copy; |
| 3223 | struct bt_ctf_field_type_floating_point *floating_point, *copy_float; |
| 3224 | |
| 3225 | floating_point = container_of(type, |
| 3226 | struct bt_ctf_field_type_floating_point, parent); |
| 3227 | copy = bt_ctf_field_type_floating_point_create(); |
| 3228 | if (!copy) { |
| 3229 | goto end; |
| 3230 | } |
| 3231 | |
| 3232 | copy_float = container_of(copy, |
| 3233 | struct bt_ctf_field_type_floating_point, parent); |
| 3234 | copy_float->declaration = floating_point->declaration; |
| 3235 | copy_float->sign = floating_point->sign; |
| 3236 | copy_float->mantissa = floating_point->mantissa; |
| 3237 | copy_float->exp = floating_point->exp; |
| 3238 | copy_float->user_byte_order = floating_point->user_byte_order; |
| 3239 | end: |
| 3240 | return copy; |
| 3241 | } |
| 3242 | |
| 3243 | static |
| 3244 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( |
| 3245 | struct bt_ctf_field_type *type) |
| 3246 | { |
| 3247 | int i; |
| 3248 | GHashTableIter iter; |
| 3249 | gpointer key, value; |
| 3250 | struct bt_ctf_field_type *copy; |
| 3251 | struct bt_ctf_field_type_structure *structure, *copy_structure; |
| 3252 | |
| 3253 | structure = container_of(type, struct bt_ctf_field_type_structure, |
| 3254 | parent); |
| 3255 | copy = bt_ctf_field_type_structure_create(); |
| 3256 | if (!copy) { |
| 3257 | goto end; |
| 3258 | } |
| 3259 | |
| 3260 | copy_structure = container_of(copy, |
| 3261 | struct bt_ctf_field_type_structure, parent); |
| 3262 | |
| 3263 | /* Copy field_name_to_index */ |
| 3264 | g_hash_table_iter_init(&iter, structure->field_name_to_index); |
| 3265 | while (g_hash_table_iter_next (&iter, &key, &value)) { |
| 3266 | g_hash_table_insert(copy_structure->field_name_to_index, |
| 3267 | key, value); |
| 3268 | } |
| 3269 | |
| 3270 | for (i = 0; i < structure->fields->len; i++) { |
| 3271 | struct structure_field *entry, *copy_entry; |
| 3272 | struct bt_ctf_field_type *copy_field; |
| 3273 | |
| 3274 | copy_entry = g_new0(struct structure_field, 1); |
| 3275 | if (!copy_entry) { |
| 3276 | goto error; |
| 3277 | } |
| 3278 | |
| 3279 | entry = g_ptr_array_index(structure->fields, i); |
| 3280 | copy_field = bt_ctf_field_type_copy(entry->type); |
| 3281 | if (!copy_field) { |
| 3282 | g_free(copy_entry); |
| 3283 | goto error; |
| 3284 | } |
| 3285 | |
| 3286 | copy_entry->name = entry->name; |
| 3287 | copy_entry->type = copy_field; |
| 3288 | g_ptr_array_add(copy_structure->fields, copy_entry); |
| 3289 | } |
| 3290 | |
| 3291 | copy_structure->declaration = structure->declaration; |
| 3292 | end: |
| 3293 | return copy; |
| 3294 | error: |
| 3295 | BT_PUT(copy); |
| 3296 | return copy; |
| 3297 | } |
| 3298 | |
| 3299 | static |
| 3300 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( |
| 3301 | struct bt_ctf_field_type *type) |
| 3302 | { |
| 3303 | int i; |
| 3304 | GHashTableIter iter; |
| 3305 | gpointer key, value; |
| 3306 | struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL; |
| 3307 | struct bt_ctf_field_type_variant *variant, *copy_variant; |
| 3308 | |
| 3309 | variant = container_of(type, struct bt_ctf_field_type_variant, |
| 3310 | parent); |
| 3311 | if (variant->tag) { |
| 3312 | copy_tag = bt_ctf_field_type_copy(&variant->tag->parent); |
| 3313 | if (!copy_tag) { |
| 3314 | goto end; |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | copy = bt_ctf_field_type_variant_create(copy_tag, |
| 3319 | variant->tag_name->len ? variant->tag_name->str : NULL); |
| 3320 | if (!copy) { |
| 3321 | goto end; |
| 3322 | } |
| 3323 | |
| 3324 | copy_variant = container_of(copy, struct bt_ctf_field_type_variant, |
| 3325 | parent); |
| 3326 | |
| 3327 | /* Copy field_name_to_index */ |
| 3328 | g_hash_table_iter_init(&iter, variant->field_name_to_index); |
| 3329 | while (g_hash_table_iter_next (&iter, &key, &value)) { |
| 3330 | g_hash_table_insert(copy_variant->field_name_to_index, |
| 3331 | key, value); |
| 3332 | } |
| 3333 | |
| 3334 | for (i = 0; i < variant->fields->len; i++) { |
| 3335 | struct structure_field *entry, *copy_entry; |
| 3336 | struct bt_ctf_field_type *copy_field; |
| 3337 | |
| 3338 | copy_entry = g_new0(struct structure_field, 1); |
| 3339 | if (!copy_entry) { |
| 3340 | goto error; |
| 3341 | } |
| 3342 | |
| 3343 | entry = g_ptr_array_index(variant->fields, i); |
| 3344 | copy_field = bt_ctf_field_type_copy(entry->type); |
| 3345 | if (!copy_field) { |
| 3346 | g_free(copy_entry); |
| 3347 | goto error; |
| 3348 | } |
| 3349 | |
| 3350 | copy_entry->name = entry->name; |
| 3351 | copy_entry->type = copy_field; |
| 3352 | g_ptr_array_add(copy_variant->fields, copy_entry); |
| 3353 | } |
| 3354 | |
| 3355 | copy_variant->declaration = variant->declaration; |
| 3356 | if (variant->tag_path) { |
| 3357 | copy_variant->tag_path = bt_ctf_field_path_copy( |
| 3358 | variant->tag_path); |
| 3359 | if (!copy_variant->tag_path) { |
| 3360 | goto error; |
| 3361 | } |
| 3362 | } |
| 3363 | end: |
| 3364 | bt_put(copy_tag); |
| 3365 | return copy; |
| 3366 | error: |
| 3367 | bt_put(copy_tag); |
| 3368 | BT_PUT(copy); |
| 3369 | return copy; |
| 3370 | } |
| 3371 | |
| 3372 | static |
| 3373 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy( |
| 3374 | struct bt_ctf_field_type *type) |
| 3375 | { |
| 3376 | struct bt_ctf_field_type *copy = NULL, *copy_element; |
| 3377 | struct bt_ctf_field_type_array *array, *copy_array; |
| 3378 | |
| 3379 | array = container_of(type, struct bt_ctf_field_type_array, |
| 3380 | parent); |
| 3381 | copy_element = bt_ctf_field_type_copy(array->element_type); |
| 3382 | if (!copy_element) { |
| 3383 | goto end; |
| 3384 | } |
| 3385 | |
| 3386 | copy = bt_ctf_field_type_array_create(copy_element, array->length); |
| 3387 | if (!copy) { |
| 3388 | goto end; |
| 3389 | } |
| 3390 | |
| 3391 | copy_array = container_of(copy, struct bt_ctf_field_type_array, |
| 3392 | parent); |
| 3393 | copy_array->declaration = array->declaration; |
| 3394 | end: |
| 3395 | bt_put(copy_element); |
| 3396 | return copy; |
| 3397 | } |
| 3398 | |
| 3399 | static |
| 3400 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( |
| 3401 | struct bt_ctf_field_type *type) |
| 3402 | { |
| 3403 | struct bt_ctf_field_type *copy = NULL, *copy_element; |
| 3404 | struct bt_ctf_field_type_sequence *sequence, *copy_sequence; |
| 3405 | |
| 3406 | sequence = container_of(type, struct bt_ctf_field_type_sequence, |
| 3407 | parent); |
| 3408 | copy_element = bt_ctf_field_type_copy(sequence->element_type); |
| 3409 | if (!copy_element) { |
| 3410 | goto end; |
| 3411 | } |
| 3412 | |
| 3413 | copy = bt_ctf_field_type_sequence_create(copy_element, |
| 3414 | sequence->length_field_name->len ? |
| 3415 | sequence->length_field_name->str : NULL); |
| 3416 | if (!copy) { |
| 3417 | goto end; |
| 3418 | } |
| 3419 | |
| 3420 | copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence, |
| 3421 | parent); |
| 3422 | copy_sequence->declaration = sequence->declaration; |
| 3423 | if (sequence->length_field_path) { |
| 3424 | copy_sequence->length_field_path = bt_ctf_field_path_copy( |
| 3425 | sequence->length_field_path); |
| 3426 | if (!copy_sequence->length_field_path) { |
| 3427 | goto error; |
| 3428 | } |
| 3429 | } |
| 3430 | end: |
| 3431 | bt_put(copy_element); |
| 3432 | return copy; |
| 3433 | error: |
| 3434 | BT_PUT(copy); |
| 3435 | goto end; |
| 3436 | } |
| 3437 | |
| 3438 | static |
| 3439 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( |
| 3440 | struct bt_ctf_field_type *type) |
| 3441 | { |
| 3442 | struct bt_ctf_field_type *copy; |
| 3443 | struct bt_ctf_field_type_string *string, *copy_string; |
| 3444 | |
| 3445 | copy = bt_ctf_field_type_string_create(); |
| 3446 | if (!copy) { |
| 3447 | goto end; |
| 3448 | } |
| 3449 | |
| 3450 | string = container_of(type, struct bt_ctf_field_type_string, |
| 3451 | parent); |
| 3452 | copy_string = container_of(type, struct bt_ctf_field_type_string, |
| 3453 | parent); |
| 3454 | copy_string->declaration = string->declaration; |
| 3455 | end: |
| 3456 | return copy; |
| 3457 | } |
| 3458 | |
| 3459 | static |
| 3460 | int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *type_a, |
| 3461 | struct bt_ctf_field_type *type_b) |
| 3462 | { |
| 3463 | int ret = 1; |
| 3464 | struct bt_ctf_field_type_integer *integer_a; |
| 3465 | struct bt_ctf_field_type_integer *integer_b; |
| 3466 | struct declaration_integer *decl_a; |
| 3467 | struct declaration_integer *decl_b; |
| 3468 | |
| 3469 | integer_a = container_of(type_a, struct bt_ctf_field_type_integer, |
| 3470 | parent); |
| 3471 | integer_b = container_of(type_b, struct bt_ctf_field_type_integer, |
| 3472 | parent); |
| 3473 | decl_a = &integer_a->declaration; |
| 3474 | decl_b = &integer_b->declaration; |
| 3475 | |
| 3476 | /* Length */ |
| 3477 | if (decl_a->len != decl_b->len) { |
| 3478 | goto end; |
| 3479 | } |
| 3480 | |
| 3481 | /* |
| 3482 | * Compare user byte orders only, not the cached, |
| 3483 | * real byte orders. |
| 3484 | */ |
| 3485 | if (integer_a->user_byte_order != integer_b->user_byte_order) { |
| 3486 | goto end; |
| 3487 | } |
| 3488 | |
| 3489 | /* Signedness */ |
| 3490 | if (decl_a->signedness != decl_b->signedness) { |
| 3491 | goto end; |
| 3492 | } |
| 3493 | |
| 3494 | /* Base */ |
| 3495 | if (decl_a->base != decl_b->base) { |
| 3496 | goto end; |
| 3497 | } |
| 3498 | |
| 3499 | /* Encoding */ |
| 3500 | if (decl_a->encoding != decl_b->encoding) { |
| 3501 | goto end; |
| 3502 | } |
| 3503 | |
| 3504 | /* Mapped clock */ |
| 3505 | if (integer_a->mapped_clock != integer_b->mapped_clock) { |
| 3506 | goto end; |
| 3507 | } |
| 3508 | |
| 3509 | /* Equal */ |
| 3510 | ret = 0; |
| 3511 | |
| 3512 | end: |
| 3513 | return ret; |
| 3514 | } |
| 3515 | |
| 3516 | static |
| 3517 | int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *type_a, |
| 3518 | struct bt_ctf_field_type *type_b) |
| 3519 | { |
| 3520 | int ret = 1; |
| 3521 | struct bt_ctf_field_type_floating_point *float_a; |
| 3522 | struct bt_ctf_field_type_floating_point *float_b; |
| 3523 | |
| 3524 | float_a = container_of(type_a, |
| 3525 | struct bt_ctf_field_type_floating_point, parent); |
| 3526 | float_b = container_of(type_b, |
| 3527 | struct bt_ctf_field_type_floating_point, parent); |
| 3528 | |
| 3529 | /* Sign length */ |
| 3530 | if (float_a->sign.len != float_b->sign.len) { |
| 3531 | goto end; |
| 3532 | } |
| 3533 | |
| 3534 | /* Exponent length */ |
| 3535 | if (float_a->exp.len != float_b->exp.len) { |
| 3536 | goto end; |
| 3537 | } |
| 3538 | |
| 3539 | /* Mantissa length */ |
| 3540 | if (float_a->mantissa.len != float_b->mantissa.len) { |
| 3541 | goto end; |
| 3542 | } |
| 3543 | |
| 3544 | /* |
| 3545 | * Compare user byte orders only, not the cached, |
| 3546 | * real byte orders. |
| 3547 | */ |
| 3548 | if (float_a->user_byte_order != float_b->user_byte_order) { |
| 3549 | goto end; |
| 3550 | } |
| 3551 | |
| 3552 | /* Equal */ |
| 3553 | ret = 0; |
| 3554 | |
| 3555 | end: |
| 3556 | return ret; |
| 3557 | } |
| 3558 | |
| 3559 | static |
| 3560 | int compare_enumeration_mappings(struct enumeration_mapping *mapping_a, |
| 3561 | struct enumeration_mapping *mapping_b) |
| 3562 | { |
| 3563 | int ret = 1; |
| 3564 | |
| 3565 | /* Label */ |
| 3566 | if (mapping_a->string != mapping_b->string) { |
| 3567 | goto end; |
| 3568 | } |
| 3569 | |
| 3570 | /* Range start */ |
| 3571 | if (mapping_a->range_start._unsigned != |
| 3572 | mapping_b->range_start._unsigned) { |
| 3573 | goto end; |
| 3574 | } |
| 3575 | |
| 3576 | /* Range end */ |
| 3577 | if (mapping_a->range_end._unsigned != |
| 3578 | mapping_b->range_end._unsigned) { |
| 3579 | goto end; |
| 3580 | } |
| 3581 | |
| 3582 | /* Equal */ |
| 3583 | ret = 0; |
| 3584 | |
| 3585 | end: |
| 3586 | return ret; |
| 3587 | } |
| 3588 | |
| 3589 | static |
| 3590 | int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *type_a, |
| 3591 | struct bt_ctf_field_type *type_b) |
| 3592 | { |
| 3593 | int ret = 1; |
| 3594 | int i; |
| 3595 | struct bt_ctf_field_type_enumeration *enum_a; |
| 3596 | struct bt_ctf_field_type_enumeration *enum_b; |
| 3597 | |
| 3598 | enum_a = container_of(type_a, |
| 3599 | struct bt_ctf_field_type_enumeration, parent); |
| 3600 | enum_b = container_of(type_b, |
| 3601 | struct bt_ctf_field_type_enumeration, parent); |
| 3602 | |
| 3603 | /* Container field type */ |
| 3604 | ret = bt_ctf_field_type_compare(enum_a->container, enum_b->container); |
| 3605 | if (ret) { |
| 3606 | goto end; |
| 3607 | } |
| 3608 | |
| 3609 | ret = 1; |
| 3610 | |
| 3611 | /* Entries */ |
| 3612 | if (enum_a->entries->len != enum_b->entries->len) { |
| 3613 | goto end; |
| 3614 | } |
| 3615 | |
| 3616 | for (i = 0; i < enum_a->entries->len; ++i) { |
| 3617 | struct enumeration_mapping *mapping_a = |
| 3618 | g_ptr_array_index(enum_a->entries, i); |
| 3619 | struct enumeration_mapping *mapping_b = |
| 3620 | g_ptr_array_index(enum_b->entries, i); |
| 3621 | |
| 3622 | if (compare_enumeration_mappings(mapping_a, mapping_b)) { |
| 3623 | goto end; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | /* Equal */ |
| 3628 | ret = 0; |
| 3629 | |
| 3630 | end: |
| 3631 | return ret; |
| 3632 | } |
| 3633 | |
| 3634 | static |
| 3635 | int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *type_a, |
| 3636 | struct bt_ctf_field_type *type_b) |
| 3637 | { |
| 3638 | int ret = 1; |
| 3639 | struct bt_ctf_field_type_string *string_a; |
| 3640 | struct bt_ctf_field_type_string *string_b; |
| 3641 | |
| 3642 | string_a = container_of(type_a, |
| 3643 | struct bt_ctf_field_type_string, parent); |
| 3644 | string_b = container_of(type_b, |
| 3645 | struct bt_ctf_field_type_string, parent); |
| 3646 | |
| 3647 | /* Encoding */ |
| 3648 | if (string_a->declaration.encoding != string_b->declaration.encoding) { |
| 3649 | goto end; |
| 3650 | } |
| 3651 | |
| 3652 | /* Equal */ |
| 3653 | ret = 0; |
| 3654 | |
| 3655 | end: |
| 3656 | return ret; |
| 3657 | } |
| 3658 | |
| 3659 | static |
| 3660 | int compare_structure_fields(struct structure_field *field_a, |
| 3661 | struct structure_field *field_b) |
| 3662 | { |
| 3663 | int ret = 1; |
| 3664 | |
| 3665 | /* Label */ |
| 3666 | if (field_a->name != field_b->name) { |
| 3667 | goto end; |
| 3668 | } |
| 3669 | |
| 3670 | /* Type */ |
| 3671 | ret = bt_ctf_field_type_compare(field_a->type, field_b->type); |
| 3672 | |
| 3673 | end: |
| 3674 | return ret; |
| 3675 | } |
| 3676 | |
| 3677 | static |
| 3678 | int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *type_a, |
| 3679 | struct bt_ctf_field_type *type_b) |
| 3680 | { |
| 3681 | int ret = 1; |
| 3682 | int i; |
| 3683 | struct bt_ctf_field_type_structure *struct_a; |
| 3684 | struct bt_ctf_field_type_structure *struct_b; |
| 3685 | |
| 3686 | struct_a = container_of(type_a, |
| 3687 | struct bt_ctf_field_type_structure, parent); |
| 3688 | struct_b = container_of(type_b, |
| 3689 | struct bt_ctf_field_type_structure, parent); |
| 3690 | |
| 3691 | /* Alignment */ |
| 3692 | if (bt_ctf_field_type_get_alignment(type_a) != |
| 3693 | bt_ctf_field_type_get_alignment(type_b)) { |
| 3694 | goto end; |
| 3695 | } |
| 3696 | |
| 3697 | /* Fields */ |
| 3698 | if (struct_a->fields->len != struct_b->fields->len) { |
| 3699 | goto end; |
| 3700 | } |
| 3701 | |
| 3702 | for (i = 0; i < struct_a->fields->len; ++i) { |
| 3703 | struct structure_field *field_a = |
| 3704 | g_ptr_array_index(struct_a->fields, i); |
| 3705 | struct structure_field *field_b = |
| 3706 | g_ptr_array_index(struct_b->fields, i); |
| 3707 | |
| 3708 | ret = compare_structure_fields(field_a, field_b); |
| 3709 | if (ret) { |
| 3710 | goto end; |
| 3711 | } |
| 3712 | |
| 3713 | ret = 1; |
| 3714 | } |
| 3715 | |
| 3716 | /* Equal */ |
| 3717 | ret = 0; |
| 3718 | |
| 3719 | end: |
| 3720 | return ret; |
| 3721 | } |
| 3722 | |
| 3723 | static |
| 3724 | int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *type_a, |
| 3725 | struct bt_ctf_field_type *type_b) |
| 3726 | { |
| 3727 | int ret = 1; |
| 3728 | int i; |
| 3729 | struct bt_ctf_field_type_variant *variant_a; |
| 3730 | struct bt_ctf_field_type_variant *variant_b; |
| 3731 | |
| 3732 | variant_a = container_of(type_a, |
| 3733 | struct bt_ctf_field_type_variant, parent); |
| 3734 | variant_b = container_of(type_b, |
| 3735 | struct bt_ctf_field_type_variant, parent); |
| 3736 | |
| 3737 | /* Tag name */ |
| 3738 | if (strcmp(variant_a->tag_name->str, variant_b->tag_name->str)) { |
| 3739 | goto end; |
| 3740 | } |
| 3741 | |
| 3742 | /* Tag type */ |
| 3743 | ret = bt_ctf_field_type_compare( |
| 3744 | (struct bt_ctf_field_type *) variant_a->tag, |
| 3745 | (struct bt_ctf_field_type *) variant_b->tag); |
| 3746 | if (ret) { |
| 3747 | goto end; |
| 3748 | } |
| 3749 | |
| 3750 | ret = 1; |
| 3751 | |
| 3752 | /* Fields */ |
| 3753 | if (variant_a->fields->len != variant_b->fields->len) { |
| 3754 | goto end; |
| 3755 | } |
| 3756 | |
| 3757 | for (i = 0; i < variant_a->fields->len; ++i) { |
| 3758 | struct structure_field *field_a = |
| 3759 | g_ptr_array_index(variant_a->fields, i); |
| 3760 | struct structure_field *field_b = |
| 3761 | g_ptr_array_index(variant_b->fields, i); |
| 3762 | |
| 3763 | ret = compare_structure_fields(field_a, field_b); |
| 3764 | if (ret) { |
| 3765 | goto end; |
| 3766 | } |
| 3767 | |
| 3768 | ret = 1; |
| 3769 | } |
| 3770 | |
| 3771 | /* Equal */ |
| 3772 | ret = 0; |
| 3773 | |
| 3774 | end: |
| 3775 | return ret; |
| 3776 | } |
| 3777 | |
| 3778 | static |
| 3779 | int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *type_a, |
| 3780 | struct bt_ctf_field_type *type_b) |
| 3781 | { |
| 3782 | int ret = 1; |
| 3783 | struct bt_ctf_field_type_array *array_a; |
| 3784 | struct bt_ctf_field_type_array *array_b; |
| 3785 | |
| 3786 | array_a = container_of(type_a, |
| 3787 | struct bt_ctf_field_type_array, parent); |
| 3788 | array_b = container_of(type_b, |
| 3789 | struct bt_ctf_field_type_array, parent); |
| 3790 | |
| 3791 | /* Length */ |
| 3792 | if (array_a->length != array_b->length) { |
| 3793 | goto end; |
| 3794 | } |
| 3795 | |
| 3796 | /* Element type */ |
| 3797 | ret = bt_ctf_field_type_compare(array_a->element_type, |
| 3798 | array_b->element_type); |
| 3799 | |
| 3800 | end: |
| 3801 | return ret; |
| 3802 | } |
| 3803 | |
| 3804 | static |
| 3805 | int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *type_a, |
| 3806 | struct bt_ctf_field_type *type_b) |
| 3807 | { |
| 3808 | int ret = -1; |
| 3809 | struct bt_ctf_field_type_sequence *sequence_a; |
| 3810 | struct bt_ctf_field_type_sequence *sequence_b; |
| 3811 | |
| 3812 | sequence_a = container_of(type_a, |
| 3813 | struct bt_ctf_field_type_sequence, parent); |
| 3814 | sequence_b = container_of(type_b, |
| 3815 | struct bt_ctf_field_type_sequence, parent); |
| 3816 | |
| 3817 | /* Length name */ |
| 3818 | if (strcmp(sequence_a->length_field_name->str, |
| 3819 | sequence_b->length_field_name->str)) { |
| 3820 | goto end; |
| 3821 | } |
| 3822 | |
| 3823 | /* Element type */ |
| 3824 | ret = bt_ctf_field_type_compare(sequence_a->element_type, |
| 3825 | sequence_b->element_type); |
| 3826 | |
| 3827 | end: |
| 3828 | return ret; |
| 3829 | } |
| 3830 | |
| 3831 | int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a, |
| 3832 | struct bt_ctf_field_type *type_b) |
| 3833 | { |
| 3834 | int ret = 1; |
| 3835 | |
| 3836 | if (type_a == type_b) { |
| 3837 | /* Same reference: equal (even if both are NULL) */ |
| 3838 | ret = 0; |
| 3839 | goto end; |
| 3840 | } |
| 3841 | |
| 3842 | if (!type_a || !type_b) { |
| 3843 | ret = -1; |
| 3844 | goto end; |
| 3845 | } |
| 3846 | |
| 3847 | if (type_a->declaration->id != type_b->declaration->id) { |
| 3848 | /* Different type IDs */ |
| 3849 | goto end; |
| 3850 | } |
| 3851 | |
| 3852 | if (type_a->declaration->id == CTF_TYPE_UNKNOWN) { |
| 3853 | /* Both have unknown type IDs */ |
| 3854 | goto end; |
| 3855 | } |
| 3856 | |
| 3857 | ret = type_compare_funcs[type_a->declaration->id](type_a, type_b); |
| 3858 | |
| 3859 | end: |
| 3860 | return ret; |
| 3861 | } |