4 * BabelTrace - Structure 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 #define max(a, b) ((a) < (b) ? (b) : (a))
39 struct bt_definition
*_struct_definition_new(struct bt_declaration
*declaration
,
40 struct definition_scope
*parent_scope
,
41 GQuark field_name
, int index
,
42 const char *root_name
);
44 void _struct_definition_free(struct bt_definition
*definition
);
46 int bt_struct_rw(struct bt_stream_pos
*ppos
, struct bt_definition
*definition
)
48 struct definition_struct
*struct_definition
=
49 container_of(definition
, struct definition_struct
, p
);
53 for (i
= 0; i
< struct_definition
->fields
->len
; i
++) {
54 struct bt_definition
*field
=
55 g_ptr_array_index(struct_definition
->fields
, i
);
56 ret
= generic_rw(ppos
, field
);
64 void _struct_declaration_free(struct bt_declaration
*declaration
)
66 struct declaration_struct
*struct_declaration
=
67 container_of(declaration
, struct declaration_struct
, p
);
70 bt_free_declaration_scope(struct_declaration
->scope
);
71 g_hash_table_destroy(struct_declaration
->fields_by_name
);
73 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
74 struct declaration_field
*declaration_field
=
75 &g_array_index(struct_declaration
->fields
,
76 struct declaration_field
, i
);
77 bt_declaration_unref(declaration_field
->declaration
);
79 g_array_free(struct_declaration
->fields
, true);
80 g_free(struct_declaration
);
83 struct declaration_struct
*
84 bt_struct_declaration_new(struct declaration_scope
*parent_scope
,
87 struct declaration_struct
*struct_declaration
;
88 struct bt_declaration
*declaration
;
90 struct_declaration
= g_new(struct declaration_struct
, 1);
91 declaration
= &struct_declaration
->p
;
92 struct_declaration
->fields_by_name
= g_hash_table_new(g_direct_hash
,
94 struct_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
95 sizeof(struct declaration_field
),
96 DEFAULT_NR_STRUCT_FIELDS
);
97 struct_declaration
->scope
= bt_new_declaration_scope(parent_scope
);
98 declaration
->id
= CTF_TYPE_STRUCT
;
99 declaration
->alignment
= max(1, min_align
);
100 declaration
->declaration_free
= _struct_declaration_free
;
101 declaration
->definition_new
= _struct_definition_new
;
102 declaration
->definition_free
= _struct_definition_free
;
103 declaration
->ref
= 1;
104 return struct_declaration
;
108 struct bt_definition
*
109 _struct_definition_new(struct bt_declaration
*declaration
,
110 struct definition_scope
*parent_scope
,
111 GQuark field_name
, int index
,
112 const char *root_name
)
114 struct declaration_struct
*struct_declaration
=
115 container_of(declaration
, struct declaration_struct
, p
);
116 struct definition_struct
*_struct
;
120 _struct
= g_new(struct definition_struct
, 1);
121 bt_declaration_ref(&struct_declaration
->p
);
122 _struct
->p
.declaration
= declaration
;
123 _struct
->declaration
= struct_declaration
;
126 * Use INT_MAX order to ensure that all fields of the parent
127 * scope are seen as being prior to this scope.
129 _struct
->p
.index
= root_name
? INT_MAX
: index
;
130 _struct
->p
.name
= field_name
;
131 _struct
->p
.path
= bt_new_definition_path(parent_scope
, field_name
, root_name
);
132 _struct
->p
.scope
= bt_new_definition_scope(parent_scope
, field_name
, root_name
);
134 ret
= bt_register_field_definition(field_name
, &_struct
->p
,
136 assert(!ret
|| ret
== -EPERM
);
138 _struct
->fields
= g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS
);
139 g_ptr_array_set_size(_struct
->fields
, struct_declaration
->fields
->len
);
140 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
141 struct declaration_field
*declaration_field
=
142 &g_array_index(struct_declaration
->fields
,
143 struct declaration_field
, i
);
144 struct bt_definition
**field
=
145 (struct bt_definition
**) &g_ptr_array_index(_struct
->fields
, i
);
147 *field
= declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
149 declaration_field
->name
, i
, NULL
);
156 for (i
--; i
>= 0; i
--) {
157 struct bt_definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
158 bt_definition_unref(field
);
160 bt_free_definition_scope(_struct
->p
.scope
);
161 bt_declaration_unref(&struct_declaration
->p
);
167 void _struct_definition_free(struct bt_definition
*definition
)
169 struct definition_struct
*_struct
=
170 container_of(definition
, struct definition_struct
, p
);
173 assert(_struct
->fields
->len
== _struct
->declaration
->fields
->len
);
174 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
175 struct bt_definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
176 bt_definition_unref(field
);
178 bt_free_definition_scope(_struct
->p
.scope
);
179 bt_declaration_unref(_struct
->p
.declaration
);
180 g_ptr_array_free(_struct
->fields
, TRUE
);
184 void bt_struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
185 const char *field_name
,
186 struct bt_declaration
*field_declaration
)
188 struct declaration_field
*field
;
191 g_array_set_size(struct_declaration
->fields
, struct_declaration
->fields
->len
+ 1);
192 index
= struct_declaration
->fields
->len
- 1; /* last field (new) */
193 field
= &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
194 field
->name
= g_quark_from_string(field_name
);
195 bt_declaration_ref(field_declaration
);
196 field
->declaration
= field_declaration
;
197 /* Keep index in hash rather than pointer, because array can relocate */
198 g_hash_table_insert(struct_declaration
->fields_by_name
,
199 (gpointer
) (unsigned long) field
->name
,
202 * Alignment of structure is the max alignment of declarations contained
205 struct_declaration
->p
.alignment
= max(struct_declaration
->p
.alignment
,
206 field_declaration
->alignment
);
210 * bt_struct_declaration_lookup_field_index - returns field index
212 * Returns the index of a field in a structure, or -1 if it does not
215 int bt_struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
221 found
= g_hash_table_lookup_extended(struct_declaration
->fields_by_name
,
222 (gconstpointer
) (unsigned long) field_name
,
226 return (int) (unsigned long) index
;
230 * field returned only valid as long as the field structure is not appended to.
232 struct declaration_field
*
233 bt_struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
238 return &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
242 * field returned only valid as long as the field structure is not appended to.
244 struct bt_definition
*
245 bt_struct_definition_get_field_from_index(const struct definition_struct
*_struct
,
250 return g_ptr_array_index(_struct
->fields
, index
);
253 uint64_t bt_struct_declaration_len(const struct declaration_struct
*struct_declaration
)
255 return struct_declaration
->fields
->len
;
This page took 0.033436 seconds and 4 git commands to generate.