4 * BabelTrace - Structure Type Converter
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
24 #define max(a, b) ((a) < (b) ? (b) : (a))
28 struct definition
*_struct_definition_new(struct declaration
*declaration
,
29 struct definition_scope
*parent_scope
,
30 GQuark field_name
, int index
,
31 const char *root_name
);
33 void _struct_definition_free(struct definition
*definition
);
35 int struct_rw(struct stream_pos
*ppos
, struct definition
*definition
)
37 struct definition_struct
*struct_definition
=
38 container_of(definition
, struct definition_struct
, p
);
42 for (i
= 0; i
< struct_definition
->fields
->len
; i
++) {
43 struct definition
*field
=
44 g_ptr_array_index(struct_definition
->fields
, i
);
45 ret
= generic_rw(ppos
, field
);
53 void _struct_declaration_free(struct declaration
*declaration
)
55 struct declaration_struct
*struct_declaration
=
56 container_of(declaration
, struct declaration_struct
, p
);
59 free_declaration_scope(struct_declaration
->scope
);
60 g_hash_table_destroy(struct_declaration
->fields_by_name
);
62 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
63 struct declaration_field
*declaration_field
=
64 &g_array_index(struct_declaration
->fields
,
65 struct declaration_field
, i
);
66 declaration_unref(declaration_field
->declaration
);
68 g_array_free(struct_declaration
->fields
, true);
69 g_free(struct_declaration
);
72 struct declaration_struct
*
73 struct_declaration_new(struct declaration_scope
*parent_scope
,
76 struct declaration_struct
*struct_declaration
;
77 struct declaration
*declaration
;
79 struct_declaration
= g_new(struct declaration_struct
, 1);
80 declaration
= &struct_declaration
->p
;
81 struct_declaration
->fields_by_name
= g_hash_table_new(g_direct_hash
,
83 struct_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
84 sizeof(struct declaration_field
),
85 DEFAULT_NR_STRUCT_FIELDS
);
86 struct_declaration
->scope
= new_declaration_scope(parent_scope
);
87 declaration
->id
= CTF_TYPE_STRUCT
;
88 declaration
->alignment
= max(1, min_align
);
89 declaration
->declaration_free
= _struct_declaration_free
;
90 declaration
->definition_new
= _struct_definition_new
;
91 declaration
->definition_free
= _struct_definition_free
;
93 return struct_declaration
;
98 _struct_definition_new(struct declaration
*declaration
,
99 struct definition_scope
*parent_scope
,
100 GQuark field_name
, int index
,
101 const char *root_name
)
103 struct declaration_struct
*struct_declaration
=
104 container_of(declaration
, struct declaration_struct
, p
);
105 struct definition_struct
*_struct
;
109 _struct
= g_new(struct definition_struct
, 1);
110 declaration_ref(&struct_declaration
->p
);
111 _struct
->p
.declaration
= declaration
;
112 _struct
->declaration
= struct_declaration
;
115 * Use INT_MAX order to ensure that all fields of the parent
116 * scope are seen as being prior to this scope.
118 _struct
->p
.index
= root_name
? INT_MAX
: index
;
119 _struct
->p
.name
= field_name
;
120 _struct
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
121 _struct
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
123 ret
= register_field_definition(field_name
, &_struct
->p
,
125 assert(!ret
|| ret
== -EPERM
);
127 _struct
->fields
= g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS
);
128 g_ptr_array_set_size(_struct
->fields
, struct_declaration
->fields
->len
);
129 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
130 struct declaration_field
*declaration_field
=
131 &g_array_index(struct_declaration
->fields
,
132 struct declaration_field
, i
);
133 struct definition
**field
=
134 (struct definition
**) &g_ptr_array_index(_struct
->fields
, i
);
136 *field
= declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
138 declaration_field
->name
, i
, NULL
);
145 for (i
--; i
>= 0; i
--) {
146 struct definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
147 definition_unref(field
);
149 free_definition_scope(_struct
->p
.scope
);
150 declaration_unref(&struct_declaration
->p
);
156 void _struct_definition_free(struct definition
*definition
)
158 struct definition_struct
*_struct
=
159 container_of(definition
, struct definition_struct
, p
);
162 assert(_struct
->fields
->len
== _struct
->declaration
->fields
->len
);
163 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
164 struct definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
165 definition_unref(field
);
167 free_definition_scope(_struct
->p
.scope
);
168 declaration_unref(_struct
->p
.declaration
);
172 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
173 const char *field_name
,
174 struct declaration
*field_declaration
)
176 struct declaration_field
*field
;
179 g_array_set_size(struct_declaration
->fields
, struct_declaration
->fields
->len
+ 1);
180 index
= struct_declaration
->fields
->len
- 1; /* last field (new) */
181 field
= &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
182 field
->name
= g_quark_from_string(field_name
);
183 declaration_ref(field_declaration
);
184 field
->declaration
= field_declaration
;
185 /* Keep index in hash rather than pointer, because array can relocate */
186 g_hash_table_insert(struct_declaration
->fields_by_name
,
187 (gpointer
) (unsigned long) field
->name
,
190 * Alignment of structure is the max alignment of declarations contained
193 struct_declaration
->p
.alignment
= max(struct_declaration
->p
.alignment
,
194 field_declaration
->alignment
);
198 * struct_declaration_lookup_field_index - returns field index
200 * Returns the index of a field in a structure, or -1 if it does not
203 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
209 found
= g_hash_table_lookup_extended(struct_declaration
->fields_by_name
,
210 (gconstpointer
) (unsigned long) field_name
,
214 return (int) (unsigned long) index
;
218 * field returned only valid as long as the field structure is not appended to.
220 struct declaration_field
*
221 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
226 return &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
230 * field returned only valid as long as the field structure is not appended to.
233 struct_definition_get_field_from_index(struct definition_struct
*_struct
,
238 return g_ptr_array_index(_struct
->fields
, index
);
241 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
)
243 return struct_declaration
->fields
->len
;
This page took 0.041437 seconds and 4 git commands to generate.