Add type class/type structure management
[babeltrace.git] / types / struct.c
1 /*
2 * struct.c
3 *
4 * BabelTrace - Structure Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21
22 #ifndef max
23 #define max(a, b) ((a) < (b) ? (b) : (a))
24 #endif
25
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
32 void struct_copy(struct stream_pos *dest, const struct format *fdest,
33 struct stream_pos *src, const struct format *fsrc,
34 struct type *type)
35 {
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;
39
40 fsrc->struct_begin(src, struct_class);
41 fdest->struct_begin(dest, struct_class);
42
43 for (i = 0; i < _struct->fields->len; i++) {
44 struct field *field = &g_array_index(_struct->fields,
45 struct field, i);
46 struct type_class *field_class = field->type->p._class;
47
48 field_class->copy(dest, fdest, src, fsrc, &field->type->p);
49
50 }
51 fsrc->struct_end(src, struct_class);
52 fdest->struct_end(dest, struct_class);
53 }
54
55 static
56 void _struct_type_class_free(struct type_class *type_class)
57 {
58 struct type_class_struct *struct_class =
59 container_of(type_class, struct type_class_struct, p);
60 unsigned long i;
61
62 g_hash_table_destroy(struct_class->fields_by_name);
63
64 for (i = 0; i < struct_class->fields->len; i++) {
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);
69 }
70 g_array_free(struct_class->fields, true);
71 g_free(struct_class);
72 }
73
74 struct type_class_struct *
75 struct_type_class_new(const char *name)
76 {
77 struct type_class_struct *struct_class;
78 struct type_class *type_class;
79 int ret;
80
81 struct_class = g_new(struct type_class_struct, 1);
82 type_class = &struct_class->p;
83 struct_class->fields_by_name = g_hash_table_new(g_direct_hash,
84 g_direct_equal);
85 struct_class->fields = g_array_sized_new(FALSE, TRUE,
86 sizeof(struct type_class_field),
87 DEFAULT_NR_STRUCT_FIELDS);
88 type_class->name = g_quark_from_string(name);
89 type_class->alignment = 1;
90 type_class->copy = struct_copy;
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;
94 type_class->ref = 1;
95
96 if (type_class->name) {
97 ret = register_type(type_class);
98 if (ret)
99 goto error_register;
100 }
101 return struct_class;
102
103 error_register:
104 g_hash_table_destroy(struct_class->fields_by_name);
105 g_array_free(struct_class->fields, true);
106 g_free(struct_class);
107 return NULL;
108 }
109
110 static
111 struct type_struct *_struct_type_new(struct type_class *type_class,
112 struct declaration_scope *parent_scope)
113 {
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;
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) */
154 field = &g_array_index(struct_class->fields, struct type_class_field, index);
155 field->name = g_quark_from_string(field_name);
156 type_ref(type_class);
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
171 struct_type_class_lookup_field_index(struct type_class_struct *struct_class,
172 GQuark field_name)
173 {
174 unsigned long index;
175
176 index = (unsigned long) g_hash_table_lookup(struct_class->fields_by_name,
177 (gconstpointer) (unsigned long) field_name);
178 return index;
179 }
180
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
191 /*
192 * field returned only valid as long as the field structure is not appended to.
193 */
194 struct field *
195 struct_type_get_field_from_index(struct type_struct *struct_type,
196 unsigned long index)
197 {
198 return &g_array_index(struct_type->fields, struct field, index);
199 }
This page took 0.032977 seconds and 4 git commands to generate.