namespace the variant functions
[babeltrace.git] / types / struct.c
CommitLineData
11796b96 1/*
ccd7e1c8 2 * struct.c
11796b96 3 *
ccd7e1c8 4 * BabelTrace - Structure Type Converter
11796b96 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11796b96 9 *
ccd7e1c8
MD
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:
11796b96 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
c462e188
MD
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
11796b96
MD
27 */
28
29#include <babeltrace/compiler.h>
4c8bfb7e 30#include <babeltrace/format.h>
3122e6f0 31#include <babeltrace/types.h>
98df1c9f 32#include <errno.h>
11796b96 33
be85c1c7
MD
34#ifndef max
35#define max(a, b) ((a) < (b) ? (b) : (a))
36#endif
37
c054553d 38static
f6625916 39struct definition *_struct_definition_new(struct declaration *declaration,
05c749e5 40 struct definition_scope *parent_scope,
98df1c9f
MD
41 GQuark field_name, int index,
42 const char *root_name);
c054553d 43static
e1151715 44void _struct_definition_free(struct definition *definition);
c054553d 45
c8c98132 46int bt_struct_rw(struct stream_pos *ppos, struct definition *definition)
11796b96 47{
d11e9c49 48 struct definition_struct *struct_definition =
e1151715 49 container_of(definition, struct definition_struct, p);
c054553d 50 unsigned long i;
c5e74408 51 int ret;
11796b96 52
d11e9c49 53 for (i = 0; i < struct_definition->fields->len; i++) {
b1a2f580
MD
54 struct definition *field =
55 g_ptr_array_index(struct_definition->fields, i);
56 ret = generic_rw(ppos, field);
c5e74408
MD
57 if (ret)
58 return ret;
11796b96 59 }
c5e74408 60 return 0;
11796b96
MD
61}
62
c054553d 63static
f6625916 64void _struct_declaration_free(struct declaration *declaration)
11796b96 65{
f6625916
MD
66 struct declaration_struct *struct_declaration =
67 container_of(declaration, struct declaration_struct, p);
c054553d 68 unsigned long i;
4c8bfb7e 69
becd02a1 70 bt_free_declaration_scope(struct_declaration->scope);
f6625916 71 g_hash_table_destroy(struct_declaration->fields_by_name);
4c8bfb7e 72
f6625916
MD
73 for (i = 0; i < struct_declaration->fields->len; i++) {
74 struct declaration_field *declaration_field =
75 &g_array_index(struct_declaration->fields,
76 struct declaration_field, i);
e6b4b4f4 77 bt_declaration_unref(declaration_field->declaration);
4c8bfb7e 78 }
f6625916
MD
79 g_array_free(struct_declaration->fields, true);
80 g_free(struct_declaration);
11796b96
MD
81}
82
ab4cf058 83struct declaration_struct *
c8c98132 84 bt_struct_declaration_new(struct declaration_scope *parent_scope,
b7e35bad 85 uint64_t min_align)
11796b96 86{
f6625916
MD
87 struct declaration_struct *struct_declaration;
88 struct declaration *declaration;
11796b96 89
f6625916
MD
90 struct_declaration = g_new(struct declaration_struct, 1);
91 declaration = &struct_declaration->p;
92 struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash,
e19c3d69 93 g_direct_equal);
f6625916
MD
94 struct_declaration->fields = g_array_sized_new(FALSE, TRUE,
95 sizeof(struct declaration_field),
e19c3d69 96 DEFAULT_NR_STRUCT_FIELDS);
becd02a1 97 struct_declaration->scope = bt_new_declaration_scope(parent_scope);
f6625916 98 declaration->id = CTF_TYPE_STRUCT;
b7e35bad 99 declaration->alignment = max(1, min_align);
f6625916
MD
100 declaration->declaration_free = _struct_declaration_free;
101 declaration->definition_new = _struct_definition_new;
102 declaration->definition_free = _struct_definition_free;
103 declaration->ref = 1;
104 return struct_declaration;
11796b96
MD
105}
106
c054553d 107static
e1151715 108struct definition *
f6625916 109 _struct_definition_new(struct declaration *declaration,
05c749e5 110 struct definition_scope *parent_scope,
98df1c9f
MD
111 GQuark field_name, int index,
112 const char *root_name)
11796b96 113{
f6625916
MD
114 struct declaration_struct *struct_declaration =
115 container_of(declaration, struct declaration_struct, p);
e1151715 116 struct definition_struct *_struct;
98df1c9f 117 int i;
ac88af75 118 int ret;
c054553d 119
e1151715 120 _struct = g_new(struct definition_struct, 1);
e6b4b4f4 121 bt_declaration_ref(&struct_declaration->p);
f6625916
MD
122 _struct->p.declaration = declaration;
123 _struct->declaration = struct_declaration;
c054553d 124 _struct->p.ref = 1;
98df1c9f
MD
125 /*
126 * Use INT_MAX order to ensure that all fields of the parent
127 * scope are seen as being prior to this scope.
128 */
129 _struct->p.index = root_name ? INT_MAX : index;
b1a2f580 130 _struct->p.name = field_name;
98df1c9f 131 _struct->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 132 _struct->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
133
134 ret = register_field_definition(field_name, &_struct->p,
135 parent_scope);
136 assert(!ret || ret == -EPERM);
137
b1a2f580
MD
138 _struct->fields = g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS);
139 g_ptr_array_set_size(_struct->fields, struct_declaration->fields->len);
f6625916
MD
140 for (i = 0; i < struct_declaration->fields->len; i++) {
141 struct declaration_field *declaration_field =
142 &g_array_index(struct_declaration->fields,
143 struct declaration_field, i);
b1a2f580
MD
144 struct definition **field =
145 (struct definition **) &g_ptr_array_index(_struct->fields, i);
ac88af75 146
b1a2f580 147 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
a35173fe 148 _struct->p.scope,
98df1c9f
MD
149 declaration_field->name, i, NULL);
150 if (!*field)
151 goto error;
ac88af75 152 }
c054553d 153 return &_struct->p;
98df1c9f
MD
154
155error:
156 for (i--; i >= 0; i--) {
157 struct definition *field = g_ptr_array_index(_struct->fields, i);
13fad8b6 158 bt_definition_unref(field);
98df1c9f 159 }
a35173fe 160 free_definition_scope(_struct->p.scope);
e6b4b4f4 161 bt_declaration_unref(&struct_declaration->p);
98df1c9f
MD
162 g_free(_struct);
163 return NULL;
c054553d
MD
164}
165
166static
e1151715 167void _struct_definition_free(struct definition *definition)
c054553d 168{
e1151715
MD
169 struct definition_struct *_struct =
170 container_of(definition, struct definition_struct, p);
c054553d
MD
171 unsigned long i;
172
f6625916 173 assert(_struct->fields->len == _struct->declaration->fields->len);
c054553d 174 for (i = 0; i < _struct->fields->len; i++) {
b1a2f580 175 struct definition *field = g_ptr_array_index(_struct->fields, i);
13fad8b6 176 bt_definition_unref(field);
c054553d 177 }
a35173fe 178 free_definition_scope(_struct->p.scope);
e6b4b4f4 179 bt_declaration_unref(_struct->p.declaration);
15d4fe3c 180 g_ptr_array_free(_struct->fields, TRUE);
c054553d
MD
181 g_free(_struct);
182}
183
c8c98132 184void bt_struct_declaration_add_field(struct declaration_struct *struct_declaration,
e19c3d69 185 const char *field_name,
f6625916 186 struct declaration *field_declaration)
c054553d 187{
f6625916 188 struct declaration_field *field;
11796b96
MD
189 unsigned long index;
190
f6625916
MD
191 g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1);
192 index = struct_declaration->fields->len - 1; /* last field (new) */
193 field = &g_array_index(struct_declaration->fields, struct declaration_field, index);
11796b96 194 field->name = g_quark_from_string(field_name);
e6b4b4f4 195 bt_declaration_ref(field_declaration);
f6625916 196 field->declaration = field_declaration;
11796b96 197 /* Keep index in hash rather than pointer, because array can relocate */
f6625916 198 g_hash_table_insert(struct_declaration->fields_by_name,
11796b96
MD
199 (gpointer) (unsigned long) field->name,
200 (gpointer) index);
201 /*
f6625916 202 * Alignment of structure is the max alignment of declarations contained
11796b96
MD
203 * therein.
204 */
f6625916
MD
205 struct_declaration->p.alignment = max(struct_declaration->p.alignment,
206 field_declaration->alignment);
11796b96
MD
207}
208
0f980a35 209/*
c8c98132 210 * bt_struct_declaration_lookup_field_index - returns field index
0f980a35
MD
211 *
212 * Returns the index of a field in a structure, or -1 if it does not
213 * exist.
214 */
c8c98132 215int bt_struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
e19c3d69 216 GQuark field_name)
11796b96 217{
0f980a35
MD
218 gpointer index;
219 gboolean found;
220
221 found = g_hash_table_lookup_extended(struct_declaration->fields_by_name,
222 (gconstpointer) (unsigned long) field_name,
223 NULL, &index);
224 if (!found)
225 return -1;
226 return (int) (unsigned long) index;
11796b96
MD
227}
228
c054553d
MD
229/*
230 * field returned only valid as long as the field structure is not appended to.
231 */
f6625916 232struct declaration_field *
c8c98132 233 bt_struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 234 int index)
c054553d 235{
0f980a35
MD
236 if (index < 0)
237 return NULL;
f6625916 238 return &g_array_index(struct_declaration->fields, struct declaration_field, index);
c054553d
MD
239}
240
11796b96
MD
241/*
242 * field returned only valid as long as the field structure is not appended to.
243 */
b1a2f580 244struct definition *
c8c98132 245bt_struct_definition_get_field_from_index(struct definition_struct *_struct,
0f980a35 246 int index)
11796b96 247{
0f980a35
MD
248 if (index < 0)
249 return NULL;
b1a2f580 250 return g_ptr_array_index(_struct->fields, index);
11796b96 251}
fd3382e8 252
c8c98132 253uint64_t bt_struct_declaration_len(struct declaration_struct *struct_declaration)
fd3382e8
MD
254{
255 return struct_declaration->fields->len;
256}
This page took 0.039239 seconds and 4 git commands to generate.