Python bindings: add has_intersection property to TraceCollection
[babeltrace.git] / types / array.c
1 /*
2 * array.c
3 *
4 * BabelTrace - Array Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
27 */
28
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/format.h>
31 #include <babeltrace/types.h>
32 #include <inttypes.h>
33
34 static
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);
38 static
39 void _array_definition_free(struct bt_definition *definition);
40
41 int bt_array_rw(struct bt_stream_pos *pos, struct bt_definition *definition)
42 {
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;
47 uint64_t i;
48 int ret;
49
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);
55 if (ret)
56 return ret;
57 }
58 return 0;
59 }
60
61 static
62 void _array_declaration_free(struct bt_declaration *declaration)
63 {
64 struct declaration_array *array_declaration =
65 container_of(declaration, struct declaration_array, p);
66
67 bt_free_declaration_scope(array_declaration->scope);
68 bt_declaration_unref(array_declaration->elem);
69 g_free(array_declaration);
70 }
71
72 struct declaration_array *
73 bt_array_declaration_new(size_t len,
74 struct bt_declaration *elem_declaration,
75 struct declaration_scope *parent_scope)
76 {
77 struct declaration_array *array_declaration;
78 struct bt_declaration *declaration;
79
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;
91 declaration->ref = 1;
92 return array_declaration;
93 }
94
95 static
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)
100 {
101 struct declaration_array *array_declaration =
102 container_of(declaration, struct declaration_array, p);
103 struct definition_array *array;
104 int ret;
105 int i;
106
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;
111 array->p.ref = 1;
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;
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,
121 parent_scope);
122 assert(!ret);
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 }
136
137 array->elems = g_ptr_array_sized_new(array_declaration->len);
138 g_ptr_array_set_size(array->elems, array_declaration->len);
139 for (i = 0; i < array_declaration->len; i++) {
140 struct bt_definition **field;
141 GString *str;
142 GQuark name;
143
144 str = g_string_new("");
145 g_string_printf(str, "[%u]", (unsigned int) i);
146 name = g_quark_from_string(str->str);
147 (void) g_string_free(str, TRUE);
148
149 field = (struct bt_definition **) &g_ptr_array_index(array->elems, i);
150 *field = array_declaration->elem->definition_new(array_declaration->elem,
151 array->p.scope,
152 name, i, NULL);
153 if (!*field)
154 goto error;
155 }
156
157 return &array->p;
158
159 error:
160 for (i--; i >= 0; i--) {
161 struct bt_definition *field;
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);
167 bt_free_definition_scope(array->p.scope);
168 bt_declaration_unref(array->p.declaration);
169 g_free(array);
170 return NULL;
171 }
172
173 static
174 void _array_definition_free(struct bt_definition *definition)
175 {
176 struct definition_array *array =
177 container_of(definition, struct definition_array, p);
178 uint64_t i;
179
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++) {
184 struct bt_definition *field;
185
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);
190 }
191 bt_free_definition_scope(array->p.scope);
192 bt_declaration_unref(array->p.declaration);
193 g_free(array);
194 }
195
196 uint64_t bt_array_len(struct definition_array *array)
197 {
198 if (!array->elems)
199 return array->string->len;
200 return array->elems->len;
201 }
202
203 struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i)
204 {
205 if (!array->elems)
206 return NULL;
207 if (i >= array->elems->len)
208 return NULL;
209 return g_ptr_array_index(array->elems, i);
210 }
211
212 int bt_get_array_len(const struct bt_definition *field)
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
223 GString *bt_get_char_array(const struct bt_definition *field)
224 {
225 struct definition_array *array_definition;
226 struct declaration_array *array_declaration;
227 struct bt_declaration *elem;
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.050661 seconds and 4 git commands to generate.