Create copy of integer declaration before applying base-16 for pointers
[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 40 for (i = 0; i < struct_definition->fields->len; i++) {
b1a2f580
MD
41 struct definition *field =
42 g_ptr_array_index(struct_definition->fields, i);
43 ret = generic_rw(ppos, field);
c5e74408
MD
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 70struct declaration_struct *
b7e35bad
MD
71 struct_declaration_new(struct declaration_scope *parent_scope,
72 uint64_t min_align)
11796b96 73{
f6625916
MD
74 struct declaration_struct *struct_declaration;
75 struct declaration *declaration;
11796b96 76
f6625916
MD
77 struct_declaration = g_new(struct declaration_struct, 1);
78 declaration = &struct_declaration->p;
79 struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash,
e19c3d69 80 g_direct_equal);
f6625916
MD
81 struct_declaration->fields = g_array_sized_new(FALSE, TRUE,
82 sizeof(struct declaration_field),
e19c3d69 83 DEFAULT_NR_STRUCT_FIELDS);
f6625916
MD
84 struct_declaration->scope = new_declaration_scope(parent_scope);
85 declaration->id = CTF_TYPE_STRUCT;
b7e35bad 86 declaration->alignment = max(1, min_align);
f6625916
MD
87 declaration->declaration_free = _struct_declaration_free;
88 declaration->definition_new = _struct_definition_new;
89 declaration->definition_free = _struct_definition_free;
90 declaration->ref = 1;
91 return struct_declaration;
11796b96
MD
92}
93
c054553d 94static
e1151715 95struct definition *
f6625916 96 _struct_definition_new(struct declaration *declaration,
05c749e5
MD
97 struct definition_scope *parent_scope,
98 GQuark field_name, int index)
11796b96 99{
f6625916
MD
100 struct declaration_struct *struct_declaration =
101 container_of(declaration, struct declaration_struct, p);
e1151715 102 struct definition_struct *_struct;
ac88af75
MD
103 unsigned long i;
104 int ret;
c054553d 105
e1151715 106 _struct = g_new(struct definition_struct, 1);
f6625916
MD
107 declaration_ref(&struct_declaration->p);
108 _struct->p.declaration = declaration;
109 _struct->declaration = struct_declaration;
c054553d 110 _struct->p.ref = 1;
05c749e5 111 _struct->p.index = index;
b1a2f580 112 _struct->p.name = field_name;
31262354 113 _struct->p.path = new_definition_path(parent_scope, field_name);
05c749e5 114 _struct->scope = new_definition_scope(parent_scope, field_name);
b1a2f580
MD
115 _struct->fields = g_ptr_array_sized_new(DEFAULT_NR_STRUCT_FIELDS);
116 g_ptr_array_set_size(_struct->fields, struct_declaration->fields->len);
f6625916
MD
117 for (i = 0; i < struct_declaration->fields->len; i++) {
118 struct declaration_field *declaration_field =
119 &g_array_index(struct_declaration->fields,
120 struct declaration_field, i);
b1a2f580
MD
121 struct definition **field =
122 (struct definition **) &g_ptr_array_index(_struct->fields, i);
ac88af75 123
b1a2f580 124 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
05c749e5 125 _struct->scope,
b1a2f580
MD
126 declaration_field->name, i);
127 ret = register_field_definition(declaration_field->name,
128 *field,
e1151715 129 _struct->scope);
ac88af75
MD
130 assert(!ret);
131 }
c054553d
MD
132 return &_struct->p;
133}
134
135static
e1151715 136void _struct_definition_free(struct definition *definition)
c054553d 137{
e1151715
MD
138 struct definition_struct *_struct =
139 container_of(definition, struct definition_struct, p);
c054553d
MD
140 unsigned long i;
141
f6625916 142 assert(_struct->fields->len == _struct->declaration->fields->len);
c054553d 143 for (i = 0; i < _struct->fields->len; i++) {
b1a2f580
MD
144 struct definition *field = g_ptr_array_index(_struct->fields, i);
145 definition_unref(field);
c054553d 146 }
e1151715 147 free_definition_scope(_struct->scope);
f6625916 148 declaration_unref(_struct->p.declaration);
c054553d
MD
149 g_free(_struct);
150}
151
f6625916 152void struct_declaration_add_field(struct declaration_struct *struct_declaration,
e19c3d69 153 const char *field_name,
f6625916 154 struct declaration *field_declaration)
c054553d 155{
f6625916 156 struct declaration_field *field;
11796b96
MD
157 unsigned long index;
158
f6625916
MD
159 g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1);
160 index = struct_declaration->fields->len - 1; /* last field (new) */
161 field = &g_array_index(struct_declaration->fields, struct declaration_field, index);
11796b96 162 field->name = g_quark_from_string(field_name);
f6625916
MD
163 declaration_ref(field_declaration);
164 field->declaration = field_declaration;
11796b96 165 /* Keep index in hash rather than pointer, because array can relocate */
f6625916 166 g_hash_table_insert(struct_declaration->fields_by_name,
11796b96
MD
167 (gpointer) (unsigned long) field->name,
168 (gpointer) index);
169 /*
f6625916 170 * Alignment of structure is the max alignment of declarations contained
11796b96
MD
171 * therein.
172 */
f6625916
MD
173 struct_declaration->p.alignment = max(struct_declaration->p.alignment,
174 field_declaration->alignment);
11796b96
MD
175}
176
0f980a35
MD
177/*
178 * struct_declaration_lookup_field_index - returns field index
179 *
180 * Returns the index of a field in a structure, or -1 if it does not
181 * exist.
182 */
183int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
e19c3d69 184 GQuark field_name)
11796b96 185{
0f980a35
MD
186 gpointer index;
187 gboolean found;
188
189 found = g_hash_table_lookup_extended(struct_declaration->fields_by_name,
190 (gconstpointer) (unsigned long) field_name,
191 NULL, &index);
192 if (!found)
193 return -1;
194 return (int) (unsigned long) index;
11796b96
MD
195}
196
c054553d
MD
197/*
198 * field returned only valid as long as the field structure is not appended to.
199 */
f6625916
MD
200struct declaration_field *
201 struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 202 int index)
c054553d 203{
0f980a35
MD
204 if (index < 0)
205 return NULL;
f6625916 206 return &g_array_index(struct_declaration->fields, struct declaration_field, index);
c054553d
MD
207}
208
11796b96
MD
209/*
210 * field returned only valid as long as the field structure is not appended to.
211 */
b1a2f580 212struct definition *
e1151715 213struct_definition_get_field_from_index(struct definition_struct *_struct,
0f980a35 214 int index)
11796b96 215{
0f980a35
MD
216 if (index < 0)
217 return NULL;
b1a2f580 218 return g_ptr_array_index(_struct->fields, index);
11796b96 219}
fd3382e8
MD
220
221uint64_t struct_declaration_len(struct declaration_struct *struct_declaration)
222{
223 return struct_declaration->fields->len;
224}
This page took 0.033791 seconds and 4 git commands to generate.