| 1 | #ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H |
| 2 | #define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Babeltrace - CTF IR: Event Fields internal |
| 6 | * |
| 7 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <babeltrace/assert-pre-internal.h> |
| 31 | #include <babeltrace/common-internal.h> |
| 32 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 33 | #include <babeltrace/ctf-ir/utils-internal.h> |
| 34 | #include <babeltrace/object-internal.h> |
| 35 | #include <babeltrace/babeltrace-internal.h> |
| 36 | #include <babeltrace/types.h> |
| 37 | #include <stdint.h> |
| 38 | #include <string.h> |
| 39 | #include <inttypes.h> |
| 40 | #include <stdbool.h> |
| 41 | #include <glib.h> |
| 42 | |
| 43 | #define BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(_field, _type_id, _name) \ |
| 44 | BT_ASSERT_PRE(((struct bt_field *) (_field))->type->id == (_type_id), \ |
| 45 | _name " has the wrong type ID: expected-type-id=%s, " \ |
| 46 | "%![field-]+f", \ |
| 47 | bt_common_field_type_id_string(_type_id), (_field)) |
| 48 | |
| 49 | #define BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(_field, _name) \ |
| 50 | BT_ASSERT_PRE( \ |
| 51 | ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \ |
| 52 | ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION, \ |
| 53 | _name " is not an unsigned integer field: %![field-]+f", \ |
| 54 | (_field)) |
| 55 | |
| 56 | #define BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(_field, _name) \ |
| 57 | BT_ASSERT_PRE( \ |
| 58 | ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_SIGNED_INTEGER || \ |
| 59 | ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \ |
| 60 | _name " is not a signed integer field: %![field-]+f", \ |
| 61 | (_field)) |
| 62 | |
| 63 | #define BT_ASSERT_PRE_FIELD_IS_ARRAY(_field, _name) \ |
| 64 | BT_ASSERT_PRE( \ |
| 65 | ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_STATIC_ARRAY || \ |
| 66 | ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_DYNAMIC_ARRAY, \ |
| 67 | _name " is not an array field: %![field-]+f", (_field)) |
| 68 | |
| 69 | #define BT_ASSERT_PRE_FIELD_IS_SET(_field, _name) \ |
| 70 | BT_ASSERT_PRE(bt_field_is_set(_field), \ |
| 71 | _name " is not set: %!+f", (_field)) |
| 72 | |
| 73 | #define BT_ASSERT_PRE_FIELD_HOT(_field, _name) \ |
| 74 | BT_ASSERT_PRE_HOT((struct bt_field *) (_field), (_name), \ |
| 75 | ": %!+f", (_field)) |
| 76 | |
| 77 | struct bt_field; |
| 78 | |
| 79 | typedef struct bt_field *(* bt_field_create_func)(struct bt_field_type *); |
| 80 | typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool); |
| 81 | typedef bool (*bt_field_method_is_set)(struct bt_field *); |
| 82 | typedef void (*bt_field_method_reset)(struct bt_field *); |
| 83 | |
| 84 | struct bt_field_methods { |
| 85 | bt_field_method_set_is_frozen set_is_frozen; |
| 86 | bt_field_method_is_set is_set; |
| 87 | bt_field_method_reset reset; |
| 88 | }; |
| 89 | |
| 90 | struct bt_field { |
| 91 | struct bt_object base; |
| 92 | |
| 93 | /* Owned by this */ |
| 94 | struct bt_field_type *type; |
| 95 | |
| 96 | /* Virtual table for slow path (dev mode) operations */ |
| 97 | struct bt_field_methods *methods; |
| 98 | |
| 99 | bool is_set; |
| 100 | bool frozen; |
| 101 | }; |
| 102 | |
| 103 | struct bt_field_integer { |
| 104 | struct bt_field common; |
| 105 | |
| 106 | union { |
| 107 | uint64_t u; |
| 108 | int64_t i; |
| 109 | } value; |
| 110 | }; |
| 111 | |
| 112 | struct bt_field_real { |
| 113 | struct bt_field common; |
| 114 | double value; |
| 115 | }; |
| 116 | |
| 117 | struct bt_field_structure { |
| 118 | struct bt_field common; |
| 119 | |
| 120 | /* Array of `struct bt_field *`, owned by this */ |
| 121 | GPtrArray *fields; |
| 122 | }; |
| 123 | |
| 124 | struct bt_field_variant { |
| 125 | struct bt_field common; |
| 126 | |
| 127 | /* Weak: belongs to `fields` below */ |
| 128 | struct bt_field *selected_field; |
| 129 | |
| 130 | /* Index of currently selected field */ |
| 131 | uint64_t selected_index; |
| 132 | |
| 133 | /* Array of `struct bt_field *`, owned by this */ |
| 134 | GPtrArray *fields; |
| 135 | }; |
| 136 | |
| 137 | struct bt_field_array { |
| 138 | struct bt_field common; |
| 139 | |
| 140 | /* Array of `struct bt_field *`, owned by this */ |
| 141 | GPtrArray *fields; |
| 142 | |
| 143 | /* Current effective length */ |
| 144 | uint64_t length; |
| 145 | }; |
| 146 | |
| 147 | struct bt_field_string { |
| 148 | struct bt_field common; |
| 149 | GArray *buf; |
| 150 | uint64_t length; |
| 151 | }; |
| 152 | |
| 153 | #ifdef BT_DEV_MODE |
| 154 | # define bt_field_set_is_frozen _bt_field_set_is_frozen |
| 155 | # define bt_field_is_set _bt_field_is_set |
| 156 | # define bt_field_reset _bt_field_reset |
| 157 | # define bt_field_set_single _bt_field_set_single |
| 158 | #else |
| 159 | # define bt_field_set_is_frozen(_field, _is_frozen) |
| 160 | # define bt_field_is_set(_field) (BT_FALSE) |
| 161 | # define bt_field_reset(_field) |
| 162 | # define bt_field_set_single(_field, _val) |
| 163 | #endif |
| 164 | |
| 165 | BT_HIDDEN |
| 166 | void _bt_field_set_is_frozen(struct bt_field *field, bool is_frozen); |
| 167 | |
| 168 | static inline |
| 169 | void _bt_field_reset(struct bt_field *field) |
| 170 | { |
| 171 | BT_ASSERT(field); |
| 172 | BT_ASSERT(field->methods->reset); |
| 173 | field->methods->reset(field); |
| 174 | } |
| 175 | |
| 176 | static inline |
| 177 | void _bt_field_set_single(struct bt_field *field, bool value) |
| 178 | { |
| 179 | BT_ASSERT(field); |
| 180 | field->is_set = value; |
| 181 | } |
| 182 | |
| 183 | static inline |
| 184 | bt_bool _bt_field_is_set(struct bt_field *field) |
| 185 | { |
| 186 | bt_bool is_set = BT_FALSE; |
| 187 | |
| 188 | if (!field) { |
| 189 | goto end; |
| 190 | } |
| 191 | |
| 192 | BT_ASSERT(bt_field_type_has_known_id(field->type)); |
| 193 | BT_ASSERT(field->methods->is_set); |
| 194 | is_set = field->methods->is_set(field); |
| 195 | |
| 196 | end: |
| 197 | return is_set; |
| 198 | } |
| 199 | |
| 200 | BT_HIDDEN |
| 201 | struct bt_field *bt_field_create(struct bt_field_type *type); |
| 202 | |
| 203 | BT_HIDDEN |
| 204 | void bt_field_destroy(struct bt_field *field); |
| 205 | |
| 206 | #endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */ |