ea2e28c14c29884e5b1faf8551504913d15bbb06
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>
23 #define max(a, b) ((a) < (b) ? (b) : (a))
27 struct definition
*_struct_definition_new(struct declaration
*declaration
,
28 struct definition_scope
*parent_scope
,
29 GQuark field_name
, int index
);
31 void _struct_definition_free(struct definition
*definition
);
33 void struct_copy(struct stream_pos
*dest
, const struct format
*fdest
,
34 struct stream_pos
*src
, const struct format
*fsrc
,
35 struct definition
*definition
)
37 struct definition_struct
*_struct
=
38 container_of(definition
, struct definition_struct
, p
);
39 struct declaration_struct
*struct_declaration
= _struct
->declaration
;
42 fsrc
->struct_begin(src
, struct_declaration
);
44 fdest
->struct_begin(dest
, struct_declaration
);
46 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
47 struct field
*field
= &g_array_index(_struct
->fields
,
49 struct declaration
*field_declaration
= field
->definition
->declaration
;
51 field_declaration
->copy(dest
, fdest
, src
, fsrc
, field
->definition
);
54 fsrc
->struct_end(src
, struct_declaration
);
56 fdest
->struct_end(dest
, struct_declaration
);
60 void _struct_declaration_free(struct declaration
*declaration
)
62 struct declaration_struct
*struct_declaration
=
63 container_of(declaration
, struct declaration_struct
, p
);
66 free_declaration_scope(struct_declaration
->scope
);
67 g_hash_table_destroy(struct_declaration
->fields_by_name
);
69 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
70 struct declaration_field
*declaration_field
=
71 &g_array_index(struct_declaration
->fields
,
72 struct declaration_field
, i
);
73 declaration_unref(declaration_field
->declaration
);
75 g_array_free(struct_declaration
->fields
, true);
76 g_free(struct_declaration
);
79 struct declaration_struct
*
80 struct_declaration_new(struct declaration_scope
*parent_scope
)
82 struct declaration_struct
*struct_declaration
;
83 struct declaration
*declaration
;
85 struct_declaration
= g_new(struct declaration_struct
, 1);
86 declaration
= &struct_declaration
->p
;
87 struct_declaration
->fields_by_name
= g_hash_table_new(g_direct_hash
,
89 struct_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
90 sizeof(struct declaration_field
),
91 DEFAULT_NR_STRUCT_FIELDS
);
92 struct_declaration
->scope
= new_declaration_scope(parent_scope
);
93 declaration
->id
= CTF_TYPE_STRUCT
;
94 declaration
->alignment
= 1;
95 declaration
->copy
= struct_copy
;
96 declaration
->declaration_free
= _struct_declaration_free
;
97 declaration
->definition_new
= _struct_definition_new
;
98 declaration
->definition_free
= _struct_definition_free
;
100 return struct_declaration
;
105 _struct_definition_new(struct declaration
*declaration
,
106 struct definition_scope
*parent_scope
,
107 GQuark field_name
, int index
)
109 struct declaration_struct
*struct_declaration
=
110 container_of(declaration
, struct declaration_struct
, p
);
111 struct definition_struct
*_struct
;
115 _struct
= g_new(struct definition_struct
, 1);
116 declaration_ref(&struct_declaration
->p
);
117 _struct
->p
.declaration
= declaration
;
118 _struct
->declaration
= struct_declaration
;
120 _struct
->p
.index
= index
;
121 _struct
->scope
= new_definition_scope(parent_scope
, field_name
);
122 _struct
->fields
= g_array_sized_new(FALSE
, TRUE
,
123 sizeof(struct field
),
124 DEFAULT_NR_STRUCT_FIELDS
);
125 g_array_set_size(_struct
->fields
, struct_declaration
->fields
->len
);
126 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
127 struct declaration_field
*declaration_field
=
128 &g_array_index(struct_declaration
->fields
,
129 struct declaration_field
, i
);
130 struct field
*field
= &g_array_index(_struct
->fields
,
133 field
->name
= declaration_field
->name
;
135 declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
138 ret
= register_field_definition(field
->name
,
147 void _struct_definition_free(struct definition
*definition
)
149 struct definition_struct
*_struct
=
150 container_of(definition
, struct definition_struct
, p
);
153 assert(_struct
->fields
->len
== _struct
->declaration
->fields
->len
);
154 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
155 struct field
*field
= &g_array_index(_struct
->fields
,
157 definition_unref(field
->definition
);
159 free_definition_scope(_struct
->scope
);
160 declaration_unref(_struct
->p
.declaration
);
164 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
165 const char *field_name
,
166 struct declaration
*field_declaration
)
168 struct declaration_field
*field
;
171 g_array_set_size(struct_declaration
->fields
, struct_declaration
->fields
->len
+ 1);
172 index
= struct_declaration
->fields
->len
- 1; /* last field (new) */
173 field
= &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
174 field
->name
= g_quark_from_string(field_name
);
175 declaration_ref(field_declaration
);
176 field
->declaration
= field_declaration
;
177 /* Keep index in hash rather than pointer, because array can relocate */
178 g_hash_table_insert(struct_declaration
->fields_by_name
,
179 (gpointer
) (unsigned long) field
->name
,
182 * Alignment of structure is the max alignment of declarations contained
185 struct_declaration
->p
.alignment
= max(struct_declaration
->p
.alignment
,
186 field_declaration
->alignment
);
190 * struct_declaration_lookup_field_index - returns field index
192 * Returns the index of a field in a structure, or -1 if it does not
195 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
201 found
= g_hash_table_lookup_extended(struct_declaration
->fields_by_name
,
202 (gconstpointer
) (unsigned long) field_name
,
206 return (int) (unsigned long) index
;
210 * field returned only valid as long as the field structure is not appended to.
212 struct declaration_field
*
213 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
218 return &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
222 * field returned only valid as long as the field structure is not appended to.
225 struct_definition_get_field_from_index(struct definition_struct
*_struct
,
230 return &g_array_index(_struct
->fields
, struct field
, index
);
This page took 0.037326 seconds and 4 git commands to generate.