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>
26 #define max(a, b) ((a) < (b) ? (b) : (a))
30 struct definition
*_struct_definition_new(struct declaration
*declaration
,
31 struct definition_scope
*parent_scope
,
32 GQuark field_name
, int index
,
33 const char *root_name
);
35 void _struct_definition_free(struct definition
*definition
);
37 int struct_rw(struct stream_pos
*ppos
, struct definition
*definition
)
39 struct definition_struct
*struct_definition
=
40 container_of(definition
, struct definition_struct
, p
);
44 for (i
= 0; i
< struct_definition
->fields
->len
; i
++) {
45 struct definition
*field
=
46 g_ptr_array_index(struct_definition
->fields
, i
);
47 ret
= generic_rw(ppos
, field
);
55 void _struct_declaration_free(struct declaration
*declaration
)
57 struct declaration_struct
*struct_declaration
=
58 container_of(declaration
, struct declaration_struct
, p
);
61 free_declaration_scope(struct_declaration
->scope
);
62 g_hash_table_destroy(struct_declaration
->fields_by_name
);
64 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
65 struct declaration_field
*declaration_field
=
66 &g_array_index(struct_declaration
->fields
,
67 struct declaration_field
, i
);
68 declaration_unref(declaration_field
->declaration
);
70 g_array_free(struct_declaration
->fields
, true);
71 g_free(struct_declaration
);
74 struct declaration_struct
*
75 struct_declaration_new(struct declaration_scope
*parent_scope
,
78 struct declaration_struct
*struct_declaration
;
79 struct declaration
*declaration
;
81 struct_declaration
= g_new(struct declaration_struct
, 1);
82 declaration
= &struct_declaration
->p
;
83 struct_declaration
->fields_by_name
= g_hash_table_new(g_direct_hash
,
85 struct_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
86 sizeof(struct declaration_field
),
87 DEFAULT_NR_STRUCT_FIELDS
);
88 struct_declaration
->scope
= new_declaration_scope(parent_scope
);
89 declaration
->id
= CTF_TYPE_STRUCT
;
90 declaration
->alignment
= max(1, min_align
);
91 declaration
->declaration_free
= _struct_declaration_free
;
92 declaration
->definition_new
= _struct_definition_new
;
93 declaration
->definition_free
= _struct_definition_free
;
95 return struct_declaration
;
100 _struct_definition_new(struct declaration
*declaration
,
101 struct definition_scope
*parent_scope
,
102 GQuark field_name
, int index
,
103 const char *root_name
)
105 struct declaration_struct
*struct_declaration
=
106 container_of(declaration
, struct declaration_struct
, p
);
107 struct definition_struct
*_struct
;
111 _struct
= g_new(struct definition_struct
, 1);
112 declaration_ref(&struct_declaration
->p
);
113 _struct
->p
.declaration
= declaration
;
114 _struct
->declaration
= struct_declaration
;
117 * Use INT_MAX order to ensure that all fields of the parent
118 * scope are seen as being prior to this scope.
120 _struct
->p
.index
= root_name
? INT_MAX
: index
;
121 _struct
->p
.name
= field_name
;
122 _struct
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
123 _struct
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
125 ret
= register_field_definition(field_name
, &_struct
->p
,
127 assert(!ret
|| ret
== -EPERM
);
129 _struct
->fields
= g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS
);
130 g_ptr_array_set_size(_struct
->fields
, struct_declaration
->fields
->len
);
131 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
132 struct declaration_field
*declaration_field
=
133 &g_array_index(struct_declaration
->fields
,
134 struct declaration_field
, i
);
135 struct definition
**field
=
136 (struct definition
**) &g_ptr_array_index(_struct
->fields
, i
);
138 *field
= declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
140 declaration_field
->name
, i
, NULL
);
147 for (i
--; i
>= 0; i
--) {
148 struct definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
149 definition_unref(field
);
151 free_definition_scope(_struct
->p
.scope
);
152 declaration_unref(&struct_declaration
->p
);
158 void _struct_definition_free(struct definition
*definition
)
160 struct definition_struct
*_struct
=
161 container_of(definition
, struct definition_struct
, p
);
164 assert(_struct
->fields
->len
== _struct
->declaration
->fields
->len
);
165 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
166 struct definition
*field
= g_ptr_array_index(_struct
->fields
, i
);
167 definition_unref(field
);
169 free_definition_scope(_struct
->p
.scope
);
170 declaration_unref(_struct
->p
.declaration
);
174 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
175 const char *field_name
,
176 struct declaration
*field_declaration
)
178 struct declaration_field
*field
;
181 g_array_set_size(struct_declaration
->fields
, struct_declaration
->fields
->len
+ 1);
182 index
= struct_declaration
->fields
->len
- 1; /* last field (new) */
183 field
= &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
184 field
->name
= g_quark_from_string(field_name
);
185 declaration_ref(field_declaration
);
186 field
->declaration
= field_declaration
;
187 /* Keep index in hash rather than pointer, because array can relocate */
188 g_hash_table_insert(struct_declaration
->fields_by_name
,
189 (gpointer
) (unsigned long) field
->name
,
192 * Alignment of structure is the max alignment of declarations contained
195 struct_declaration
->p
.alignment
= max(struct_declaration
->p
.alignment
,
196 field_declaration
->alignment
);
200 * struct_declaration_lookup_field_index - returns field index
202 * Returns the index of a field in a structure, or -1 if it does not
205 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
211 found
= g_hash_table_lookup_extended(struct_declaration
->fields_by_name
,
212 (gconstpointer
) (unsigned long) field_name
,
216 return (int) (unsigned long) index
;
220 * field returned only valid as long as the field structure is not appended to.
222 struct declaration_field
*
223 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
228 return &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
232 * field returned only valid as long as the field structure is not appended to.
235 struct_definition_get_field_from_index(struct definition_struct
*_struct
,
240 return g_ptr_array_index(_struct
->fields
, index
);
243 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
)
245 return struct_declaration
->fields
->len
;
This page took 0.035161 seconds and 4 git commands to generate.