Add out-of-bound checks
[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>
11796b96 21
be85c1c7
MD
22#ifndef max
23#define max(a, b) ((a) < (b) ? (b) : (a))
24#endif
25
c054553d 26static
f6625916 27struct definition *_struct_definition_new(struct declaration *declaration,
05c749e5
MD
28 struct definition_scope *parent_scope,
29 GQuark field_name, int index);
c054553d 30static
e1151715 31void _struct_definition_free(struct definition *definition);
c054553d 32
c5e74408 33int struct_rw(struct stream_pos *ppos, struct definition *definition)
11796b96 34{
d11e9c49 35 struct definition_struct *struct_definition =
e1151715 36 container_of(definition, struct definition_struct, p);
c054553d 37 unsigned long i;
c5e74408 38 int ret;
11796b96 39
d11e9c49
MD
40 for (i = 0; i < struct_definition->fields->len; i++) {
41 struct field *field = &g_array_index(struct_definition->fields,
11796b96 42 struct field, i);
c5e74408
MD
43 ret = generic_rw(ppos, field->definition);
44 if (ret)
45 return ret;
11796b96 46 }
c5e74408 47 return 0;
11796b96
MD
48}
49
c054553d 50static
f6625916 51void _struct_declaration_free(struct declaration *declaration)
11796b96 52{
f6625916
MD
53 struct declaration_struct *struct_declaration =
54 container_of(declaration, struct declaration_struct, p);
c054553d 55 unsigned long i;
4c8bfb7e 56
f6625916
MD
57 free_declaration_scope(struct_declaration->scope);
58 g_hash_table_destroy(struct_declaration->fields_by_name);
4c8bfb7e 59
f6625916
MD
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);
4c8bfb7e 65 }
f6625916
MD
66 g_array_free(struct_declaration->fields, true);
67 g_free(struct_declaration);
11796b96
MD
68}
69
ab4cf058
MD
70struct declaration_struct *
71 struct_declaration_new(struct declaration_scope *parent_scope)
11796b96 72{
f6625916
MD
73 struct declaration_struct *struct_declaration;
74 struct declaration *declaration;
11796b96 75
f6625916
MD
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,
e19c3d69 79 g_direct_equal);
f6625916
MD
80 struct_declaration->fields = g_array_sized_new(FALSE, TRUE,
81 sizeof(struct declaration_field),
e19c3d69 82 DEFAULT_NR_STRUCT_FIELDS);
f6625916
MD
83 struct_declaration->scope = new_declaration_scope(parent_scope);
84 declaration->id = CTF_TYPE_STRUCT;
f6625916 85 declaration->alignment = 1;
f6625916
MD
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;
11796b96
MD
91}
92
c054553d 93static
e1151715 94struct definition *
f6625916 95 _struct_definition_new(struct declaration *declaration,
05c749e5
MD
96 struct definition_scope *parent_scope,
97 GQuark field_name, int index)
11796b96 98{
f6625916
MD
99 struct declaration_struct *struct_declaration =
100 container_of(declaration, struct declaration_struct, p);
e1151715 101 struct definition_struct *_struct;
ac88af75
MD
102 unsigned long i;
103 int ret;
c054553d 104
e1151715 105 _struct = g_new(struct definition_struct, 1);
f6625916
MD
106 declaration_ref(&struct_declaration->p);
107 _struct->p.declaration = declaration;
108 _struct->declaration = struct_declaration;
c054553d 109 _struct->p.ref = 1;
05c749e5
MD
110 _struct->p.index = index;
111 _struct->scope = new_definition_scope(parent_scope, field_name);
c054553d
MD
112 _struct->fields = g_array_sized_new(FALSE, TRUE,
113 sizeof(struct field),
114 DEFAULT_NR_STRUCT_FIELDS);
f6625916
MD
115 g_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);
ac88af75
MD
120 struct field *field = &g_array_index(_struct->fields,
121 struct field, i);
122
f6625916 123 field->name = declaration_field->name;
e1151715 124 field->definition =
f6625916 125 declaration_field->declaration->definition_new(declaration_field->declaration,
05c749e5
MD
126 _struct->scope,
127 field->name, i);
e1151715
MD
128 ret = register_field_definition(field->name,
129 field->definition,
130 _struct->scope);
ac88af75
MD
131 assert(!ret);
132 }
c054553d
MD
133 return &_struct->p;
134}
135
136static
e1151715 137void _struct_definition_free(struct definition *definition)
c054553d 138{
e1151715
MD
139 struct definition_struct *_struct =
140 container_of(definition, struct definition_struct, p);
c054553d
MD
141 unsigned long i;
142
f6625916 143 assert(_struct->fields->len == _struct->declaration->fields->len);
c054553d
MD
144 for (i = 0; i < _struct->fields->len; i++) {
145 struct field *field = &g_array_index(_struct->fields,
146 struct field, i);
e1151715 147 definition_unref(field->definition);
c054553d 148 }
e1151715 149 free_definition_scope(_struct->scope);
f6625916 150 declaration_unref(_struct->p.declaration);
c054553d
MD
151 g_free(_struct);
152}
153
f6625916 154void struct_declaration_add_field(struct declaration_struct *struct_declaration,
e19c3d69 155 const char *field_name,
f6625916 156 struct declaration *field_declaration)
c054553d 157{
f6625916 158 struct declaration_field *field;
11796b96
MD
159 unsigned long index;
160
f6625916
MD
161 g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1);
162 index = struct_declaration->fields->len - 1; /* last field (new) */
163 field = &g_array_index(struct_declaration->fields, struct declaration_field, index);
11796b96 164 field->name = g_quark_from_string(field_name);
f6625916
MD
165 declaration_ref(field_declaration);
166 field->declaration = field_declaration;
11796b96 167 /* Keep index in hash rather than pointer, because array can relocate */
f6625916 168 g_hash_table_insert(struct_declaration->fields_by_name,
11796b96
MD
169 (gpointer) (unsigned long) field->name,
170 (gpointer) index);
171 /*
f6625916 172 * Alignment of structure is the max alignment of declarations contained
11796b96
MD
173 * therein.
174 */
f6625916
MD
175 struct_declaration->p.alignment = max(struct_declaration->p.alignment,
176 field_declaration->alignment);
11796b96
MD
177}
178
0f980a35
MD
179/*
180 * struct_declaration_lookup_field_index - returns field index
181 *
182 * Returns the index of a field in a structure, or -1 if it does not
183 * exist.
184 */
185int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
e19c3d69 186 GQuark field_name)
11796b96 187{
0f980a35
MD
188 gpointer index;
189 gboolean found;
190
191 found = g_hash_table_lookup_extended(struct_declaration->fields_by_name,
192 (gconstpointer) (unsigned long) field_name,
193 NULL, &index);
194 if (!found)
195 return -1;
196 return (int) (unsigned long) index;
11796b96
MD
197}
198
c054553d
MD
199/*
200 * field returned only valid as long as the field structure is not appended to.
201 */
f6625916
MD
202struct declaration_field *
203 struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 204 int index)
c054553d 205{
0f980a35
MD
206 if (index < 0)
207 return NULL;
f6625916 208 return &g_array_index(struct_declaration->fields, struct declaration_field, index);
c054553d
MD
209}
210
11796b96
MD
211/*
212 * field returned only valid as long as the field structure is not appended to.
213 */
214struct field *
e1151715 215struct_definition_get_field_from_index(struct definition_struct *_struct,
0f980a35 216 int index)
11796b96 217{
0f980a35
MD
218 if (index < 0)
219 return NULL;
e19c3d69 220 return &g_array_index(_struct->fields, struct field, index);
11796b96 221}
This page took 0.033771 seconds and 4 git commands to generate.