Commit | Line | Data |
---|---|---|
11796b96 | 1 | /* |
ccd7e1c8 | 2 | * struct.c |
11796b96 | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Structure Type Converter |
11796b96 | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
11796b96 | 7 | * |
ccd7e1c8 MD |
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: | |
11796b96 | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
11796b96 MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
11796b96 | 21 | |
be85c1c7 MD |
22 | #ifndef max |
23 | #define max(a, b) ((a) < (b) ? (b) : (a)) | |
24 | #endif | |
25 | ||
c054553d | 26 | static |
f6625916 | 27 | struct definition *_struct_definition_new(struct declaration *declaration, |
e1151715 | 28 | struct definition_scope *parent_scope); |
c054553d | 29 | static |
e1151715 | 30 | void _struct_definition_free(struct definition *definition); |
c054553d | 31 | |
11796b96 MD |
32 | void struct_copy(struct stream_pos *dest, const struct format *fdest, |
33 | struct stream_pos *src, const struct format *fsrc, | |
e1151715 | 34 | struct definition *definition) |
11796b96 | 35 | { |
e1151715 MD |
36 | struct definition_struct *_struct = |
37 | container_of(definition, struct definition_struct, p); | |
f6625916 | 38 | struct declaration_struct *struct_declaration = _struct->declaration; |
c054553d | 39 | unsigned long i; |
11796b96 | 40 | |
f6625916 MD |
41 | fsrc->struct_begin(src, struct_declaration); |
42 | fdest->struct_begin(dest, struct_declaration); | |
11796b96 | 43 | |
c054553d MD |
44 | for (i = 0; i < _struct->fields->len; i++) { |
45 | struct field *field = &g_array_index(_struct->fields, | |
11796b96 | 46 | struct field, i); |
f6625916 | 47 | struct declaration *field_declaration = field->definition->declaration; |
11796b96 | 48 | |
f6625916 | 49 | field_declaration->copy(dest, fdest, src, fsrc, field->definition); |
11796b96 MD |
50 | |
51 | } | |
f6625916 MD |
52 | fsrc->struct_end(src, struct_declaration); |
53 | fdest->struct_end(dest, struct_declaration); | |
11796b96 MD |
54 | } |
55 | ||
c054553d | 56 | static |
f6625916 | 57 | void _struct_declaration_free(struct declaration *declaration) |
11796b96 | 58 | { |
f6625916 MD |
59 | struct declaration_struct *struct_declaration = |
60 | container_of(declaration, struct declaration_struct, p); | |
c054553d | 61 | unsigned long i; |
4c8bfb7e | 62 | |
f6625916 MD |
63 | free_declaration_scope(struct_declaration->scope); |
64 | g_hash_table_destroy(struct_declaration->fields_by_name); | |
4c8bfb7e | 65 | |
f6625916 MD |
66 | for (i = 0; i < struct_declaration->fields->len; i++) { |
67 | struct declaration_field *declaration_field = | |
68 | &g_array_index(struct_declaration->fields, | |
69 | struct declaration_field, i); | |
70 | declaration_unref(declaration_field->declaration); | |
4c8bfb7e | 71 | } |
f6625916 MD |
72 | g_array_free(struct_declaration->fields, true); |
73 | g_free(struct_declaration); | |
11796b96 MD |
74 | } |
75 | ||
f6625916 MD |
76 | struct declaration_struct *struct_declaration_new(const char *name, |
77 | struct declaration_scope *parent_scope) | |
11796b96 | 78 | { |
f6625916 MD |
79 | struct declaration_struct *struct_declaration; |
80 | struct declaration *declaration; | |
11796b96 | 81 | |
f6625916 MD |
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, | |
e19c3d69 | 85 | g_direct_equal); |
f6625916 MD |
86 | struct_declaration->fields = g_array_sized_new(FALSE, TRUE, |
87 | sizeof(struct declaration_field), | |
e19c3d69 | 88 | DEFAULT_NR_STRUCT_FIELDS); |
f6625916 MD |
89 | struct_declaration->scope = new_declaration_scope(parent_scope); |
90 | declaration->id = CTF_TYPE_STRUCT; | |
91 | declaration->name = g_quark_from_string(name); | |
92 | declaration->alignment = 1; | |
93 | declaration->copy = struct_copy; | |
94 | declaration->declaration_free = _struct_declaration_free; | |
95 | declaration->definition_new = _struct_definition_new; | |
96 | declaration->definition_free = _struct_definition_free; | |
97 | declaration->ref = 1; | |
98 | return struct_declaration; | |
11796b96 MD |
99 | } |
100 | ||
c054553d | 101 | static |
e1151715 | 102 | struct definition * |
f6625916 | 103 | _struct_definition_new(struct declaration *declaration, |
e1151715 | 104 | struct definition_scope *parent_scope) |
11796b96 | 105 | { |
f6625916 MD |
106 | struct declaration_struct *struct_declaration = |
107 | container_of(declaration, struct declaration_struct, p); | |
e1151715 | 108 | struct definition_struct *_struct; |
ac88af75 MD |
109 | unsigned long i; |
110 | int ret; | |
c054553d | 111 | |
e1151715 | 112 | _struct = g_new(struct definition_struct, 1); |
f6625916 MD |
113 | declaration_ref(&struct_declaration->p); |
114 | _struct->p.declaration = declaration; | |
115 | _struct->declaration = struct_declaration; | |
c054553d | 116 | _struct->p.ref = 1; |
e1151715 | 117 | _struct->scope = new_definition_scope(parent_scope); |
c054553d MD |
118 | _struct->fields = g_array_sized_new(FALSE, TRUE, |
119 | sizeof(struct field), | |
120 | DEFAULT_NR_STRUCT_FIELDS); | |
f6625916 MD |
121 | g_array_set_size(_struct->fields, struct_declaration->fields->len); |
122 | for (i = 0; i < struct_declaration->fields->len; i++) { | |
123 | struct declaration_field *declaration_field = | |
124 | &g_array_index(struct_declaration->fields, | |
125 | struct declaration_field, i); | |
ac88af75 MD |
126 | struct field *field = &g_array_index(_struct->fields, |
127 | struct field, i); | |
128 | ||
f6625916 | 129 | field->name = declaration_field->name; |
e1151715 | 130 | field->definition = |
f6625916 | 131 | declaration_field->declaration->definition_new(declaration_field->declaration, |
ac88af75 | 132 | _struct->scope); |
e1151715 MD |
133 | ret = register_field_definition(field->name, |
134 | field->definition, | |
135 | _struct->scope); | |
ac88af75 MD |
136 | assert(!ret); |
137 | } | |
c054553d MD |
138 | return &_struct->p; |
139 | } | |
140 | ||
141 | static | |
e1151715 | 142 | void _struct_definition_free(struct definition *definition) |
c054553d | 143 | { |
e1151715 MD |
144 | struct definition_struct *_struct = |
145 | container_of(definition, struct definition_struct, p); | |
c054553d MD |
146 | unsigned long i; |
147 | ||
f6625916 | 148 | assert(_struct->fields->len == _struct->declaration->fields->len); |
c054553d MD |
149 | for (i = 0; i < _struct->fields->len; i++) { |
150 | struct field *field = &g_array_index(_struct->fields, | |
151 | struct field, i); | |
e1151715 | 152 | definition_unref(field->definition); |
c054553d | 153 | } |
e1151715 | 154 | free_definition_scope(_struct->scope); |
f6625916 | 155 | declaration_unref(_struct->p.declaration); |
c054553d MD |
156 | g_free(_struct); |
157 | } | |
158 | ||
f6625916 | 159 | void struct_declaration_add_field(struct declaration_struct *struct_declaration, |
e19c3d69 | 160 | const char *field_name, |
f6625916 | 161 | struct declaration *field_declaration) |
c054553d | 162 | { |
f6625916 | 163 | struct declaration_field *field; |
11796b96 MD |
164 | unsigned long index; |
165 | ||
f6625916 MD |
166 | g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1); |
167 | index = struct_declaration->fields->len - 1; /* last field (new) */ | |
168 | field = &g_array_index(struct_declaration->fields, struct declaration_field, index); | |
11796b96 | 169 | field->name = g_quark_from_string(field_name); |
f6625916 MD |
170 | declaration_ref(field_declaration); |
171 | field->declaration = field_declaration; | |
11796b96 | 172 | /* Keep index in hash rather than pointer, because array can relocate */ |
f6625916 | 173 | g_hash_table_insert(struct_declaration->fields_by_name, |
11796b96 MD |
174 | (gpointer) (unsigned long) field->name, |
175 | (gpointer) index); | |
176 | /* | |
f6625916 | 177 | * Alignment of structure is the max alignment of declarations contained |
11796b96 MD |
178 | * therein. |
179 | */ | |
f6625916 MD |
180 | struct_declaration->p.alignment = max(struct_declaration->p.alignment, |
181 | field_declaration->alignment); | |
11796b96 MD |
182 | } |
183 | ||
184 | unsigned long | |
f6625916 | 185 | struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration, |
e19c3d69 | 186 | GQuark field_name) |
11796b96 MD |
187 | { |
188 | unsigned long index; | |
189 | ||
f6625916 | 190 | index = (unsigned long) g_hash_table_lookup(struct_declaration->fields_by_name, |
4c8bfb7e | 191 | (gconstpointer) (unsigned long) field_name); |
11796b96 MD |
192 | return index; |
193 | } | |
194 | ||
c054553d MD |
195 | /* |
196 | * field returned only valid as long as the field structure is not appended to. | |
197 | */ | |
f6625916 MD |
198 | struct declaration_field * |
199 | struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration, | |
e19c3d69 | 200 | unsigned long index) |
c054553d | 201 | { |
f6625916 | 202 | return &g_array_index(struct_declaration->fields, struct declaration_field, index); |
c054553d MD |
203 | } |
204 | ||
11796b96 MD |
205 | /* |
206 | * field returned only valid as long as the field structure is not appended to. | |
207 | */ | |
208 | struct field * | |
e1151715 | 209 | struct_definition_get_field_from_index(struct definition_struct *_struct, |
e19c3d69 | 210 | unsigned long index) |
11796b96 | 211 | { |
e19c3d69 | 212 | return &g_array_index(_struct->fields, struct field, index); |
11796b96 | 213 | } |