Fix: return an error if no trace can be opened
[babeltrace.git] / types / struct.c
1 /*
2 * struct.c
3 *
4 * BabelTrace - Structure Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
23 #include <babeltrace/types.h>
24 #include <errno.h>
25
26 #ifndef max
27 #define max(a, b) ((a) < (b) ? (b) : (a))
28 #endif
29
30 static
31 struct definition *_struct_definition_new(struct declaration *declaration,
32 struct definition_scope *parent_scope,
33 GQuark field_name, int index,
34 const char *root_name);
35 static
36 void _struct_definition_free(struct definition *definition);
37
38 int struct_rw(struct stream_pos *ppos, struct definition *definition)
39 {
40 struct definition_struct *struct_definition =
41 container_of(definition, struct definition_struct, p);
42 unsigned long i;
43 int ret;
44
45 for (i = 0; i < struct_definition->fields->len; i++) {
46 struct definition *field =
47 g_ptr_array_index(struct_definition->fields, i);
48 ret = generic_rw(ppos, field);
49 if (ret)
50 return ret;
51 }
52 return 0;
53 }
54
55 static
56 void _struct_declaration_free(struct declaration *declaration)
57 {
58 struct declaration_struct *struct_declaration =
59 container_of(declaration, struct declaration_struct, p);
60 unsigned long i;
61
62 free_declaration_scope(struct_declaration->scope);
63 g_hash_table_destroy(struct_declaration->fields_by_name);
64
65 for (i = 0; i < struct_declaration->fields->len; i++) {
66 struct declaration_field *declaration_field =
67 &g_array_index(struct_declaration->fields,
68 struct declaration_field, i);
69 declaration_unref(declaration_field->declaration);
70 }
71 g_array_free(struct_declaration->fields, true);
72 g_free(struct_declaration);
73 }
74
75 struct declaration_struct *
76 struct_declaration_new(struct declaration_scope *parent_scope,
77 uint64_t min_align)
78 {
79 struct declaration_struct *struct_declaration;
80 struct declaration *declaration;
81
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,
85 g_direct_equal);
86 struct_declaration->fields = g_array_sized_new(FALSE, TRUE,
87 sizeof(struct declaration_field),
88 DEFAULT_NR_STRUCT_FIELDS);
89 struct_declaration->scope = new_declaration_scope(parent_scope);
90 declaration->id = CTF_TYPE_STRUCT;
91 declaration->alignment = max(1, min_align);
92 declaration->declaration_free = _struct_declaration_free;
93 declaration->definition_new = _struct_definition_new;
94 declaration->definition_free = _struct_definition_free;
95 declaration->ref = 1;
96 return struct_declaration;
97 }
98
99 static
100 struct definition *
101 _struct_definition_new(struct declaration *declaration,
102 struct definition_scope *parent_scope,
103 GQuark field_name, int index,
104 const char *root_name)
105 {
106 struct declaration_struct *struct_declaration =
107 container_of(declaration, struct declaration_struct, p);
108 struct definition_struct *_struct;
109 int i;
110 int ret;
111
112 _struct = g_new(struct definition_struct, 1);
113 declaration_ref(&struct_declaration->p);
114 _struct->p.declaration = declaration;
115 _struct->declaration = struct_declaration;
116 _struct->p.ref = 1;
117 /*
118 * Use INT_MAX order to ensure that all fields of the parent
119 * scope are seen as being prior to this scope.
120 */
121 _struct->p.index = root_name ? INT_MAX : index;
122 _struct->p.name = field_name;
123 _struct->p.path = new_definition_path(parent_scope, field_name, root_name);
124 _struct->p.scope = new_definition_scope(parent_scope, field_name, root_name);
125
126 ret = register_field_definition(field_name, &_struct->p,
127 parent_scope);
128 assert(!ret || ret == -EPERM);
129
130 _struct->fields = g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS);
131 g_ptr_array_set_size(_struct->fields, struct_declaration->fields->len);
132 for (i = 0; i < struct_declaration->fields->len; i++) {
133 struct declaration_field *declaration_field =
134 &g_array_index(struct_declaration->fields,
135 struct declaration_field, i);
136 struct definition **field =
137 (struct definition **) &g_ptr_array_index(_struct->fields, i);
138
139 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
140 _struct->p.scope,
141 declaration_field->name, i, NULL);
142 if (!*field)
143 goto error;
144 }
145 return &_struct->p;
146
147 error:
148 for (i--; i >= 0; i--) {
149 struct definition *field = g_ptr_array_index(_struct->fields, i);
150 definition_unref(field);
151 }
152 free_definition_scope(_struct->p.scope);
153 declaration_unref(&struct_declaration->p);
154 g_free(_struct);
155 return NULL;
156 }
157
158 static
159 void _struct_definition_free(struct definition *definition)
160 {
161 struct definition_struct *_struct =
162 container_of(definition, struct definition_struct, p);
163 unsigned long i;
164
165 assert(_struct->fields->len == _struct->declaration->fields->len);
166 for (i = 0; i < _struct->fields->len; i++) {
167 struct definition *field = g_ptr_array_index(_struct->fields, i);
168 definition_unref(field);
169 }
170 free_definition_scope(_struct->p.scope);
171 declaration_unref(_struct->p.declaration);
172 g_free(_struct);
173 }
174
175 void struct_declaration_add_field(struct declaration_struct *struct_declaration,
176 const char *field_name,
177 struct declaration *field_declaration)
178 {
179 struct declaration_field *field;
180 unsigned long index;
181
182 g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1);
183 index = struct_declaration->fields->len - 1; /* last field (new) */
184 field = &g_array_index(struct_declaration->fields, struct declaration_field, index);
185 field->name = g_quark_from_string(field_name);
186 declaration_ref(field_declaration);
187 field->declaration = field_declaration;
188 /* Keep index in hash rather than pointer, because array can relocate */
189 g_hash_table_insert(struct_declaration->fields_by_name,
190 (gpointer) (unsigned long) field->name,
191 (gpointer) index);
192 /*
193 * Alignment of structure is the max alignment of declarations contained
194 * therein.
195 */
196 struct_declaration->p.alignment = max(struct_declaration->p.alignment,
197 field_declaration->alignment);
198 }
199
200 /*
201 * struct_declaration_lookup_field_index - returns field index
202 *
203 * Returns the index of a field in a structure, or -1 if it does not
204 * exist.
205 */
206 int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
207 GQuark field_name)
208 {
209 gpointer index;
210 gboolean found;
211
212 found = g_hash_table_lookup_extended(struct_declaration->fields_by_name,
213 (gconstpointer) (unsigned long) field_name,
214 NULL, &index);
215 if (!found)
216 return -1;
217 return (int) (unsigned long) index;
218 }
219
220 /*
221 * field returned only valid as long as the field structure is not appended to.
222 */
223 struct declaration_field *
224 struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
225 int index)
226 {
227 if (index < 0)
228 return NULL;
229 return &g_array_index(struct_declaration->fields, struct declaration_field, index);
230 }
231
232 /*
233 * field returned only valid as long as the field structure is not appended to.
234 */
235 struct definition *
236 struct_definition_get_field_from_index(struct definition_struct *_struct,
237 int index)
238 {
239 if (index < 0)
240 return NULL;
241 return g_ptr_array_index(_struct->fields, index);
242 }
243
244 uint64_t struct_declaration_len(struct declaration_struct *struct_declaration)
245 {
246 return struct_declaration->fields->len;
247 }
This page took 0.035405 seconds and 4 git commands to generate.