| 1 | /* |
| 2 | * BabelTrace - Structure Type Converter |
| 3 | * |
| 4 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <babeltrace/compiler.h> |
| 22 | #include <babeltrace/types.h> |
| 23 | |
| 24 | void struct_copy(struct stream_pos *dest, const struct format *fdest, |
| 25 | struct stream_pos *src, const struct format *fsrc, |
| 26 | const struct type_class *type_class) |
| 27 | { |
| 28 | struct type_class_struct *struct_class = |
| 29 | container_of(type_class, struct type_class_struct, p); |
| 30 | unsigned int i; |
| 31 | |
| 32 | fsrc->struct_begin(src, struct_class); |
| 33 | fdest->struct_begin(dest, struct_class); |
| 34 | |
| 35 | for (i = 0; i < struct_class->fields->len; i++) { |
| 36 | struct field *field = &g_array_index(struct_class->fields, |
| 37 | struct field, i); |
| 38 | struct type_class *field_class = field->type_class; |
| 39 | |
| 40 | field_class->copy(dest, fdest, src, fsrc, type_class); |
| 41 | |
| 42 | } |
| 43 | fsrc->struct_end(src, struct_class); |
| 44 | fdest->struct_end(dest, struct_class); |
| 45 | } |
| 46 | |
| 47 | void struct_type_free(struct type_class_struct *struct_class) |
| 48 | { |
| 49 | g_hash_table_destroy(struct_class->fields_by_name); |
| 50 | g_array_free(struct_class->fields, true); |
| 51 | g_free(struct_class); |
| 52 | } |
| 53 | |
| 54 | static void _struct_type_free(struct type_class *type_class) |
| 55 | { |
| 56 | struct type_class_struct *struct_class = |
| 57 | container_of(type_class, struct type_class_struct, p); |
| 58 | struct_type_free(struct_class); |
| 59 | } |
| 60 | |
| 61 | struct type_class_struct *struct_type_new(const char *name) |
| 62 | { |
| 63 | struct type_class_struct *struct_class; |
| 64 | int ret; |
| 65 | |
| 66 | struct_class = g_new(struct type_class_struct, 1); |
| 67 | type_class = &float_class->p; |
| 68 | |
| 69 | struct_class->fields_by_name = g_hash_table_new(g_direct_hash, |
| 70 | g_direct_equal); |
| 71 | struct_class->fields = g_array_sized_new(false, false, |
| 72 | sizeof(struct field), |
| 73 | DEFAULT_NR_STRUCT_FIELDS) |
| 74 | type_class->name = g_quark_from_string(name); |
| 75 | type_class->alignment = 1; |
| 76 | type_class->copy = struct_copy; |
| 77 | type_class->free = _struct_type_free; |
| 78 | |
| 79 | if (type_class->name) { |
| 80 | ret = ctf_register_type(type_class); |
| 81 | if (ret) |
| 82 | goto error_register; |
| 83 | } |
| 84 | return struct_class; |
| 85 | |
| 86 | error_register: |
| 87 | g_free(struct_class); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | void struct_type_add_field(struct type_class_struct *struct_class, |
| 92 | const char *field_name, |
| 93 | struct type_class *type_class) |
| 94 | { |
| 95 | struct field *field; |
| 96 | unsigned long index; |
| 97 | |
| 98 | g_array_set_size(struct_class->fields, struct_class->fields->len + 1); |
| 99 | index = struct_class->fields->len - 1; /* last field (new) */ |
| 100 | field = &g_array_index(struct_class->fields, struct field, index); |
| 101 | field->name = g_quark_from_string(field_name); |
| 102 | field->type_class = type_class; |
| 103 | /* Keep index in hash rather than pointer, because array can relocate */ |
| 104 | g_hash_table_insert(struct_class->fields_by_name, |
| 105 | (gpointer) (unsigned long) field->name, |
| 106 | (gpointer) index); |
| 107 | /* |
| 108 | * Alignment of structure is the max alignment of types contained |
| 109 | * therein. |
| 110 | */ |
| 111 | struct_class->p.alignment = max(struct_class->p.alignment, |
| 112 | type_class->alignment); |
| 113 | } |
| 114 | |
| 115 | unsigned long |
| 116 | struct_type_lookup_field_index(struct type_class_struct *struct_class, |
| 117 | GQuark field_name) |
| 118 | { |
| 119 | unsigned long index; |
| 120 | |
| 121 | index = (unsigned long) g_hash_table_lookup(struct_class->fields_by_name, |
| 122 | field_name); |
| 123 | return index; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * field returned only valid as long as the field structure is not appended to. |
| 128 | */ |
| 129 | struct field * |
| 130 | struct_type_get_field_from_index(struct type_class_struct *struct_class, |
| 131 | unsigned long index) |
| 132 | { |
| 133 | return &g_array_index(struct_class->fields, struct field, index); |
| 134 | } |