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 | |
11796b96 MD |
33 | void struct_copy(struct stream_pos *dest, const struct format *fdest, |
34 | struct stream_pos *src, const struct format *fsrc, | |
e1151715 | 35 | struct definition *definition) |
11796b96 | 36 | { |
e1151715 MD |
37 | struct definition_struct *_struct = |
38 | container_of(definition, struct definition_struct, p); | |
f6625916 | 39 | struct declaration_struct *struct_declaration = _struct->declaration; |
c054553d | 40 | unsigned long i; |
11796b96 | 41 | |
f6625916 | 42 | fsrc->struct_begin(src, struct_declaration); |
0f980a35 MD |
43 | if (fdest) |
44 | fdest->struct_begin(dest, struct_declaration); | |
11796b96 | 45 | |
c054553d MD |
46 | for (i = 0; i < _struct->fields->len; i++) { |
47 | struct field *field = &g_array_index(_struct->fields, | |
11796b96 | 48 | struct field, i); |
f6625916 | 49 | struct declaration *field_declaration = field->definition->declaration; |
11796b96 | 50 | |
f6625916 | 51 | field_declaration->copy(dest, fdest, src, fsrc, field->definition); |
11796b96 MD |
52 | |
53 | } | |
f6625916 | 54 | fsrc->struct_end(src, struct_declaration); |
0f980a35 MD |
55 | if (fdest) |
56 | fdest->struct_end(dest, struct_declaration); | |
11796b96 MD |
57 | } |
58 | ||
c054553d | 59 | static |
f6625916 | 60 | void _struct_declaration_free(struct declaration *declaration) |
11796b96 | 61 | { |
f6625916 MD |
62 | struct declaration_struct *struct_declaration = |
63 | container_of(declaration, struct declaration_struct, p); | |
c054553d | 64 | unsigned long i; |
4c8bfb7e | 65 | |
f6625916 MD |
66 | free_declaration_scope(struct_declaration->scope); |
67 | g_hash_table_destroy(struct_declaration->fields_by_name); | |
4c8bfb7e | 68 | |
f6625916 MD |
69 | for (i = 0; i < struct_declaration->fields->len; i++) { |
70 | struct declaration_field *declaration_field = | |
71 | &g_array_index(struct_declaration->fields, | |
72 | struct declaration_field, i); | |
73 | declaration_unref(declaration_field->declaration); | |
4c8bfb7e | 74 | } |
f6625916 MD |
75 | g_array_free(struct_declaration->fields, true); |
76 | g_free(struct_declaration); | |
11796b96 MD |
77 | } |
78 | ||
ab4cf058 MD |
79 | struct declaration_struct * |
80 | struct_declaration_new(struct declaration_scope *parent_scope) | |
11796b96 | 81 | { |
f6625916 MD |
82 | struct declaration_struct *struct_declaration; |
83 | struct declaration *declaration; | |
11796b96 | 84 | |
f6625916 MD |
85 | struct_declaration = g_new(struct declaration_struct, 1); |
86 | declaration = &struct_declaration->p; | |
87 | struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash, | |
e19c3d69 | 88 | g_direct_equal); |
f6625916 MD |
89 | struct_declaration->fields = g_array_sized_new(FALSE, TRUE, |
90 | sizeof(struct declaration_field), | |
e19c3d69 | 91 | DEFAULT_NR_STRUCT_FIELDS); |
f6625916 MD |
92 | struct_declaration->scope = new_declaration_scope(parent_scope); |
93 | declaration->id = CTF_TYPE_STRUCT; | |
f6625916 MD |
94 | declaration->alignment = 1; |
95 | declaration->copy = struct_copy; | |
96 | declaration->declaration_free = _struct_declaration_free; | |
97 | declaration->definition_new = _struct_definition_new; | |
98 | declaration->definition_free = _struct_definition_free; | |
99 | declaration->ref = 1; | |
100 | return struct_declaration; | |
11796b96 MD |
101 | } |
102 | ||
c054553d | 103 | static |
e1151715 | 104 | struct definition * |
f6625916 | 105 | _struct_definition_new(struct declaration *declaration, |
05c749e5 MD |
106 | struct definition_scope *parent_scope, |
107 | GQuark field_name, int index) | |
11796b96 | 108 | { |
f6625916 MD |
109 | struct declaration_struct *struct_declaration = |
110 | container_of(declaration, struct declaration_struct, p); | |
e1151715 | 111 | struct definition_struct *_struct; |
ac88af75 MD |
112 | unsigned long i; |
113 | int ret; | |
c054553d | 114 | |
e1151715 | 115 | _struct = g_new(struct definition_struct, 1); |
f6625916 MD |
116 | declaration_ref(&struct_declaration->p); |
117 | _struct->p.declaration = declaration; | |
118 | _struct->declaration = struct_declaration; | |
c054553d | 119 | _struct->p.ref = 1; |
05c749e5 MD |
120 | _struct->p.index = index; |
121 | _struct->scope = new_definition_scope(parent_scope, field_name); | |
c054553d MD |
122 | _struct->fields = g_array_sized_new(FALSE, TRUE, |
123 | sizeof(struct field), | |
124 | DEFAULT_NR_STRUCT_FIELDS); | |
f6625916 MD |
125 | g_array_set_size(_struct->fields, struct_declaration->fields->len); |
126 | for (i = 0; i < struct_declaration->fields->len; i++) { | |
127 | struct declaration_field *declaration_field = | |
128 | &g_array_index(struct_declaration->fields, | |
129 | struct declaration_field, i); | |
ac88af75 MD |
130 | struct field *field = &g_array_index(_struct->fields, |
131 | struct field, i); | |
132 | ||
f6625916 | 133 | field->name = declaration_field->name; |
e1151715 | 134 | field->definition = |
f6625916 | 135 | declaration_field->declaration->definition_new(declaration_field->declaration, |
05c749e5 MD |
136 | _struct->scope, |
137 | field->name, i); | |
e1151715 MD |
138 | ret = register_field_definition(field->name, |
139 | field->definition, | |
140 | _struct->scope); | |
ac88af75 MD |
141 | assert(!ret); |
142 | } | |
c054553d MD |
143 | return &_struct->p; |
144 | } | |
145 | ||
146 | static | |
e1151715 | 147 | void _struct_definition_free(struct definition *definition) |
c054553d | 148 | { |
e1151715 MD |
149 | struct definition_struct *_struct = |
150 | container_of(definition, struct definition_struct, p); | |
c054553d MD |
151 | unsigned long i; |
152 | ||
f6625916 | 153 | assert(_struct->fields->len == _struct->declaration->fields->len); |
c054553d MD |
154 | for (i = 0; i < _struct->fields->len; i++) { |
155 | struct field *field = &g_array_index(_struct->fields, | |
156 | struct field, i); | |
e1151715 | 157 | definition_unref(field->definition); |
c054553d | 158 | } |
e1151715 | 159 | free_definition_scope(_struct->scope); |
f6625916 | 160 | declaration_unref(_struct->p.declaration); |
c054553d MD |
161 | g_free(_struct); |
162 | } | |
163 | ||
f6625916 | 164 | void struct_declaration_add_field(struct declaration_struct *struct_declaration, |
e19c3d69 | 165 | const char *field_name, |
f6625916 | 166 | struct declaration *field_declaration) |
c054553d | 167 | { |
f6625916 | 168 | struct declaration_field *field; |
11796b96 MD |
169 | unsigned long index; |
170 | ||
f6625916 MD |
171 | g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1); |
172 | index = struct_declaration->fields->len - 1; /* last field (new) */ | |
173 | field = &g_array_index(struct_declaration->fields, struct declaration_field, index); | |
11796b96 | 174 | field->name = g_quark_from_string(field_name); |
f6625916 MD |
175 | declaration_ref(field_declaration); |
176 | field->declaration = field_declaration; | |
11796b96 | 177 | /* Keep index in hash rather than pointer, because array can relocate */ |
f6625916 | 178 | g_hash_table_insert(struct_declaration->fields_by_name, |
11796b96 MD |
179 | (gpointer) (unsigned long) field->name, |
180 | (gpointer) index); | |
181 | /* | |
f6625916 | 182 | * Alignment of structure is the max alignment of declarations contained |
11796b96 MD |
183 | * therein. |
184 | */ | |
f6625916 MD |
185 | struct_declaration->p.alignment = max(struct_declaration->p.alignment, |
186 | field_declaration->alignment); | |
11796b96 MD |
187 | } |
188 | ||
0f980a35 MD |
189 | /* |
190 | * struct_declaration_lookup_field_index - returns field index | |
191 | * | |
192 | * Returns the index of a field in a structure, or -1 if it does not | |
193 | * exist. | |
194 | */ | |
195 | int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration, | |
e19c3d69 | 196 | GQuark field_name) |
11796b96 | 197 | { |
0f980a35 MD |
198 | gpointer index; |
199 | gboolean found; | |
200 | ||
201 | found = g_hash_table_lookup_extended(struct_declaration->fields_by_name, | |
202 | (gconstpointer) (unsigned long) field_name, | |
203 | NULL, &index); | |
204 | if (!found) | |
205 | return -1; | |
206 | return (int) (unsigned long) index; | |
11796b96 MD |
207 | } |
208 | ||
c054553d MD |
209 | /* |
210 | * field returned only valid as long as the field structure is not appended to. | |
211 | */ | |
f6625916 MD |
212 | struct declaration_field * |
213 | struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration, | |
0f980a35 | 214 | int index) |
c054553d | 215 | { |
0f980a35 MD |
216 | if (index < 0) |
217 | return NULL; | |
f6625916 | 218 | return &g_array_index(struct_declaration->fields, struct declaration_field, index); |
c054553d MD |
219 | } |
220 | ||
11796b96 MD |
221 | /* |
222 | * field returned only valid as long as the field structure is not appended to. | |
223 | */ | |
224 | struct field * | |
e1151715 | 225 | struct_definition_get_field_from_index(struct definition_struct *_struct, |
0f980a35 | 226 | int index) |
11796b96 | 227 | { |
0f980a35 MD |
228 | if (index < 0) |
229 | return NULL; | |
e19c3d69 | 230 | return &g_array_index(_struct->fields, struct field, index); |
11796b96 | 231 | } |