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