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