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