Commit | Line | Data |
---|---|---|
11796b96 | 1 | /* |
ccd7e1c8 | 2 | * struct.c |
11796b96 | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Structure Type Converter |
11796b96 | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
11796b96 | 7 | * |
ccd7e1c8 MD |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
11796b96 | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
11796b96 MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
11796b96 | 21 | |
be85c1c7 MD |
22 | #ifndef max |
23 | #define max(a, b) ((a) < (b) ? (b) : (a)) | |
24 | #endif | |
25 | ||
c054553d | 26 | static |
f6625916 | 27 | struct definition *_struct_definition_new(struct declaration *declaration, |
05c749e5 MD |
28 | struct definition_scope *parent_scope, |
29 | GQuark field_name, int index); | |
c054553d | 30 | static |
e1151715 | 31 | void _struct_definition_free(struct definition *definition); |
c054553d | 32 | |
d11e9c49 | 33 | void struct_rw(struct stream_pos *ppos, struct definition *definition) |
11796b96 | 34 | { |
d11e9c49 | 35 | struct definition_struct *struct_definition = |
e1151715 | 36 | container_of(definition, struct definition_struct, p); |
c054553d | 37 | unsigned long i; |
11796b96 | 38 | |
d11e9c49 MD |
39 | for (i = 0; i < struct_definition->fields->len; i++) { |
40 | struct field *field = &g_array_index(struct_definition->fields, | |
11796b96 | 41 | struct field, i); |
d11e9c49 | 42 | generic_rw(ppos, field->definition); |
11796b96 | 43 | } |
11796b96 MD |
44 | } |
45 | ||
c054553d | 46 | static |
f6625916 | 47 | void _struct_declaration_free(struct declaration *declaration) |
11796b96 | 48 | { |
f6625916 MD |
49 | struct declaration_struct *struct_declaration = |
50 | container_of(declaration, struct declaration_struct, p); | |
c054553d | 51 | unsigned long i; |
4c8bfb7e | 52 | |
f6625916 MD |
53 | free_declaration_scope(struct_declaration->scope); |
54 | g_hash_table_destroy(struct_declaration->fields_by_name); | |
4c8bfb7e | 55 | |
f6625916 MD |
56 | for (i = 0; i < struct_declaration->fields->len; i++) { |
57 | struct declaration_field *declaration_field = | |
58 | &g_array_index(struct_declaration->fields, | |
59 | struct declaration_field, i); | |
60 | declaration_unref(declaration_field->declaration); | |
4c8bfb7e | 61 | } |
f6625916 MD |
62 | g_array_free(struct_declaration->fields, true); |
63 | g_free(struct_declaration); | |
11796b96 MD |
64 | } |
65 | ||
ab4cf058 MD |
66 | struct declaration_struct * |
67 | struct_declaration_new(struct declaration_scope *parent_scope) | |
11796b96 | 68 | { |
f6625916 MD |
69 | struct declaration_struct *struct_declaration; |
70 | struct declaration *declaration; | |
11796b96 | 71 | |
f6625916 MD |
72 | struct_declaration = g_new(struct declaration_struct, 1); |
73 | declaration = &struct_declaration->p; | |
74 | struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash, | |
e19c3d69 | 75 | g_direct_equal); |
f6625916 MD |
76 | struct_declaration->fields = g_array_sized_new(FALSE, TRUE, |
77 | sizeof(struct declaration_field), | |
e19c3d69 | 78 | DEFAULT_NR_STRUCT_FIELDS); |
f6625916 MD |
79 | struct_declaration->scope = new_declaration_scope(parent_scope); |
80 | declaration->id = CTF_TYPE_STRUCT; | |
f6625916 | 81 | declaration->alignment = 1; |
f6625916 MD |
82 | declaration->declaration_free = _struct_declaration_free; |
83 | declaration->definition_new = _struct_definition_new; | |
84 | declaration->definition_free = _struct_definition_free; | |
85 | declaration->ref = 1; | |
86 | return struct_declaration; | |
11796b96 MD |
87 | } |
88 | ||
c054553d | 89 | static |
e1151715 | 90 | struct definition * |
f6625916 | 91 | _struct_definition_new(struct declaration *declaration, |
05c749e5 MD |
92 | struct definition_scope *parent_scope, |
93 | GQuark field_name, int index) | |
11796b96 | 94 | { |
f6625916 MD |
95 | struct declaration_struct *struct_declaration = |
96 | container_of(declaration, struct declaration_struct, p); | |
e1151715 | 97 | struct definition_struct *_struct; |
ac88af75 MD |
98 | unsigned long i; |
99 | int ret; | |
c054553d | 100 | |
e1151715 | 101 | _struct = g_new(struct definition_struct, 1); |
f6625916 MD |
102 | declaration_ref(&struct_declaration->p); |
103 | _struct->p.declaration = declaration; | |
104 | _struct->declaration = struct_declaration; | |
c054553d | 105 | _struct->p.ref = 1; |
05c749e5 MD |
106 | _struct->p.index = index; |
107 | _struct->scope = new_definition_scope(parent_scope, field_name); | |
c054553d MD |
108 | _struct->fields = g_array_sized_new(FALSE, TRUE, |
109 | sizeof(struct field), | |
110 | DEFAULT_NR_STRUCT_FIELDS); | |
f6625916 MD |
111 | g_array_set_size(_struct->fields, struct_declaration->fields->len); |
112 | for (i = 0; i < struct_declaration->fields->len; i++) { | |
113 | struct declaration_field *declaration_field = | |
114 | &g_array_index(struct_declaration->fields, | |
115 | struct declaration_field, i); | |
ac88af75 MD |
116 | struct field *field = &g_array_index(_struct->fields, |
117 | struct field, i); | |
118 | ||
f6625916 | 119 | field->name = declaration_field->name; |
e1151715 | 120 | field->definition = |
f6625916 | 121 | declaration_field->declaration->definition_new(declaration_field->declaration, |
05c749e5 MD |
122 | _struct->scope, |
123 | field->name, i); | |
e1151715 MD |
124 | ret = register_field_definition(field->name, |
125 | field->definition, | |
126 | _struct->scope); | |
ac88af75 MD |
127 | assert(!ret); |
128 | } | |
c054553d MD |
129 | return &_struct->p; |
130 | } | |
131 | ||
132 | static | |
e1151715 | 133 | void _struct_definition_free(struct definition *definition) |
c054553d | 134 | { |
e1151715 MD |
135 | struct definition_struct *_struct = |
136 | container_of(definition, struct definition_struct, p); | |
c054553d MD |
137 | unsigned long i; |
138 | ||
f6625916 | 139 | assert(_struct->fields->len == _struct->declaration->fields->len); |
c054553d MD |
140 | for (i = 0; i < _struct->fields->len; i++) { |
141 | struct field *field = &g_array_index(_struct->fields, | |
142 | struct field, i); | |
e1151715 | 143 | definition_unref(field->definition); |
c054553d | 144 | } |
e1151715 | 145 | free_definition_scope(_struct->scope); |
f6625916 | 146 | declaration_unref(_struct->p.declaration); |
c054553d MD |
147 | g_free(_struct); |
148 | } | |
149 | ||
f6625916 | 150 | void struct_declaration_add_field(struct declaration_struct *struct_declaration, |
e19c3d69 | 151 | const char *field_name, |
f6625916 | 152 | struct declaration *field_declaration) |
c054553d | 153 | { |
f6625916 | 154 | struct declaration_field *field; |
11796b96 MD |
155 | unsigned long index; |
156 | ||
f6625916 MD |
157 | g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1); |
158 | index = struct_declaration->fields->len - 1; /* last field (new) */ | |
159 | field = &g_array_index(struct_declaration->fields, struct declaration_field, index); | |
11796b96 | 160 | field->name = g_quark_from_string(field_name); |
f6625916 MD |
161 | declaration_ref(field_declaration); |
162 | field->declaration = field_declaration; | |
11796b96 | 163 | /* Keep index in hash rather than pointer, because array can relocate */ |
f6625916 | 164 | g_hash_table_insert(struct_declaration->fields_by_name, |
11796b96 MD |
165 | (gpointer) (unsigned long) field->name, |
166 | (gpointer) index); | |
167 | /* | |
f6625916 | 168 | * Alignment of structure is the max alignment of declarations contained |
11796b96 MD |
169 | * therein. |
170 | */ | |
f6625916 MD |
171 | struct_declaration->p.alignment = max(struct_declaration->p.alignment, |
172 | field_declaration->alignment); | |
11796b96 MD |
173 | } |
174 | ||
0f980a35 MD |
175 | /* |
176 | * struct_declaration_lookup_field_index - returns field index | |
177 | * | |
178 | * Returns the index of a field in a structure, or -1 if it does not | |
179 | * exist. | |
180 | */ | |
181 | int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration, | |
e19c3d69 | 182 | GQuark field_name) |
11796b96 | 183 | { |
0f980a35 MD |
184 | gpointer index; |
185 | gboolean found; | |
186 | ||
187 | found = g_hash_table_lookup_extended(struct_declaration->fields_by_name, | |
188 | (gconstpointer) (unsigned long) field_name, | |
189 | NULL, &index); | |
190 | if (!found) | |
191 | return -1; | |
192 | return (int) (unsigned long) index; | |
11796b96 MD |
193 | } |
194 | ||
c054553d MD |
195 | /* |
196 | * field returned only valid as long as the field structure is not appended to. | |
197 | */ | |
f6625916 MD |
198 | struct declaration_field * |
199 | struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration, | |
0f980a35 | 200 | int index) |
c054553d | 201 | { |
0f980a35 MD |
202 | if (index < 0) |
203 | return NULL; | |
f6625916 | 204 | return &g_array_index(struct_declaration->fields, struct declaration_field, index); |
c054553d MD |
205 | } |
206 | ||
11796b96 MD |
207 | /* |
208 | * field returned only valid as long as the field structure is not appended to. | |
209 | */ | |
210 | struct field * | |
e1151715 | 211 | struct_definition_get_field_from_index(struct definition_struct *_struct, |
0f980a35 | 212 | int index) |
11796b96 | 213 | { |
0f980a35 MD |
214 | if (index < 0) |
215 | return NULL; | |
e19c3d69 | 216 | return &g_array_index(_struct->fields, struct field, index); |
11796b96 | 217 | } |