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