namespace the array functions
[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
dcaa2e7d 41int bt_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 *
dcaa2e7d 73 bt_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;
470f467f 87 declaration->alignment = elem_declaration->alignment;
f6625916
MD
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;
d06d03db 93}
c054553d
MD
94
95static
e1151715 96struct definition *
f6625916 97 _array_definition_new(struct declaration *declaration,
05c749e5 98 struct definition_scope *parent_scope,
98df1c9f 99 GQuark field_name, int index, const char *root_name)
c054553d 100{
f6625916
MD
101 struct declaration_array *array_declaration =
102 container_of(declaration, struct declaration_array, p);
e1151715 103 struct definition_array *array;
98df1c9f
MD
104 int ret;
105 int i;
c054553d 106
e1151715 107 array = g_new(struct definition_array, 1);
f6625916
MD
108 declaration_ref(&array_declaration->p);
109 array->p.declaration = declaration;
110 array->declaration = array_declaration;
c054553d 111 array->p.ref = 1;
98df1c9f
MD
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;
b1a2f580 117 array->p.name = field_name;
98df1c9f 118 array->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 119 array->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
120 ret = register_field_definition(field_name, &array->p,
121 parent_scope);
122 assert(!ret);
81dee1bb
MD
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
b1a2f580
MD
142 array->elems = g_ptr_array_sized_new(array_declaration->len);
143 g_ptr_array_set_size(array->elems, array_declaration->len);
0f980a35 144 for (i = 0; i < array_declaration->len; i++) {
b1a2f580 145 struct definition **field;
0f980a35
MD
146 GString *str;
147 GQuark name;
148
149 str = g_string_new("");
98df1c9f 150 g_string_printf(str, "[%u]", (unsigned int) i);
0f980a35
MD
151 name = g_quark_from_string(str->str);
152 (void) g_string_free(str, TRUE);
153
b1a2f580
MD
154 field = (struct definition **) &g_ptr_array_index(array->elems, i);
155 *field = array_declaration->elem->definition_new(array_declaration->elem,
a35173fe 156 array->p.scope,
98df1c9f
MD
157 name, i, NULL);
158 if (!*field)
159 goto error;
0f980a35 160 }
81dee1bb 161
c054553d 162 return &array->p;
98df1c9f
MD
163
164error:
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);
a35173fe 172 free_definition_scope(array->p.scope);
98df1c9f
MD
173 declaration_unref(array->p.declaration);
174 g_free(array);
175 return NULL;
c054553d
MD
176}
177
178static
e1151715 179void _array_definition_free(struct definition *definition)
c054553d 180{
e1151715
MD
181 struct definition_array *array =
182 container_of(definition, struct definition_array, p);
0f980a35 183 uint64_t i;
c054553d 184
81dee1bb
MD
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;
0f980a35 190
81dee1bb
MD
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);
0f980a35 195 }
a35173fe 196 free_definition_scope(array->p.scope);
f6625916 197 declaration_unref(array->p.declaration);
c054553d
MD
198 g_free(array);
199}
0f980a35 200
dcaa2e7d 201uint64_t bt_array_len(struct definition_array *array)
3838df27 202{
81dee1bb
MD
203 if (!array->elems)
204 return array->string->len;
3838df27
MD
205 return array->elems->len;
206}
207
dcaa2e7d 208struct definition *bt_array_index(struct definition_array *array, uint64_t i)
0f980a35 209{
81dee1bb
MD
210 if (!array->elems)
211 return NULL;
0f980a35
MD
212 if (i >= array->elems->len)
213 return NULL;
b1a2f580 214 return g_ptr_array_index(array->elems, i);
0f980a35 215}
343c801f 216
dcaa2e7d 217int bt_get_array_len(const struct definition *field)
8673030f
JD
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
dcaa2e7d 228GString *bt_get_char_array(const struct definition *field)
343c801f
JD
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.03862 seconds and 4 git commands to generate.