4 * BabelTrace - Array Type Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/format.h>
31 #include <babeltrace/types.h>
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
);
39 void _array_definition_free(struct definition
*definition
);
41 int bt_array_rw(struct stream_pos
*pos
, struct definition
*definition
)
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
;
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
);
62 void _array_declaration_free(struct declaration
*declaration
)
64 struct declaration_array
*array_declaration
=
65 container_of(declaration
, struct declaration_array
, p
);
67 free_declaration_scope(array_declaration
->scope
);
68 bt_declaration_unref(array_declaration
->elem
);
69 g_free(array_declaration
);
72 struct declaration_array
*
73 bt_array_declaration_new(size_t len
,
74 struct declaration
*elem_declaration
,
75 struct declaration_scope
*parent_scope
)
77 struct declaration_array
*array_declaration
;
78 struct declaration
*declaration
;
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
;
92 return array_declaration
;
97 _array_definition_new(struct declaration
*declaration
,
98 struct definition_scope
*parent_scope
,
99 GQuark field_name
, int index
, const char *root_name
)
101 struct declaration_array
*array_declaration
=
102 container_of(declaration
, struct declaration_array
, p
);
103 struct definition_array
*array
;
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
;
113 * Use INT_MAX order to ensure that all fields of the parent
114 * scope are seen as being prior to this scope.
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
,
123 array
->string
= NULL
;
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
);
130 if (integer_declaration
->encoding
== CTF_STRING_UTF8
131 || integer_declaration
->encoding
== CTF_STRING_ASCII
) {
133 array
->string
= g_string_new("");
135 if (integer_declaration
->len
== CHAR_BIT
136 && integer_declaration
->p
.alignment
== CHAR_BIT
) {
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
;
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
);
154 field
= (struct definition
**) &g_ptr_array_index(array
->elems
, i
);
155 *field
= array_declaration
->elem
->definition_new(array_declaration
->elem
,
165 for (i
--; i
>= 0; i
--) {
166 struct definition
*field
;
168 field
= g_ptr_array_index(array
->elems
, i
);
169 field
->declaration
->definition_free(field
);
171 (void) g_ptr_array_free(array
->elems
, TRUE
);
172 free_definition_scope(array
->p
.scope
);
173 bt_declaration_unref(array
->p
.declaration
);
179 void _array_definition_free(struct definition
*definition
)
181 struct definition_array
*array
=
182 container_of(definition
, struct definition_array
, p
);
186 (void) g_string_free(array
->string
, TRUE
);
188 for (i
= 0; i
< array
->elems
->len
; i
++) {
189 struct definition
*field
;
191 field
= g_ptr_array_index(array
->elems
, i
);
192 field
->declaration
->definition_free(field
);
194 (void) g_ptr_array_free(array
->elems
, TRUE
);
196 free_definition_scope(array
->p
.scope
);
197 bt_declaration_unref(array
->p
.declaration
);
201 uint64_t bt_array_len(struct definition_array
*array
)
204 return array
->string
->len
;
205 return array
->elems
->len
;
208 struct definition
*bt_array_index(struct definition_array
*array
, uint64_t i
)
212 if (i
>= array
->elems
->len
)
214 return g_ptr_array_index(array
->elems
, i
);
217 int bt_get_array_len(const struct definition
*field
)
219 struct definition_array
*array_definition
;
220 struct declaration_array
*array_declaration
;
222 array_definition
= container_of(field
, struct definition_array
, p
);
223 array_declaration
= array_definition
->declaration
;
225 return array_declaration
->len
;
228 GString
*bt_get_char_array(const struct definition
*field
)
230 struct definition_array
*array_definition
;
231 struct declaration_array
*array_declaration
;
232 struct declaration
*elem
;
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
);
241 if (integer_declaration
->encoding
== CTF_STRING_UTF8
242 || integer_declaration
->encoding
== CTF_STRING_ASCII
) {
244 if (integer_declaration
->len
== CHAR_BIT
245 && integer_declaration
->p
.alignment
== CHAR_BIT
) {
247 return array_definition
->string
;
251 fprintf(stderr
, "[warning] Extracting string\n");
This page took 0.037357 seconds and 6 git commands to generate.