001d05409d13b3579bd9b9ff5a2f09784a36aa96
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 bt_definition
*_array_definition_new(struct bt_declaration
*declaration
,
36 struct definition_scope
*parent_scope
,
37 GQuark field_name
, int index
, const char *root_name
);
39 void _array_definition_free(struct bt_definition
*definition
);
41 int bt_array_rw(struct bt_stream_pos
*pos
, struct bt_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 bt_definition
*field
=
53 g_ptr_array_index(array_definition
->elems
, i
);
54 ret
= generic_rw(pos
, field
);
62 void _array_declaration_free(struct bt_declaration
*declaration
)
64 struct declaration_array
*array_declaration
=
65 container_of(declaration
, struct declaration_array
, p
);
67 bt_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 bt_declaration
*elem_declaration
,
75 struct declaration_scope
*parent_scope
)
77 struct declaration_array
*array_declaration
;
78 struct bt_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
= bt_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
;
96 struct bt_definition
*
97 _array_definition_new(struct bt_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
= 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
,
123 array
->string
= NULL
;
126 if (array_declaration
->elem
->id
== CTF_TYPE_INTEGER
&&
127 bt_int_is_char(array_declaration
->elem
)) {
128 array
->string
= g_string_new("");
131 array
->elems
= g_ptr_array_sized_new(array_declaration
->len
);
132 g_ptr_array_set_size(array
->elems
, array_declaration
->len
);
133 for (i
= 0; i
< array_declaration
->len
; i
++) {
134 struct bt_definition
**field
;
138 str
= g_string_new("");
139 g_string_printf(str
, "[%u]", (unsigned int) i
);
140 name
= g_quark_from_string(str
->str
);
141 (void) g_string_free(str
, TRUE
);
143 field
= (struct bt_definition
**) &g_ptr_array_index(array
->elems
, i
);
144 *field
= array_declaration
->elem
->definition_new(array_declaration
->elem
,
154 for (i
--; i
>= 0; i
--) {
155 struct bt_definition
*field
;
157 field
= g_ptr_array_index(array
->elems
, i
);
158 field
->declaration
->definition_free(field
);
160 (void) g_ptr_array_free(array
->elems
, TRUE
);
161 bt_free_definition_scope(array
->p
.scope
);
162 bt_declaration_unref(array
->p
.declaration
);
168 void _array_definition_free(struct bt_definition
*definition
)
170 struct definition_array
*array
=
171 container_of(definition
, struct definition_array
, p
);
175 (void) g_string_free(array
->string
, TRUE
);
177 for (i
= 0; i
< array
->elems
->len
; i
++) {
178 struct bt_definition
*field
;
180 field
= g_ptr_array_index(array
->elems
, i
);
181 field
->declaration
->definition_free(field
);
183 (void) g_ptr_array_free(array
->elems
, TRUE
);
185 bt_free_definition_scope(array
->p
.scope
);
186 bt_declaration_unref(array
->p
.declaration
);
190 uint64_t bt_array_len(struct definition_array
*array
)
193 return array
->string
->len
;
194 return array
->elems
->len
;
197 struct bt_definition
*bt_array_index(struct definition_array
*array
, uint64_t i
)
201 if (i
>= array
->elems
->len
)
203 return g_ptr_array_index(array
->elems
, i
);
206 int bt_get_array_len(const struct bt_definition
*field
)
208 struct definition_array
*array_definition
;
209 struct declaration_array
*array_declaration
;
211 array_definition
= container_of(field
, struct definition_array
, p
);
212 array_declaration
= array_definition
->declaration
;
214 return array_declaration
->len
;
217 GString
*bt_get_char_array(const struct bt_definition
*field
)
219 struct definition_array
*array_definition
;
220 struct declaration_array
*array_declaration
;
221 struct bt_declaration
*elem
;
223 array_definition
= container_of(field
, struct definition_array
, p
);
224 array_declaration
= array_definition
->declaration
;
225 elem
= array_declaration
->elem
;
226 if (elem
->id
== CTF_TYPE_INTEGER
&& bt_int_is_char(elem
)) {
227 return array_definition
->string
;
229 fprintf(stderr
, "[warning] Extracting string\n");
This page took 0.034121 seconds and 4 git commands to generate.