Add missing permission notice in each source file
[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 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 declaration_unref(array_declaration->elem);
69 g_free(array_declaration);
70 }
71
72 struct declaration_array *
73 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 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 /* No need to align the array, the first element will align itself */
88 declaration->alignment = 1;
89 declaration->declaration_free = _array_declaration_free;
90 declaration->definition_new = _array_definition_new;
91 declaration->definition_free = _array_definition_free;
92 declaration->ref = 1;
93 return array_declaration;
94 }
95
96 static
97 struct definition *
98 _array_definition_new(struct declaration *declaration,
99 struct definition_scope *parent_scope,
100 GQuark field_name, int index, const char *root_name)
101 {
102 struct declaration_array *array_declaration =
103 container_of(declaration, struct declaration_array, p);
104 struct definition_array *array;
105 int ret;
106 int i;
107
108 array = g_new(struct definition_array, 1);
109 declaration_ref(&array_declaration->p);
110 array->p.declaration = declaration;
111 array->declaration = array_declaration;
112 array->p.ref = 1;
113 /*
114 * Use INT_MAX order to ensure that all fields of the parent
115 * scope are seen as being prior to this scope.
116 */
117 array->p.index = root_name ? INT_MAX : index;
118 array->p.name = field_name;
119 array->p.path = new_definition_path(parent_scope, field_name, root_name);
120 array->p.scope = new_definition_scope(parent_scope, field_name, root_name);
121 ret = register_field_definition(field_name, &array->p,
122 parent_scope);
123 assert(!ret);
124 array->string = NULL;
125 array->elems = NULL;
126
127 if (array_declaration->elem->id == CTF_TYPE_INTEGER) {
128 struct declaration_integer *integer_declaration =
129 container_of(array_declaration->elem, struct declaration_integer, p);
130
131 if (integer_declaration->encoding == CTF_STRING_UTF8
132 || integer_declaration->encoding == CTF_STRING_ASCII) {
133
134 array->string = g_string_new("");
135
136 if (integer_declaration->len == CHAR_BIT
137 && integer_declaration->p.alignment == CHAR_BIT) {
138 return &array->p;
139 }
140 }
141 }
142
143 array->elems = g_ptr_array_sized_new(array_declaration->len);
144 g_ptr_array_set_size(array->elems, array_declaration->len);
145 for (i = 0; i < array_declaration->len; i++) {
146 struct definition **field;
147 GString *str;
148 GQuark name;
149
150 str = g_string_new("");
151 g_string_printf(str, "[%u]", (unsigned int) i);
152 name = g_quark_from_string(str->str);
153 (void) g_string_free(str, TRUE);
154
155 field = (struct definition **) &g_ptr_array_index(array->elems, i);
156 *field = array_declaration->elem->definition_new(array_declaration->elem,
157 array->p.scope,
158 name, i, NULL);
159 if (!*field)
160 goto error;
161 }
162
163 return &array->p;
164
165 error:
166 for (i--; i >= 0; i--) {
167 struct definition *field;
168
169 field = g_ptr_array_index(array->elems, i);
170 field->declaration->definition_free(field);
171 }
172 (void) g_ptr_array_free(array->elems, TRUE);
173 free_definition_scope(array->p.scope);
174 declaration_unref(array->p.declaration);
175 g_free(array);
176 return NULL;
177 }
178
179 static
180 void _array_definition_free(struct definition *definition)
181 {
182 struct definition_array *array =
183 container_of(definition, struct definition_array, p);
184 uint64_t i;
185
186 if (array->string)
187 (void) g_string_free(array->string, TRUE);
188 if (array->elems) {
189 for (i = 0; i < array->elems->len; i++) {
190 struct definition *field;
191
192 field = g_ptr_array_index(array->elems, i);
193 field->declaration->definition_free(field);
194 }
195 (void) g_ptr_array_free(array->elems, TRUE);
196 }
197 free_definition_scope(array->p.scope);
198 declaration_unref(array->p.declaration);
199 g_free(array);
200 }
201
202 uint64_t array_len(struct definition_array *array)
203 {
204 if (!array->elems)
205 return array->string->len;
206 return array->elems->len;
207 }
208
209 struct definition *array_index(struct definition_array *array, uint64_t i)
210 {
211 if (!array->elems)
212 return NULL;
213 if (i >= array->elems->len)
214 return NULL;
215 return g_ptr_array_index(array->elems, i);
216 }
217
218 int get_array_len(const struct definition *field)
219 {
220 struct definition_array *array_definition;
221 struct declaration_array *array_declaration;
222
223 array_definition = container_of(field, struct definition_array, p);
224 array_declaration = array_definition->declaration;
225
226 return array_declaration->len;
227 }
228
229 GString *get_char_array(const struct definition *field)
230 {
231 struct definition_array *array_definition;
232 struct declaration_array *array_declaration;
233 struct declaration *elem;
234
235 array_definition = container_of(field, struct definition_array, p);
236 array_declaration = array_definition->declaration;
237 elem = array_declaration->elem;
238 if (elem->id == CTF_TYPE_INTEGER) {
239 struct declaration_integer *integer_declaration =
240 container_of(elem, struct declaration_integer, p);
241
242 if (integer_declaration->encoding == CTF_STRING_UTF8
243 || integer_declaration->encoding == CTF_STRING_ASCII) {
244
245 if (integer_declaration->len == CHAR_BIT
246 && integer_declaration->p.alignment == CHAR_BIT) {
247
248 return array_definition->string;
249 }
250 }
251 }
252 fprintf(stderr, "[warning] Extracting string\n");
253 return NULL;
254 }
This page took 0.050173 seconds and 4 git commands to generate.