Commit | Line | Data |
---|---|---|
11796b96 | 1 | /* |
ccd7e1c8 | 2 | * struct.c |
11796b96 | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Structure Type Converter |
11796b96 | 5 | * |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
11796b96 | 9 | * |
ccd7e1c8 MD |
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: | |
11796b96 | 16 | * |
ccd7e1c8 MD |
17 | * The above copyright notice and this permission notice shall be included in |
18 | * all copies or substantial portions of the Software. | |
11796b96 MD |
19 | */ |
20 | ||
21 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 22 | #include <babeltrace/format.h> |
98df1c9f | 23 | #include <errno.h> |
11796b96 | 24 | |
be85c1c7 MD |
25 | #ifndef max |
26 | #define max(a, b) ((a) < (b) ? (b) : (a)) | |
27 | #endif | |
28 | ||
c054553d | 29 | static |
f6625916 | 30 | struct definition *_struct_definition_new(struct declaration *declaration, |
05c749e5 | 31 | struct definition_scope *parent_scope, |
98df1c9f MD |
32 | GQuark field_name, int index, |
33 | const char *root_name); | |
c054553d | 34 | static |
e1151715 | 35 | void _struct_definition_free(struct definition *definition); |
c054553d | 36 | |
c5e74408 | 37 | int struct_rw(struct stream_pos *ppos, struct definition *definition) |
11796b96 | 38 | { |
d11e9c49 | 39 | struct definition_struct *struct_definition = |
e1151715 | 40 | container_of(definition, struct definition_struct, p); |
c054553d | 41 | unsigned long i; |
c5e74408 | 42 | int ret; |
11796b96 | 43 | |
d11e9c49 | 44 | for (i = 0; i < struct_definition->fields->len; i++) { |
b1a2f580 MD |
45 | struct definition *field = |
46 | g_ptr_array_index(struct_definition->fields, i); | |
47 | ret = generic_rw(ppos, field); | |
c5e74408 MD |
48 | if (ret) |
49 | return ret; | |
11796b96 | 50 | } |
c5e74408 | 51 | return 0; |
11796b96 MD |
52 | } |
53 | ||
c054553d | 54 | static |
f6625916 | 55 | void _struct_declaration_free(struct declaration *declaration) |
11796b96 | 56 | { |
f6625916 MD |
57 | struct declaration_struct *struct_declaration = |
58 | container_of(declaration, struct declaration_struct, p); | |
c054553d | 59 | unsigned long i; |
4c8bfb7e | 60 | |
f6625916 MD |
61 | free_declaration_scope(struct_declaration->scope); |
62 | g_hash_table_destroy(struct_declaration->fields_by_name); | |
4c8bfb7e | 63 | |
f6625916 MD |
64 | for (i = 0; i < struct_declaration->fields->len; i++) { |
65 | struct declaration_field *declaration_field = | |
66 | &g_array_index(struct_declaration->fields, | |
67 | struct declaration_field, i); | |
68 | declaration_unref(declaration_field->declaration); | |
4c8bfb7e | 69 | } |
f6625916 MD |
70 | g_array_free(struct_declaration->fields, true); |
71 | g_free(struct_declaration); | |
11796b96 MD |
72 | } |
73 | ||
ab4cf058 | 74 | struct declaration_struct * |
b7e35bad MD |
75 | struct_declaration_new(struct declaration_scope *parent_scope, |
76 | uint64_t min_align) | |
11796b96 | 77 | { |
f6625916 MD |
78 | struct declaration_struct *struct_declaration; |
79 | struct declaration *declaration; | |
11796b96 | 80 | |
f6625916 MD |
81 | struct_declaration = g_new(struct declaration_struct, 1); |
82 | declaration = &struct_declaration->p; | |
83 | struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash, | |
e19c3d69 | 84 | g_direct_equal); |
f6625916 MD |
85 | struct_declaration->fields = g_array_sized_new(FALSE, TRUE, |
86 | sizeof(struct declaration_field), | |
e19c3d69 | 87 | DEFAULT_NR_STRUCT_FIELDS); |
f6625916 MD |
88 | struct_declaration->scope = new_declaration_scope(parent_scope); |
89 | declaration->id = CTF_TYPE_STRUCT; | |
b7e35bad | 90 | declaration->alignment = max(1, min_align); |
f6625916 MD |
91 | declaration->declaration_free = _struct_declaration_free; |
92 | declaration->definition_new = _struct_definition_new; | |
93 | declaration->definition_free = _struct_definition_free; | |
94 | declaration->ref = 1; | |
95 | return struct_declaration; | |
11796b96 MD |
96 | } |
97 | ||
c054553d | 98 | static |
e1151715 | 99 | struct definition * |
f6625916 | 100 | _struct_definition_new(struct declaration *declaration, |
05c749e5 | 101 | struct definition_scope *parent_scope, |
98df1c9f MD |
102 | GQuark field_name, int index, |
103 | const char *root_name) | |
11796b96 | 104 | { |
f6625916 MD |
105 | struct declaration_struct *struct_declaration = |
106 | container_of(declaration, struct declaration_struct, p); | |
e1151715 | 107 | struct definition_struct *_struct; |
98df1c9f | 108 | int i; |
ac88af75 | 109 | int ret; |
c054553d | 110 | |
e1151715 | 111 | _struct = g_new(struct definition_struct, 1); |
f6625916 MD |
112 | declaration_ref(&struct_declaration->p); |
113 | _struct->p.declaration = declaration; | |
114 | _struct->declaration = struct_declaration; | |
c054553d | 115 | _struct->p.ref = 1; |
98df1c9f MD |
116 | /* |
117 | * Use INT_MAX order to ensure that all fields of the parent | |
118 | * scope are seen as being prior to this scope. | |
119 | */ | |
120 | _struct->p.index = root_name ? INT_MAX : index; | |
b1a2f580 | 121 | _struct->p.name = field_name; |
98df1c9f | 122 | _struct->p.path = new_definition_path(parent_scope, field_name, root_name); |
a35173fe | 123 | _struct->p.scope = new_definition_scope(parent_scope, field_name, root_name); |
98df1c9f MD |
124 | |
125 | ret = register_field_definition(field_name, &_struct->p, | |
126 | parent_scope); | |
127 | assert(!ret || ret == -EPERM); | |
128 | ||
b1a2f580 MD |
129 | _struct->fields = g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS); |
130 | g_ptr_array_set_size(_struct->fields, struct_declaration->fields->len); | |
f6625916 MD |
131 | for (i = 0; i < struct_declaration->fields->len; i++) { |
132 | struct declaration_field *declaration_field = | |
133 | &g_array_index(struct_declaration->fields, | |
134 | struct declaration_field, i); | |
b1a2f580 MD |
135 | struct definition **field = |
136 | (struct definition **) &g_ptr_array_index(_struct->fields, i); | |
ac88af75 | 137 | |
b1a2f580 | 138 | *field = declaration_field->declaration->definition_new(declaration_field->declaration, |
a35173fe | 139 | _struct->p.scope, |
98df1c9f MD |
140 | declaration_field->name, i, NULL); |
141 | if (!*field) | |
142 | goto error; | |
ac88af75 | 143 | } |
c054553d | 144 | return &_struct->p; |
98df1c9f MD |
145 | |
146 | error: | |
147 | for (i--; i >= 0; i--) { | |
148 | struct definition *field = g_ptr_array_index(_struct->fields, i); | |
149 | definition_unref(field); | |
150 | } | |
a35173fe | 151 | free_definition_scope(_struct->p.scope); |
98df1c9f MD |
152 | declaration_unref(&struct_declaration->p); |
153 | g_free(_struct); | |
154 | return NULL; | |
c054553d MD |
155 | } |
156 | ||
157 | static | |
e1151715 | 158 | void _struct_definition_free(struct definition *definition) |
c054553d | 159 | { |
e1151715 MD |
160 | struct definition_struct *_struct = |
161 | container_of(definition, struct definition_struct, p); | |
c054553d MD |
162 | unsigned long i; |
163 | ||
f6625916 | 164 | assert(_struct->fields->len == _struct->declaration->fields->len); |
c054553d | 165 | for (i = 0; i < _struct->fields->len; i++) { |
b1a2f580 MD |
166 | struct definition *field = g_ptr_array_index(_struct->fields, i); |
167 | definition_unref(field); | |
c054553d | 168 | } |
a35173fe | 169 | free_definition_scope(_struct->p.scope); |
f6625916 | 170 | declaration_unref(_struct->p.declaration); |
c054553d MD |
171 | g_free(_struct); |
172 | } | |
173 | ||
f6625916 | 174 | void struct_declaration_add_field(struct declaration_struct *struct_declaration, |
e19c3d69 | 175 | const char *field_name, |
f6625916 | 176 | struct declaration *field_declaration) |
c054553d | 177 | { |
f6625916 | 178 | struct declaration_field *field; |
11796b96 MD |
179 | unsigned long index; |
180 | ||
f6625916 MD |
181 | g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1); |
182 | index = struct_declaration->fields->len - 1; /* last field (new) */ | |
183 | field = &g_array_index(struct_declaration->fields, struct declaration_field, index); | |
11796b96 | 184 | field->name = g_quark_from_string(field_name); |
f6625916 MD |
185 | declaration_ref(field_declaration); |
186 | field->declaration = field_declaration; | |
11796b96 | 187 | /* Keep index in hash rather than pointer, because array can relocate */ |
f6625916 | 188 | g_hash_table_insert(struct_declaration->fields_by_name, |
11796b96 MD |
189 | (gpointer) (unsigned long) field->name, |
190 | (gpointer) index); | |
191 | /* | |
f6625916 | 192 | * Alignment of structure is the max alignment of declarations contained |
11796b96 MD |
193 | * therein. |
194 | */ | |
f6625916 MD |
195 | struct_declaration->p.alignment = max(struct_declaration->p.alignment, |
196 | field_declaration->alignment); | |
11796b96 MD |
197 | } |
198 | ||
0f980a35 MD |
199 | /* |
200 | * struct_declaration_lookup_field_index - returns field index | |
201 | * | |
202 | * Returns the index of a field in a structure, or -1 if it does not | |
203 | * exist. | |
204 | */ | |
205 | int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration, | |
e19c3d69 | 206 | GQuark field_name) |
11796b96 | 207 | { |
0f980a35 MD |
208 | gpointer index; |
209 | gboolean found; | |
210 | ||
211 | found = g_hash_table_lookup_extended(struct_declaration->fields_by_name, | |
212 | (gconstpointer) (unsigned long) field_name, | |
213 | NULL, &index); | |
214 | if (!found) | |
215 | return -1; | |
216 | return (int) (unsigned long) index; | |
11796b96 MD |
217 | } |
218 | ||
c054553d MD |
219 | /* |
220 | * field returned only valid as long as the field structure is not appended to. | |
221 | */ | |
f6625916 MD |
222 | struct declaration_field * |
223 | struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration, | |
0f980a35 | 224 | int index) |
c054553d | 225 | { |
0f980a35 MD |
226 | if (index < 0) |
227 | return NULL; | |
f6625916 | 228 | return &g_array_index(struct_declaration->fields, struct declaration_field, index); |
c054553d MD |
229 | } |
230 | ||
11796b96 MD |
231 | /* |
232 | * field returned only valid as long as the field structure is not appended to. | |
233 | */ | |
b1a2f580 | 234 | struct definition * |
e1151715 | 235 | struct_definition_get_field_from_index(struct definition_struct *_struct, |
0f980a35 | 236 | int index) |
11796b96 | 237 | { |
0f980a35 MD |
238 | if (index < 0) |
239 | return NULL; | |
b1a2f580 | 240 | return g_ptr_array_index(_struct->fields, index); |
11796b96 | 241 | } |
fd3382e8 MD |
242 | |
243 | uint64_t struct_declaration_len(struct declaration_struct *struct_declaration) | |
244 | { | |
245 | return struct_declaration->fields->len; | |
246 | } |