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
);
43 fdest
->struct_begin(dest
, struct_declaration
);
45 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
46 struct field
*field
= &g_array_index(_struct
->fields
,
48 struct declaration
*field_declaration
= field
->definition
->declaration
;
50 field_declaration
->copy(dest
, fdest
, src
, fsrc
, field
->definition
);
53 fsrc
->struct_end(src
, struct_declaration
);
54 fdest
->struct_end(dest
, struct_declaration
);
58 void _struct_declaration_free(struct declaration
*declaration
)
60 struct declaration_struct
*struct_declaration
=
61 container_of(declaration
, struct declaration_struct
, p
);
64 free_declaration_scope(struct_declaration
->scope
);
65 g_hash_table_destroy(struct_declaration
->fields_by_name
);
67 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
68 struct declaration_field
*declaration_field
=
69 &g_array_index(struct_declaration
->fields
,
70 struct declaration_field
, i
);
71 declaration_unref(declaration_field
->declaration
);
73 g_array_free(struct_declaration
->fields
, true);
74 g_free(struct_declaration
);
77 struct declaration_struct
*struct_declaration_new(const char *name
,
78 struct declaration_scope
*parent_scope
)
80 struct declaration_struct
*struct_declaration
;
81 struct declaration
*declaration
;
83 struct_declaration
= g_new(struct declaration_struct
, 1);
84 declaration
= &struct_declaration
->p
;
85 struct_declaration
->fields_by_name
= g_hash_table_new(g_direct_hash
,
87 struct_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
88 sizeof(struct declaration_field
),
89 DEFAULT_NR_STRUCT_FIELDS
);
90 struct_declaration
->scope
= new_declaration_scope(parent_scope
);
91 declaration
->id
= CTF_TYPE_STRUCT
;
92 declaration
->name
= g_quark_from_string(name
);
93 declaration
->alignment
= 1;
94 declaration
->copy
= struct_copy
;
95 declaration
->declaration_free
= _struct_declaration_free
;
96 declaration
->definition_new
= _struct_definition_new
;
97 declaration
->definition_free
= _struct_definition_free
;
99 return struct_declaration
;
104 _struct_definition_new(struct declaration
*declaration
,
105 struct definition_scope
*parent_scope
,
106 GQuark field_name
, int index
)
108 struct declaration_struct
*struct_declaration
=
109 container_of(declaration
, struct declaration_struct
, p
);
110 struct definition_struct
*_struct
;
114 _struct
= g_new(struct definition_struct
, 1);
115 declaration_ref(&struct_declaration
->p
);
116 _struct
->p
.declaration
= declaration
;
117 _struct
->declaration
= struct_declaration
;
119 _struct
->p
.index
= index
;
120 _struct
->scope
= new_definition_scope(parent_scope
, field_name
);
121 _struct
->fields
= g_array_sized_new(FALSE
, TRUE
,
122 sizeof(struct field
),
123 DEFAULT_NR_STRUCT_FIELDS
);
124 g_array_set_size(_struct
->fields
, struct_declaration
->fields
->len
);
125 for (i
= 0; i
< struct_declaration
->fields
->len
; i
++) {
126 struct declaration_field
*declaration_field
=
127 &g_array_index(struct_declaration
->fields
,
128 struct declaration_field
, i
);
129 struct field
*field
= &g_array_index(_struct
->fields
,
132 field
->name
= declaration_field
->name
;
134 declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
137 ret
= register_field_definition(field
->name
,
146 void _struct_definition_free(struct definition
*definition
)
148 struct definition_struct
*_struct
=
149 container_of(definition
, struct definition_struct
, p
);
152 assert(_struct
->fields
->len
== _struct
->declaration
->fields
->len
);
153 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
154 struct field
*field
= &g_array_index(_struct
->fields
,
156 definition_unref(field
->definition
);
158 free_definition_scope(_struct
->scope
);
159 declaration_unref(_struct
->p
.declaration
);
163 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
164 const char *field_name
,
165 struct declaration
*field_declaration
)
167 struct declaration_field
*field
;
170 g_array_set_size(struct_declaration
->fields
, struct_declaration
->fields
->len
+ 1);
171 index
= struct_declaration
->fields
->len
- 1; /* last field (new) */
172 field
= &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
173 field
->name
= g_quark_from_string(field_name
);
174 declaration_ref(field_declaration
);
175 field
->declaration
= field_declaration
;
176 /* Keep index in hash rather than pointer, because array can relocate */
177 g_hash_table_insert(struct_declaration
->fields_by_name
,
178 (gpointer
) (unsigned long) field
->name
,
181 * Alignment of structure is the max alignment of declarations contained
184 struct_declaration
->p
.alignment
= max(struct_declaration
->p
.alignment
,
185 field_declaration
->alignment
);
189 struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
194 index
= (unsigned long) g_hash_table_lookup(struct_declaration
->fields_by_name
,
195 (gconstpointer
) (unsigned long) field_name
);
200 * field returned only valid as long as the field structure is not appended to.
202 struct declaration_field
*
203 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
206 return &g_array_index(struct_declaration
->fields
, struct declaration_field
, index
);
210 * field returned only valid as long as the field structure is not appended to.
213 struct_definition_get_field_from_index(struct definition_struct
*_struct
,
216 return &g_array_index(_struct
->fields
, struct field
, index
);
This page took 0.032701 seconds and 4 git commands to generate.