namespace the struct functions
[babeltrace.git] / types / array.c
1 /*
2 * array.c
3 *
4 * BabelTrace - Array Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/format.h>
31 #include <babeltrace/types.h>
32 #include <inttypes.h>
33
34 static
35 struct definition *_array_definition_new(struct declaration *declaration,
36 struct definition_scope *parent_scope,
37 GQuark field_name, int index, const char *root_name);
38 static
39 void _array_definition_free(struct definition *definition);
40
41 int bt_array_rw(struct stream_pos *pos, struct definition *definition)
42 {
43 struct definition_array *array_definition =
44 container_of(definition, struct definition_array, p);
45 const struct declaration_array *array_declaration =
46 array_definition->declaration;
47 uint64_t i;
48 int ret;
49
50 /* No need to align, because the first field will align itself. */
51 for (i = 0; i < array_declaration->len; i++) {
52 struct definition *field =
53 g_ptr_array_index(array_definition->elems, i);
54 ret = generic_rw(pos, field);
55 if (ret)
56 return ret;
57 }
58 return 0;
59 }
60
61 static
62 void _array_declaration_free(struct declaration *declaration)
63 {
64 struct declaration_array *array_declaration =
65 container_of(declaration, struct declaration_array, p);
66
67 free_declaration_scope(array_declaration->scope);
68 bt_declaration_unref(array_declaration->elem);
69 g_free(array_declaration);
70 }
71
72 struct declaration_array *
73 bt_array_declaration_new(size_t len,
74 struct declaration *elem_declaration,
75 struct declaration_scope *parent_scope)
76 {
77 struct declaration_array *array_declaration;
78 struct declaration *declaration;
79
80 array_declaration = g_new(struct declaration_array, 1);
81 declaration = &array_declaration->p;
82 array_declaration->len = len;
83 bt_declaration_ref(elem_declaration);
84 array_declaration->elem = elem_declaration;
85 array_declaration->scope = new_declaration_scope(parent_scope);
86 declaration->id = CTF_TYPE_ARRAY;
87 declaration->alignment = elem_declaration->alignment;
88 declaration->declaration_free = _array_declaration_free;
89 declaration->definition_new = _array_definition_new;
90 declaration->definition_free = _array_definition_free;
91 declaration->ref = 1;
92 return array_declaration;
93 }
94
95 static
96 struct definition *
97 _array_definition_new(struct declaration *declaration,
98 struct definition_scope *parent_scope,
99 GQuark field_name, int index, const char *root_name)
100 {
101 struct declaration_array *array_declaration =
102 container_of(declaration, struct declaration_array, p);
103 struct definition_array *array;
104 int ret;
105 int i;
106
107 array = g_new(struct definition_array, 1);
108 bt_declaration_ref(&array_declaration->p);
109 array->p.declaration = declaration;
110 array->declaration = array_declaration;
111 array->p.ref = 1;
112 /*
113 * Use INT_MAX order to ensure that all fields of the parent
114 * scope are seen as being prior to this scope.
115 */
116 array->p.index = root_name ? INT_MAX : index;
117 array->p.name = field_name;
118 array->p.path = new_definition_path(parent_scope, field_name, root_name);
119 array->p.scope = new_definition_scope(parent_scope, field_name, root_name);
120 ret = register_field_definition(field_name, &array->p,
121 parent_scope);
122 assert(!ret);
123 array->string = NULL;
124 array->elems = NULL;
125
126 if (array_declaration->elem->id == CTF_TYPE_INTEGER) {
127 struct declaration_integer *integer_declaration =
128 container_of(array_declaration->elem, struct declaration_integer, p);
129
130 if (integer_declaration->encoding == CTF_STRING_UTF8
131 || integer_declaration->encoding == CTF_STRING_ASCII) {
132
133 array->string = g_string_new("");
134
135 if (integer_declaration->len == CHAR_BIT
136 && integer_declaration->p.alignment == CHAR_BIT) {
137 return &array->p;
138 }
139 }
140 }
141
142 array->elems = g_ptr_array_sized_new(array_declaration->len);
143 g_ptr_array_set_size(array->elems, array_declaration->len);
144 for (i = 0; i < array_declaration->len; i++) {
145 struct definition **field;
146 GString *str;
147 GQuark name;
148
149 str = g_string_new("");
150 g_string_printf(str, "[%u]", (unsigned int) i);
151 name = g_quark_from_string(str->str);
152 (void) g_string_free(str, TRUE);
153
154 field = (struct definition **) &g_ptr_array_index(array->elems, i);
155 *field = array_declaration->elem->definition_new(array_declaration->elem,
156 array->p.scope,
157 name, i, NULL);
158 if (!*field)
159 goto error;
160 }
161
162 return &array->p;
163
164 error:
165 for (i--; i >= 0; i--) {
166 struct definition *field;
167
168 field = g_ptr_array_index(array->elems, i);
169 field->declaration->definition_free(field);
170 }
171 (void) g_ptr_array_free(array->elems, TRUE);
172 free_definition_scope(array->p.scope);
173 bt_declaration_unref(array->p.declaration);
174 g_free(array);
175 return NULL;
176 }
177
178 static
179 void _array_definition_free(struct definition *definition)
180 {
181 struct definition_array *array =
182 container_of(definition, struct definition_array, p);
183 uint64_t i;
184
185 if (array->string)
186 (void) g_string_free(array->string, TRUE);
187 if (array->elems) {
188 for (i = 0; i < array->elems->len; i++) {
189 struct definition *field;
190
191 field = g_ptr_array_index(array->elems, i);
192 field->declaration->definition_free(field);
193 }
194 (void) g_ptr_array_free(array->elems, TRUE);
195 }
196 free_definition_scope(array->p.scope);
197 bt_declaration_unref(array->p.declaration);
198 g_free(array);
199 }
200
201 uint64_t bt_array_len(struct definition_array *array)
202 {
203 if (!array->elems)
204 return array->string->len;
205 return array->elems->len;
206 }
207
208 struct definition *bt_array_index(struct definition_array *array, uint64_t i)
209 {
210 if (!array->elems)
211 return NULL;
212 if (i >= array->elems->len)
213 return NULL;
214 return g_ptr_array_index(array->elems, i);
215 }
216
217 int bt_get_array_len(const struct definition *field)
218 {
219 struct definition_array *array_definition;
220 struct declaration_array *array_declaration;
221
222 array_definition = container_of(field, struct definition_array, p);
223 array_declaration = array_definition->declaration;
224
225 return array_declaration->len;
226 }
227
228 GString *bt_get_char_array(const struct definition *field)
229 {
230 struct definition_array *array_definition;
231 struct declaration_array *array_declaration;
232 struct declaration *elem;
233
234 array_definition = container_of(field, struct definition_array, p);
235 array_declaration = array_definition->declaration;
236 elem = array_declaration->elem;
237 if (elem->id == CTF_TYPE_INTEGER) {
238 struct declaration_integer *integer_declaration =
239 container_of(elem, struct declaration_integer, p);
240
241 if (integer_declaration->encoding == CTF_STRING_UTF8
242 || integer_declaration->encoding == CTF_STRING_ASCII) {
243
244 if (integer_declaration->len == CHAR_BIT
245 && integer_declaration->p.alignment == CHAR_BIT) {
246
247 return array_definition->string;
248 }
249 }
250 }
251 fprintf(stderr, "[warning] Extracting string\n");
252 return NULL;
253 }
This page took 0.033393 seconds and 4 git commands to generate.