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