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