Work in progress generate io struct
[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
11796b96
MD
33void struct_copy(struct stream_pos *dest, const struct format *fdest,
34 struct stream_pos *src, const struct format *fsrc,
e1151715 35 struct definition *definition)
11796b96 36{
e1151715
MD
37 struct definition_struct *_struct =
38 container_of(definition, struct definition_struct, p);
f6625916 39 struct declaration_struct *struct_declaration = _struct->declaration;
c054553d 40 unsigned long i;
11796b96 41
f6625916
MD
42 fsrc->struct_begin(src, struct_declaration);
43 fdest->struct_begin(dest, struct_declaration);
11796b96 44
c054553d
MD
45 for (i = 0; i < _struct->fields->len; i++) {
46 struct field *field = &g_array_index(_struct->fields,
11796b96 47 struct field, i);
f6625916 48 struct declaration *field_declaration = field->definition->declaration;
11796b96 49
f6625916 50 field_declaration->copy(dest, fdest, src, fsrc, field->definition);
11796b96
MD
51
52 }
f6625916
MD
53 fsrc->struct_end(src, struct_declaration);
54 fdest->struct_end(dest, struct_declaration);
11796b96
MD
55}
56
c054553d 57static
f6625916 58void _struct_declaration_free(struct declaration *declaration)
11796b96 59{
f6625916
MD
60 struct declaration_struct *struct_declaration =
61 container_of(declaration, struct declaration_struct, p);
c054553d 62 unsigned long i;
4c8bfb7e 63
f6625916
MD
64 free_declaration_scope(struct_declaration->scope);
65 g_hash_table_destroy(struct_declaration->fields_by_name);
4c8bfb7e 66
f6625916
MD
67 for (i = 0; i < struct_declaration->fields->len; i++) {
68 struct declaration_field *declaration_field =
69 &g_array_index(struct_declaration->fields,
70 struct declaration_field, i);
71 declaration_unref(declaration_field->declaration);
4c8bfb7e 72 }
f6625916
MD
73 g_array_free(struct_declaration->fields, true);
74 g_free(struct_declaration);
11796b96
MD
75}
76
f6625916
MD
77struct declaration_struct *struct_declaration_new(const char *name,
78 struct declaration_scope *parent_scope)
11796b96 79{
f6625916
MD
80 struct declaration_struct *struct_declaration;
81 struct declaration *declaration;
11796b96 82
f6625916
MD
83 struct_declaration = g_new(struct declaration_struct, 1);
84 declaration = &struct_declaration->p;
85 struct_declaration->fields_by_name = g_hash_table_new(g_direct_hash,
e19c3d69 86 g_direct_equal);
f6625916
MD
87 struct_declaration->fields = g_array_sized_new(FALSE, TRUE,
88 sizeof(struct declaration_field),
e19c3d69 89 DEFAULT_NR_STRUCT_FIELDS);
f6625916
MD
90 struct_declaration->scope = new_declaration_scope(parent_scope);
91 declaration->id = CTF_TYPE_STRUCT;
92 declaration->name = g_quark_from_string(name);
93 declaration->alignment = 1;
94 declaration->copy = struct_copy;
95 declaration->declaration_free = _struct_declaration_free;
96 declaration->definition_new = _struct_definition_new;
97 declaration->definition_free = _struct_definition_free;
98 declaration->ref = 1;
99 return struct_declaration;
11796b96
MD
100}
101
c054553d 102static
e1151715 103struct definition *
f6625916 104 _struct_definition_new(struct declaration *declaration,
05c749e5
MD
105 struct definition_scope *parent_scope,
106 GQuark field_name, int index)
11796b96 107{
f6625916
MD
108 struct declaration_struct *struct_declaration =
109 container_of(declaration, struct declaration_struct, p);
e1151715 110 struct definition_struct *_struct;
ac88af75
MD
111 unsigned long i;
112 int ret;
c054553d 113
e1151715 114 _struct = g_new(struct definition_struct, 1);
f6625916
MD
115 declaration_ref(&struct_declaration->p);
116 _struct->p.declaration = declaration;
117 _struct->declaration = struct_declaration;
c054553d 118 _struct->p.ref = 1;
05c749e5
MD
119 _struct->p.index = index;
120 _struct->scope = new_definition_scope(parent_scope, field_name);
c054553d
MD
121 _struct->fields = g_array_sized_new(FALSE, TRUE,
122 sizeof(struct field),
123 DEFAULT_NR_STRUCT_FIELDS);
f6625916
MD
124 g_array_set_size(_struct->fields, struct_declaration->fields->len);
125 for (i = 0; i < struct_declaration->fields->len; i++) {
126 struct declaration_field *declaration_field =
127 &g_array_index(struct_declaration->fields,
128 struct declaration_field, i);
ac88af75
MD
129 struct field *field = &g_array_index(_struct->fields,
130 struct field, i);
131
f6625916 132 field->name = declaration_field->name;
e1151715 133 field->definition =
f6625916 134 declaration_field->declaration->definition_new(declaration_field->declaration,
05c749e5
MD
135 _struct->scope,
136 field->name, i);
e1151715
MD
137 ret = register_field_definition(field->name,
138 field->definition,
139 _struct->scope);
ac88af75
MD
140 assert(!ret);
141 }
c054553d
MD
142 return &_struct->p;
143}
144
145static
e1151715 146void _struct_definition_free(struct definition *definition)
c054553d 147{
e1151715
MD
148 struct definition_struct *_struct =
149 container_of(definition, struct definition_struct, p);
c054553d
MD
150 unsigned long i;
151
f6625916 152 assert(_struct->fields->len == _struct->declaration->fields->len);
c054553d
MD
153 for (i = 0; i < _struct->fields->len; i++) {
154 struct field *field = &g_array_index(_struct->fields,
155 struct field, i);
e1151715 156 definition_unref(field->definition);
c054553d 157 }
e1151715 158 free_definition_scope(_struct->scope);
f6625916 159 declaration_unref(_struct->p.declaration);
c054553d
MD
160 g_free(_struct);
161}
162
f6625916 163void struct_declaration_add_field(struct declaration_struct *struct_declaration,
e19c3d69 164 const char *field_name,
f6625916 165 struct declaration *field_declaration)
c054553d 166{
f6625916 167 struct declaration_field *field;
11796b96
MD
168 unsigned long index;
169
f6625916
MD
170 g_array_set_size(struct_declaration->fields, struct_declaration->fields->len + 1);
171 index = struct_declaration->fields->len - 1; /* last field (new) */
172 field = &g_array_index(struct_declaration->fields, struct declaration_field, index);
11796b96 173 field->name = g_quark_from_string(field_name);
f6625916
MD
174 declaration_ref(field_declaration);
175 field->declaration = field_declaration;
11796b96 176 /* Keep index in hash rather than pointer, because array can relocate */
f6625916 177 g_hash_table_insert(struct_declaration->fields_by_name,
11796b96
MD
178 (gpointer) (unsigned long) field->name,
179 (gpointer) index);
180 /*
f6625916 181 * Alignment of structure is the max alignment of declarations contained
11796b96
MD
182 * therein.
183 */
f6625916
MD
184 struct_declaration->p.alignment = max(struct_declaration->p.alignment,
185 field_declaration->alignment);
11796b96
MD
186}
187
188unsigned long
f6625916 189 struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
e19c3d69 190 GQuark field_name)
11796b96
MD
191{
192 unsigned long index;
193
f6625916 194 index = (unsigned long) g_hash_table_lookup(struct_declaration->fields_by_name,
4c8bfb7e 195 (gconstpointer) (unsigned long) field_name);
11796b96
MD
196 return index;
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,
e19c3d69 204 unsigned long index)
c054553d 205{
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 */
212struct field *
e1151715 213struct_definition_get_field_from_index(struct definition_struct *_struct,
e19c3d69 214 unsigned long index)
11796b96 215{
e19c3d69 216 return &g_array_index(_struct->fields, struct field, index);
11796b96 217}
This page took 0.033142 seconds and 4 git commands to generate.