cleanup
[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
e19c3d69
MD
27struct declaration *_struct_declaration_new(struct type *type,
28 struct declaration_scope *parent_scope);
c054553d 29static
e19c3d69 30void _struct_declaration_free(struct declaration *declaration);
c054553d 31
11796b96
MD
32void struct_copy(struct stream_pos *dest, const struct format *fdest,
33 struct stream_pos *src, const struct format *fsrc,
e19c3d69 34 struct declaration *declaration)
11796b96 35{
e19c3d69
MD
36 struct declaration_struct *_struct =
37 container_of(declaration, struct declaration_struct, p);
38 struct type_struct *struct_type = _struct->type;
c054553d 39 unsigned long i;
11796b96 40
e19c3d69
MD
41 fsrc->struct_begin(src, struct_type);
42 fdest->struct_begin(dest, struct_type);
11796b96 43
c054553d
MD
44 for (i = 0; i < _struct->fields->len; i++) {
45 struct field *field = &g_array_index(_struct->fields,
11796b96 46 struct field, i);
6ee5115e 47 struct type *field_type = field->declaration->type;
11796b96 48
6ee5115e 49 field_type->copy(dest, fdest, src, fsrc, field->declaration);
11796b96
MD
50
51 }
e19c3d69
MD
52 fsrc->struct_end(src, struct_type);
53 fdest->struct_end(dest, struct_type);
11796b96
MD
54}
55
c054553d 56static
e19c3d69 57void _struct_type_free(struct type *type)
11796b96 58{
e19c3d69
MD
59 struct type_struct *struct_type =
60 container_of(type, struct type_struct, p);
c054553d 61 unsigned long i;
4c8bfb7e 62
e19c3d69 63 g_hash_table_destroy(struct_type->fields_by_name);
4c8bfb7e 64
e19c3d69 65 for (i = 0; i < struct_type->fields->len; i++) {
6ee5115e 66 struct type_field *type_field =
e19c3d69
MD
67 &g_array_index(struct_type->fields,
68 struct type_field, i);
6ee5115e 69 type_unref(type_field->type);
4c8bfb7e 70 }
e19c3d69
MD
71 g_array_free(struct_type->fields, true);
72 g_free(struct_type);
11796b96
MD
73}
74
e19c3d69 75struct type_struct *struct_type_new(const char *name)
11796b96 76{
e19c3d69
MD
77 struct type_struct *struct_type;
78 struct type *type;
11796b96 79
e19c3d69
MD
80 struct_type = g_new(struct type_struct, 1);
81 type = &struct_type->p;
82 struct_type->fields_by_name = g_hash_table_new(g_direct_hash,
83 g_direct_equal);
84 struct_type->fields = g_array_sized_new(FALSE, TRUE,
85 sizeof(struct type_field),
86 DEFAULT_NR_STRUCT_FIELDS);
87 type->name = g_quark_from_string(name);
88 type->alignment = 1;
89 type->copy = struct_copy;
90 type->type_free = _struct_type_free;
91 type->declaration_new = _struct_declaration_new;
92 type->declaration_free = _struct_declaration_free;
93 type->ref = 1;
e19c3d69 94 return struct_type;
11796b96
MD
95}
96
c054553d 97static
e19c3d69
MD
98struct declaration *
99 _struct_declaration_new(struct type *type,
100 struct declaration_scope *parent_scope)
11796b96 101{
e19c3d69
MD
102 struct type_struct *struct_type =
103 container_of(type, struct type_struct, p);
104 struct declaration_struct *_struct;
ac88af75
MD
105 unsigned long i;
106 int ret;
c054553d 107
e19c3d69
MD
108 _struct = g_new(struct declaration_struct, 1);
109 type_ref(&struct_type->p);
6ee5115e
MD
110 _struct->p.type = type;
111 _struct->type = struct_type;
c054553d
MD
112 _struct->p.ref = 1;
113 _struct->scope = new_declaration_scope(parent_scope);
114 _struct->fields = g_array_sized_new(FALSE, TRUE,
115 sizeof(struct field),
116 DEFAULT_NR_STRUCT_FIELDS);
ac88af75
MD
117 g_array_set_size(_struct->fields, struct_type->fields->len);
118 for (i = 0; i < struct_type->fields->len; i++) {
119 struct type_field *type_field =
120 &g_array_index(struct_type->fields,
121 struct type_field, i);
122 struct field *field = &g_array_index(_struct->fields,
123 struct field, i);
124
125 field->name = type_field->name;
126 field->declaration =
127 type_field->type->declaration_new(type_field->type,
128 _struct->scope);
129 ret = register_declaration(field->name,
130 field->declaration, _struct->scope);
131 assert(!ret);
132 }
c054553d
MD
133 return &_struct->p;
134}
135
136static
e19c3d69 137void _struct_declaration_free(struct declaration *declaration)
c054553d 138{
e19c3d69
MD
139 struct declaration_struct *_struct =
140 container_of(declaration, struct declaration_struct, p);
c054553d
MD
141 unsigned long i;
142
ac88af75 143 assert(_struct->fields->len == _struct->type->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);
e19c3d69 147 declaration_unref(field->declaration);
c054553d
MD
148 }
149 free_declaration_scope(_struct->scope);
e19c3d69 150 type_unref(_struct->p.type);
c054553d
MD
151 g_free(_struct);
152}
153
e19c3d69
MD
154void struct_type_add_field(struct type_struct *struct_type,
155 const char *field_name,
156 struct type *field_type)
c054553d 157{
e19c3d69 158 struct type_field *field;
11796b96
MD
159 unsigned long index;
160
e19c3d69
MD
161 g_array_set_size(struct_type->fields, struct_type->fields->len + 1);
162 index = struct_type->fields->len - 1; /* last field (new) */
163 field = &g_array_index(struct_type->fields, struct type_field, index);
11796b96 164 field->name = g_quark_from_string(field_name);
e19c3d69
MD
165 type_ref(field_type);
166 field->type = field_type;
11796b96 167 /* Keep index in hash rather than pointer, because array can relocate */
e19c3d69 168 g_hash_table_insert(struct_type->fields_by_name,
11796b96
MD
169 (gpointer) (unsigned long) field->name,
170 (gpointer) index);
171 /*
172 * Alignment of structure is the max alignment of types contained
173 * therein.
174 */
e19c3d69
MD
175 struct_type->p.alignment = max(struct_type->p.alignment,
176 field_type->alignment);
11796b96
MD
177}
178
179unsigned long
e19c3d69
MD
180 struct_type_lookup_field_index(struct type_struct *struct_type,
181 GQuark field_name)
11796b96
MD
182{
183 unsigned long index;
184
e19c3d69 185 index = (unsigned long) g_hash_table_lookup(struct_type->fields_by_name,
4c8bfb7e 186 (gconstpointer) (unsigned long) field_name);
11796b96
MD
187 return index;
188}
189
c054553d
MD
190/*
191 * field returned only valid as long as the field structure is not appended to.
192 */
e19c3d69
MD
193struct type_field *
194 struct_type_get_field_from_index(struct type_struct *struct_type,
195 unsigned long index)
c054553d 196{
e19c3d69 197 return &g_array_index(struct_type->fields, struct type_field, index);
c054553d
MD
198}
199
11796b96
MD
200/*
201 * field returned only valid as long as the field structure is not appended to.
202 */
203struct field *
e19c3d69
MD
204struct_declaration_get_field_from_index(struct declaration_struct *_struct,
205 unsigned long index)
11796b96 206{
e19c3d69 207 return &g_array_index(_struct->fields, struct field, index);
11796b96 208}
This page took 0.032329 seconds and 4 git commands to generate.