Add missing permission notice in each source file
[babeltrace.git] / types / array.c
CommitLineData
d06d03db 1/*
ccd7e1c8 2 * array.c
d06d03db 3 *
ccd7e1c8 4 * BabelTrace - Array Type Converter
d06d03db 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d06d03db 9 *
ccd7e1c8
MD
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:
d06d03db 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
c462e188
MD
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.
d06d03db
MD
27 */
28
29#include <babeltrace/compiler.h>
4c8bfb7e 30#include <babeltrace/format.h>
3122e6f0 31#include <babeltrace/types.h>
0f980a35 32#include <inttypes.h>
d06d03db 33
c054553d 34static
f6625916 35struct definition *_array_definition_new(struct declaration *declaration,
05c749e5 36 struct definition_scope *parent_scope,
98df1c9f 37 GQuark field_name, int index, const char *root_name);
c054553d 38static
e1151715 39void _array_definition_free(struct definition *definition);
c054553d 40
c5e74408 41int array_rw(struct stream_pos *pos, struct definition *definition)
d06d03db 42{
d11e9c49 43 struct definition_array *array_definition =
e1151715 44 container_of(definition, struct definition_array, p);
d11e9c49
MD
45 const struct declaration_array *array_declaration =
46 array_definition->declaration;
c054553d 47 uint64_t i;
c5e74408 48 int ret;
d06d03db 49
d11e9c49 50 /* No need to align, because the first field will align itself. */
f6625916 51 for (i = 0; i < array_declaration->len; i++) {
b1a2f580
MD
52 struct definition *field =
53 g_ptr_array_index(array_definition->elems, i);
54 ret = generic_rw(pos, field);
c5e74408
MD
55 if (ret)
56 return ret;
d06d03db 57 }
c5e74408 58 return 0;
d06d03db
MD
59}
60
c054553d 61static
f6625916 62void _array_declaration_free(struct declaration *declaration)
d06d03db 63{
f6625916
MD
64 struct declaration_array *array_declaration =
65 container_of(declaration, struct declaration_array, p);
c054553d 66
f6625916
MD
67 free_declaration_scope(array_declaration->scope);
68 declaration_unref(array_declaration->elem);
69 g_free(array_declaration);
d06d03db
MD
70}
71
f6625916 72struct declaration_array *
ab4cf058 73 array_declaration_new(size_t len,
f6625916
MD
74 struct declaration *elem_declaration,
75 struct declaration_scope *parent_scope)
d06d03db 76{
f6625916
MD
77 struct declaration_array *array_declaration;
78 struct declaration *declaration;
d06d03db 79
f6625916
MD
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;
d06d03db 87 /* No need to align the array, the first element will align itself */
f6625916 88 declaration->alignment = 1;
f6625916
MD
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;
d06d03db 94}
c054553d
MD
95
96static
e1151715 97struct definition *
f6625916 98 _array_definition_new(struct declaration *declaration,
05c749e5 99 struct definition_scope *parent_scope,
98df1c9f 100 GQuark field_name, int index, const char *root_name)
c054553d 101{
f6625916
MD
102 struct declaration_array *array_declaration =
103 container_of(declaration, struct declaration_array, p);
e1151715 104 struct definition_array *array;
98df1c9f
MD
105 int ret;
106 int i;
c054553d 107
e1151715 108 array = g_new(struct definition_array, 1);
f6625916
MD
109 declaration_ref(&array_declaration->p);
110 array->p.declaration = declaration;
111 array->declaration = array_declaration;
c054553d 112 array->p.ref = 1;
98df1c9f
MD
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;
b1a2f580 118 array->p.name = field_name;
98df1c9f 119 array->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 120 array->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
121 ret = register_field_definition(field_name, &array->p,
122 parent_scope);
123 assert(!ret);
81dee1bb
MD
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
b1a2f580
MD
143 array->elems = g_ptr_array_sized_new(array_declaration->len);
144 g_ptr_array_set_size(array->elems, array_declaration->len);
0f980a35 145 for (i = 0; i < array_declaration->len; i++) {
b1a2f580 146 struct definition **field;
0f980a35
MD
147 GString *str;
148 GQuark name;
149
150 str = g_string_new("");
98df1c9f 151 g_string_printf(str, "[%u]", (unsigned int) i);
0f980a35
MD
152 name = g_quark_from_string(str->str);
153 (void) g_string_free(str, TRUE);
154
b1a2f580
MD
155 field = (struct definition **) &g_ptr_array_index(array->elems, i);
156 *field = array_declaration->elem->definition_new(array_declaration->elem,
a35173fe 157 array->p.scope,
98df1c9f
MD
158 name, i, NULL);
159 if (!*field)
160 goto error;
0f980a35 161 }
81dee1bb 162
c054553d 163 return &array->p;
98df1c9f
MD
164
165error:
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);
a35173fe 173 free_definition_scope(array->p.scope);
98df1c9f
MD
174 declaration_unref(array->p.declaration);
175 g_free(array);
176 return NULL;
c054553d
MD
177}
178
179static
e1151715 180void _array_definition_free(struct definition *definition)
c054553d 181{
e1151715
MD
182 struct definition_array *array =
183 container_of(definition, struct definition_array, p);
0f980a35 184 uint64_t i;
c054553d 185
81dee1bb
MD
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;
0f980a35 191
81dee1bb
MD
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);
0f980a35 196 }
a35173fe 197 free_definition_scope(array->p.scope);
f6625916 198 declaration_unref(array->p.declaration);
c054553d
MD
199 g_free(array);
200}
0f980a35 201
3838df27
MD
202uint64_t array_len(struct definition_array *array)
203{
81dee1bb
MD
204 if (!array->elems)
205 return array->string->len;
3838df27
MD
206 return array->elems->len;
207}
208
0f980a35
MD
209struct definition *array_index(struct definition_array *array, uint64_t i)
210{
81dee1bb
MD
211 if (!array->elems)
212 return NULL;
0f980a35
MD
213 if (i >= array->elems->len)
214 return NULL;
b1a2f580 215 return g_ptr_array_index(array->elems, i);
0f980a35 216}
343c801f 217
8673030f
JD
218int 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
da320b83 229GString *get_char_array(const struct definition *field)
343c801f
JD
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.059123 seconds and 4 git commands to generate.