configure: check for elfutils (libelf and libdw) >= 0.154
[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
ecc54f11 35struct bt_definition *_array_definition_new(struct bt_declaration *declaration,
05c749e5 36 struct definition_scope *parent_scope,
98df1c9f 37 GQuark field_name, int index, const char *root_name);
c054553d 38static
0d69b916 39void _array_definition_free(struct bt_definition *definition);
c054553d 40
0d69b916 41int bt_array_rw(struct bt_stream_pos *pos, struct bt_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++) {
0d69b916 52 struct bt_definition *field =
b1a2f580
MD
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
ecc54f11 62void _array_declaration_free(struct bt_declaration *declaration)
d06d03db 63{
f6625916
MD
64 struct declaration_array *array_declaration =
65 container_of(declaration, struct declaration_array, p);
c054553d 66
becd02a1 67 bt_free_declaration_scope(array_declaration->scope);
e6b4b4f4 68 bt_declaration_unref(array_declaration->elem);
f6625916 69 g_free(array_declaration);
d06d03db
MD
70}
71
f6625916 72struct declaration_array *
dcaa2e7d 73 bt_array_declaration_new(size_t len,
ecc54f11 74 struct bt_declaration *elem_declaration,
f6625916 75 struct declaration_scope *parent_scope)
d06d03db 76{
f6625916 77 struct declaration_array *array_declaration;
ecc54f11 78 struct bt_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;
e6b4b4f4 83 bt_declaration_ref(elem_declaration);
f6625916 84 array_declaration->elem = elem_declaration;
becd02a1 85 array_declaration->scope = bt_new_declaration_scope(parent_scope);
f6625916 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
0d69b916 96struct bt_definition *
ecc54f11 97 _array_definition_new(struct bt_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);
e6b4b4f4 108 bt_declaration_ref(&array_declaration->p);
f6625916
MD
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;
2b77e6a6
JD
118 array->p.path = bt_new_definition_path(parent_scope, field_name, root_name);
119 array->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name);
120 ret = bt_register_field_definition(field_name, &array->p,
98df1c9f
MD
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("");
81dee1bb
MD
134 }
135 }
136
b1a2f580
MD
137 array->elems = g_ptr_array_sized_new(array_declaration->len);
138 g_ptr_array_set_size(array->elems, array_declaration->len);
0f980a35 139 for (i = 0; i < array_declaration->len; i++) {
0d69b916 140 struct bt_definition **field;
0f980a35
MD
141 GString *str;
142 GQuark name;
143
144 str = g_string_new("");
98df1c9f 145 g_string_printf(str, "[%u]", (unsigned int) i);
0f980a35
MD
146 name = g_quark_from_string(str->str);
147 (void) g_string_free(str, TRUE);
148
0d69b916 149 field = (struct bt_definition **) &g_ptr_array_index(array->elems, i);
b1a2f580 150 *field = array_declaration->elem->definition_new(array_declaration->elem,
a35173fe 151 array->p.scope,
98df1c9f
MD
152 name, i, NULL);
153 if (!*field)
154 goto error;
0f980a35 155 }
81dee1bb 156
c054553d 157 return &array->p;
98df1c9f
MD
158
159error:
160 for (i--; i >= 0; i--) {
0d69b916 161 struct bt_definition *field;
98df1c9f
MD
162
163 field = g_ptr_array_index(array->elems, i);
164 field->declaration->definition_free(field);
165 }
166 (void) g_ptr_array_free(array->elems, TRUE);
2b77e6a6 167 bt_free_definition_scope(array->p.scope);
e6b4b4f4 168 bt_declaration_unref(array->p.declaration);
98df1c9f
MD
169 g_free(array);
170 return NULL;
c054553d
MD
171}
172
173static
0d69b916 174void _array_definition_free(struct bt_definition *definition)
c054553d 175{
e1151715
MD
176 struct definition_array *array =
177 container_of(definition, struct definition_array, p);
0f980a35 178 uint64_t i;
c054553d 179
81dee1bb
MD
180 if (array->string)
181 (void) g_string_free(array->string, TRUE);
182 if (array->elems) {
183 for (i = 0; i < array->elems->len; i++) {
0d69b916 184 struct bt_definition *field;
0f980a35 185
81dee1bb
MD
186 field = g_ptr_array_index(array->elems, i);
187 field->declaration->definition_free(field);
188 }
189 (void) g_ptr_array_free(array->elems, TRUE);
0f980a35 190 }
2b77e6a6 191 bt_free_definition_scope(array->p.scope);
e6b4b4f4 192 bt_declaration_unref(array->p.declaration);
c054553d
MD
193 g_free(array);
194}
0f980a35 195
dcaa2e7d 196uint64_t bt_array_len(struct definition_array *array)
3838df27 197{
81dee1bb
MD
198 if (!array->elems)
199 return array->string->len;
3838df27
MD
200 return array->elems->len;
201}
202
0d69b916 203struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i)
0f980a35 204{
81dee1bb
MD
205 if (!array->elems)
206 return NULL;
0f980a35
MD
207 if (i >= array->elems->len)
208 return NULL;
b1a2f580 209 return g_ptr_array_index(array->elems, i);
0f980a35 210}
343c801f 211
0d69b916 212int bt_get_array_len(const struct bt_definition *field)
8673030f
JD
213{
214 struct definition_array *array_definition;
215 struct declaration_array *array_declaration;
216
217 array_definition = container_of(field, struct definition_array, p);
218 array_declaration = array_definition->declaration;
219
220 return array_declaration->len;
221}
222
0d69b916 223GString *bt_get_char_array(const struct bt_definition *field)
343c801f
JD
224{
225 struct definition_array *array_definition;
226 struct declaration_array *array_declaration;
ecc54f11 227 struct bt_declaration *elem;
343c801f
JD
228
229 array_definition = container_of(field, struct definition_array, p);
230 array_declaration = array_definition->declaration;
231 elem = array_declaration->elem;
232 if (elem->id == CTF_TYPE_INTEGER) {
233 struct declaration_integer *integer_declaration =
234 container_of(elem, struct declaration_integer, p);
235
236 if (integer_declaration->encoding == CTF_STRING_UTF8
237 || integer_declaration->encoding == CTF_STRING_ASCII) {
238
239 if (integer_declaration->len == CHAR_BIT
240 && integer_declaration->p.alignment == CHAR_BIT) {
241
242 return array_definition->string;
243 }
244 }
245 }
246 fprintf(stderr, "[warning] Extracting string\n");
247 return NULL;
248}
This page took 0.081607 seconds and 4 git commands to generate.