| 1 | /* |
| 2 | * struct.c |
| 3 | * |
| 4 | * BabelTrace - Structure Type Converter |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@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/compiler.h> |
| 30 | #include <babeltrace/format.h> |
| 31 | #include <babeltrace/types.h> |
| 32 | #include <errno.h> |
| 33 | |
| 34 | #ifndef max |
| 35 | #define max(a, b) ((a) < (b) ? (b) : (a)) |
| 36 | #endif |
| 37 | |
| 38 | static |
| 39 | struct bt_definition *_struct_definition_new(struct bt_declaration *declaration, |
| 40 | struct definition_scope *parent_scope, |
| 41 | GQuark field_name, int index, |
| 42 | const char *root_name); |
| 43 | static |
| 44 | void _struct_definition_free(struct bt_definition *definition); |
| 45 | |
| 46 | int bt_struct_rw(struct bt_stream_pos *ppos, struct bt_definition *definition) |
| 47 | { |
| 48 | struct definition_struct *struct_definition = |
| 49 | container_of(definition, struct definition_struct, p); |
| 50 | unsigned long i; |
| 51 | int ret; |
| 52 | |
| 53 | for (i = 0; i < struct_definition->fields->len; i++) { |
| 54 | struct bt_definition *field = |
| 55 | g_ptr_array_index(struct_definition->fields, i); |
| 56 | ret = generic_rw(ppos, field); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static |
| 64 | void _struct_declaration_free(struct bt_declaration *declaration) |
| 65 | { |
| 66 | struct declaration_struct *struct_declaration = |
| 67 | container_of(declaration, struct declaration_struct, p); |
| 68 | unsigned long i; |
| 69 | |
| 70 | bt_free_declaration_scope(struct_declaration->scope); |
| 71 | g_hash_table_destroy(struct_declaration->fields_by_name); |
| 72 | |
| 73 | for (i = 0; i < struct_declaration->fields->len; i++) { |
| 74 | struct declaration_field *declaration_field = |
| 75 | &g_array_index(struct_declaration->fields, |
| 76 | struct declaration_field, i); |
| 77 | bt_declaration_unref(declaration_field->declaration); |
| 78 | } |
| 79 | g_array_free(struct_declaration->fields, true); |
| 80 | g_free(struct_declaration); |
| 81 | } |
| 82 | |
| 83 | struct declaration_struct * |
| 84 | bt_struct_declaration_new(struct declaration_scope *parent_scope, |
| 85 | uint64_t min_align) |
| 86 | { |
| 87 | struct declaration_struct *struct_declaration; |
| 88 | struct bt_declaration *declaration; |
| 89 | |
| 90 | struct_declaration = g_new(struct declaration_struct, 1); |
| 91 | declaration = &struct_declaration->p; |
| 92 | struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash, |
| 93 | g_direct_equal); |
| 94 | struct_declaration->fields = g_array_sized_new(FALSE, TRUE, |
| 95 | sizeof(struct declaration_field), |
| 96 | DEFAULT_NR_STRUCT_FIELDS); |
| 97 | struct_declaration->scope = bt_new_declaration_scope(parent_scope); |
| 98 | declaration->id = CTF_TYPE_STRUCT; |
| 99 | declaration->alignment = max(1, min_align); |
| 100 | declaration->declaration_free = _struct_declaration_free; |
| 101 | declaration->definition_new = _struct_definition_new; |
| 102 | declaration->definition_free = _struct_definition_free; |
| 103 | declaration->ref = 1; |
| 104 | return struct_declaration; |
| 105 | } |
| 106 | |
| 107 | static |
| 108 | struct bt_definition * |
| 109 | _struct_definition_new(struct bt_declaration *declaration, |
| 110 | struct definition_scope *parent_scope, |
| 111 | GQuark field_name, int index, |
| 112 | const char *root_name) |
| 113 | { |
| 114 | struct declaration_struct *struct_declaration = |
| 115 | container_of(declaration, struct declaration_struct, p); |
| 116 | struct definition_struct *_struct; |
| 117 | int i; |
| 118 | int ret; |
| 119 | |
| 120 | _struct = g_new(struct definition_struct, 1); |
| 121 | bt_declaration_ref(&struct_declaration->p); |
| 122 | _struct->p.declaration = declaration; |
| 123 | _struct->declaration = struct_declaration; |
| 124 | _struct->p.ref = 1; |
| 125 | /* |
| 126 | * Use INT_MAX order to ensure that all fields of the parent |
| 127 | * scope are seen as being prior to this scope. |
| 128 | */ |
| 129 | _struct->p.index = root_name ? INT_MAX : index; |
| 130 | _struct->p.name = field_name; |
| 131 | _struct->p.path = bt_new_definition_path(parent_scope, field_name, root_name); |
| 132 | _struct->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name); |
| 133 | |
| 134 | ret = bt_register_field_definition(field_name, &_struct->p, |
| 135 | parent_scope); |
| 136 | assert(!ret || ret == -EPERM); |
| 137 | |
| 138 | _struct->fields = g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS); |
| 139 | g_ptr_array_set_size(_struct->fields, struct_declaration->fields->len); |
| 140 | for (i = 0; i < struct_declaration->fields->len; i++) { |
| 141 | struct declaration_field *declaration_field = |
| 142 | &g_array_index(struct_declaration->fields, |
| 143 | struct declaration_field, i); |
| 144 | struct bt_definition **field = |
| 145 | (struct bt_definition **) &g_ptr_array_index(_struct->fields, i); |
| 146 | |
| 147 | *field = declaration_field->declaration->definition_new(declaration_field->declaration, |
| 148 | _struct->p.scope, |
| 149 | declaration_field->name, i, NULL); |
| 150 | if (!*field) |
| 151 | goto error; |
| 152 | } |
| 153 | return &_struct->p; |
| 154 | |
| 155 | error: |
| 156 | for (i--; i >= 0; i--) { |
| 157 | struct bt_definition *field = g_ptr_array_index(_struct->fields, i); |
| 158 | bt_definition_unref(field); |
| 159 | } |
| 160 | bt_free_definition_scope(_struct->p.scope); |
| 161 | bt_declaration_unref(&struct_declaration->p); |
| 162 | g_free(_struct); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | static |
| 167 | void _struct_definition_free(struct bt_definition *definition) |
| 168 | { |
| 169 | struct definition_struct *_struct = |
| 170 | container_of(definition, struct definition_struct, p); |
| 171 | unsigned long i; |
| 172 | |
| 173 | assert(_struct->fields->len == _struct->declaration->fields->len); |
| 174 | for (i = 0; i < _struct->fields->len; i++) { |
| 175 | struct bt_definition *field = g_ptr_array_index(_struct->fields, i); |
| 176 | bt_definition_unref(field); |
| 177 | } |
| 178 | bt_free_definition_scope(_struct->p.scope); |
| 179 | bt_declaration_unref(_struct->p.declaration); |
| 180 | g_ptr_array_free(_struct->fields, TRUE); |
| 181 | g_free(_struct); |
| 182 | } |
| 183 | |
| 184 | void bt_struct_declaration_add_field(struct declaration_struct *struct_declaration, |
| 185 | const char *field_name, |
| 186 | struct bt_declaration *field_declaration) |
| 187 | { |
| 188 | struct declaration_field *field; |
| 189 | unsigned long index; |
| 190 | |
| 191 | g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1); |
| 192 | index = struct_declaration->fields->len - 1; /* last field (new) */ |
| 193 | field = &g_array_index(struct_declaration->fields, struct declaration_field, index); |
| 194 | field->name = g_quark_from_string(field_name); |
| 195 | bt_declaration_ref(field_declaration); |
| 196 | field->declaration = field_declaration; |
| 197 | /* Keep index in hash rather than pointer, because array can relocate */ |
| 198 | g_hash_table_insert(struct_declaration->fields_by_name, |
| 199 | (gpointer) (unsigned long) field->name, |
| 200 | (gpointer) index); |
| 201 | /* |
| 202 | * Alignment of structure is the max alignment of declarations contained |
| 203 | * therein. |
| 204 | */ |
| 205 | struct_declaration->p.alignment = max(struct_declaration->p.alignment, |
| 206 | field_declaration->alignment); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * bt_struct_declaration_lookup_field_index - returns field index |
| 211 | * |
| 212 | * Returns the index of a field in a structure, or -1 if it does not |
| 213 | * exist. |
| 214 | */ |
| 215 | int bt_struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration, |
| 216 | GQuark field_name) |
| 217 | { |
| 218 | gpointer index; |
| 219 | gboolean found; |
| 220 | |
| 221 | found = g_hash_table_lookup_extended(struct_declaration->fields_by_name, |
| 222 | (gconstpointer) (unsigned long) field_name, |
| 223 | NULL, &index); |
| 224 | if (!found) |
| 225 | return -1; |
| 226 | return (int) (unsigned long) index; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * field returned only valid as long as the field structure is not appended to. |
| 231 | */ |
| 232 | struct declaration_field * |
| 233 | bt_struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration, |
| 234 | int index) |
| 235 | { |
| 236 | if (index < 0) |
| 237 | return NULL; |
| 238 | return &g_array_index(struct_declaration->fields, struct declaration_field, index); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * field returned only valid as long as the field structure is not appended to. |
| 243 | */ |
| 244 | struct bt_definition * |
| 245 | bt_struct_definition_get_field_from_index(const struct definition_struct *_struct, |
| 246 | int index) |
| 247 | { |
| 248 | if (index < 0) |
| 249 | return NULL; |
| 250 | return g_ptr_array_index(_struct->fields, index); |
| 251 | } |
| 252 | |
| 253 | uint64_t bt_struct_declaration_len(const struct declaration_struct *struct_declaration) |
| 254 | { |
| 255 | return struct_declaration->fields->len; |
| 256 | } |