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.
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
23 #include <babeltrace/types.h>
27 #define max(a, b) ((a) < (b) ? (b) : (a))
31 struct definition
*_struct_definition_new(struct declaration
*declaration
,
32 struct definition_scope
*parent_scope
,
33 GQuark field_name
, int index
,
34 const char *root_name
);
36 void _struct_definition_free(struct definition
*definition
);
38 int struct_rw(struct stream_pos
*ppos
, struct definition
*definition
)
40 struct definition_struct
*struct_definition
=
41 container_of(definition
, struct definition_struct
, p
);
45 for (i
= 0; i
< struct_definition
->fields
->len
; i
++) {
46 struct definition
*field
=
47 g_ptr_array_index(struct_definition
->fields
, i
);
48 ret
= generic_rw(ppos
, field
);
56 void _struct_declaration_free(struct declaration
*declaration
)
58 struct declaration_struct
*struct_declaration
=
59 container_of(declaration
, struct declaration_struct
, p
);
62 free_declaration_scope(struct_declaration
->scope
);
63 g_hash_table_destroy(struct_declaration
->fields_by_name
);
65 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
66 struct declaration_field
*declaration_field
=
67 &g_array_index(struct_declaration
->fields
,
68 struct declaration_field
, i
);
69 declaration_unref(declaration_field
->declaration
);
71 g_array_free(struct_declaration
->fields
, true);
72 g_free(struct_declaration
);
75 struct declaration_struct
*
76 struct_declaration_new(struct declaration_scope
*parent_scope
,
79 struct declaration_struct
*struct_declaration
;
80 struct declaration
*declaration
;
82 struct_declaration
= g_new(struct declaration_struct
, 1);
83 declaration
= &struct_declaration
->p
;
84 struct_declaration
->fields_by_name
= g_hash_table_new(g_direct_hash
,
86 struct_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
87 sizeof(struct declaration_field
),
88 DEFAULT_NR_STRUCT_FIELDS
);
89 struct_declaration
->scope
= new_declaration_scope(parent_scope
);
90 declaration
->id
= CTF_TYPE_STRUCT
;
91 declaration
->alignment
= max(1, min_align
);
92 declaration
->declaration_free
= _struct_declaration_free
;
93 declaration
->definition_new
= _struct_definition_new
;
94 declaration
->definition_free
= _struct_definition_free
;
96 return struct_declaration
;
101 _struct_definition_new(struct declaration
*declaration
,
102 struct definition_scope
*parent_scope
,
103 GQuark field_name
, int index
,
104 const char *root_name
)
106 struct declaration_struct
*struct_declaration
=
107 container_of(declaration
, struct declaration_struct
, p
);
108 struct definition_struct
*_struct
;
112 _struct
= g_new(struct definition_struct
, 1);
113 declaration_ref(&struct_declaration
->p
);
114 _struct
->p
.declaration
= declaration
;
115 _struct
->declaration
= struct_declaration
;
118 * Use INT_MAX order to ensure that all fields of the parent
119 * scope are seen as being prior to this scope.
121 _struct
->p
.index
= root_name
? INT_MAX
: index
;
122 _struct
->p
.name
= field_name
;
123 _struct
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
124 _struct
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
126 ret
= register_field_definition(field_name
, &_struct
->p
,
128 assert(!ret
|| ret
== -EPERM
);
130 _struct
->fields
= g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS
);
131 g_ptr_array_set_size(_struct
->fields
, struct_declaration
->fields
->len
);
132 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
133 struct declaration_field
*declaration_field
=
134 &g_array_index(struct_declaration
->fields
,
135 struct declaration_field
, i
);
136 struct definition
**field
=
137 (struct definition
**) &g_ptr_array_index(_struct
->fields
, i
);
139 *field
= declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
141 declaration_field
->name
, i
, NULL
);
148 for (i
--; i
>= 0; i
--) {
149 struct definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
150 definition_unref(field
);
152 free_definition_scope(_struct
->p
.scope
);
153 declaration_unref(&struct_declaration
->p
);
159 void _struct_definition_free(struct definition
*definition
)
161 struct definition_struct
*_struct
=
162 container_of(definition
, struct definition_struct
, p
);
165 assert(_struct
->fields
->len
== _struct
->declaration
->fields
->len
);
166 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
167 struct definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
168 definition_unref(field
);
170 free_definition_scope(_struct
->p
.scope
);
171 declaration_unref(_struct
->p
.declaration
);
175 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
176 const char *field_name
,
177 struct declaration
*field_declaration
)
179 struct declaration_field
*field
;
182 g_array_set_size(struct_declaration
->fields
, struct_declaration
->fields
->len
+ 1);
183 index
= struct_declaration
->fields
->len
- 1; /* last field (new) */
184 field
= &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
185 field
->name
= g_quark_from_string(field_name
);
186 declaration_ref(field_declaration
);
187 field
->declaration
= field_declaration
;
188 /* Keep index in hash rather than pointer, because array can relocate */
189 g_hash_table_insert(struct_declaration
->fields_by_name
,
190 (gpointer
) (unsigned long) field
->name
,
193 * Alignment of structure is the max alignment of declarations contained
196 struct_declaration
->p
.alignment
= max(struct_declaration
->p
.alignment
,
197 field_declaration
->alignment
);
201 * struct_declaration_lookup_field_index - returns field index
203 * Returns the index of a field in a structure, or -1 if it does not
206 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
212 found
= g_hash_table_lookup_extended(struct_declaration
->fields_by_name
,
213 (gconstpointer
) (unsigned long) field_name
,
217 return (int) (unsigned long) index
;
221 * field returned only valid as long as the field structure is not appended to.
223 struct declaration_field
*
224 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
229 return &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
233 * field returned only valid as long as the field structure is not appended to.
236 struct_definition_get_field_from_index(struct definition_struct
*_struct
,
241 return g_ptr_array_index(_struct
->fields
, index
);
244 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
)
246 return struct_declaration
->fields
->len
;
This page took 0.033383 seconds and 4 git commands to generate.