4 * BabelTrace - Structure Type Converter
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
23 #define max(a, b) ((a) < (b) ? (b) : (a))
27 struct type
*_struct_type_new(struct type_class
*type_class
,
28 struct declaration_scope
*parent_scope
);
30 void _struct_type_free(struct type
*type
);
32 void struct_copy(struct stream_pos
*dest
, const struct format
*fdest
,
33 struct stream_pos
*src
, const struct format
*fsrc
,
36 struct type_struct
*_struct
= container_of(type
, struct type_struct
, p
);
37 struct type_class_struct
*struct_class
= _struct
->_class
;
40 fsrc
->struct_begin(src
, struct_class
);
41 fdest
->struct_begin(dest
, struct_class
);
43 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
44 struct field
*field
= &g_array_index(_struct
->fields
,
46 struct type_class
*field_class
= field
->type
->p
._class
;
48 field_class
->copy(dest
, fdest
, src
, fsrc
, &field
->type
->p
);
51 fsrc
->struct_end(src
, struct_class
);
52 fdest
->struct_end(dest
, struct_class
);
56 void _struct_type_class_free(struct type_class
*type_class
)
58 struct type_class_struct
*struct_class
=
59 container_of(type_class
, struct type_class_struct
, p
);
62 g_hash_table_destroy(struct_class
->fields_by_name
);
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
);
70 g_array_free(struct_class
->fields
, true);
74 struct type_class_struct
*
75 struct_type_class_new(const char *name
)
77 struct type_class_struct
*struct_class
;
78 struct type_class
*type_class
;
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
,
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
;
96 if (type_class
->name
) {
97 ret
= register_type(type_class
);
104 g_hash_table_destroy(struct_class
->fields_by_name
);
105 g_array_free(struct_class
->fields
, true);
106 g_free(struct_class
);
111 struct type_struct
*_struct_type_new(struct type_class
*type_class
,
112 struct declaration_scope
*parent_scope
)
114 struct type_class_struct
*_struct_class
=
115 container_of(type_class
, struct type_class_struct
, p
);
116 struct type_struct
*_struct
;
118 _struct
= g_new(struct type_struct
, 1);
119 type_class_ref(&_struct_class
->p
);
120 _struct
->p
._class
= _struct_class
;
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
);
130 void _struct_type_free(struct type
*type
)
132 struct type_struct
*_struct
= container_of(type
, struct type_struct
, p
);
135 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
136 struct field
*field
= &g_array_index(_struct
->fields
,
138 type_unref(field
->type
);
140 free_declaration_scope(_struct
->scope
);
141 type_class_unref(_struct
->p
._class
);
145 void struct_type_class_add_field(struct type_class_struct
*struct_class
,
146 const char *field_name
,
147 struct type_class
*type_class
)
149 struct type_class_field
*field
;
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
,
163 * Alignment of structure is the max alignment of types contained
166 struct_class
->p
.alignment
= max(struct_class
->p
.alignment
,
167 type_class
->alignment
);
171 struct_type_class_lookup_field_index(struct type_class_struct
*struct_class
,
176 index
= (unsigned long) g_hash_table_lookup(struct_class
->fields_by_name
,
177 (gconstpointer
) (unsigned long) field_name
);
182 * field returned only valid as long as the field structure is not appended to.
184 struct type_class_field
*
185 struct_type_class_get_field_from_index(struct type_class_struct
*struct_class
,
188 return &g_array_index(struct_class
->fields
, struct type_class_field
, index
);
192 * field returned only valid as long as the field structure is not appended to.
195 struct_type_get_field_from_index(struct type_struct
*struct_type
,
198 return &g_array_index(struct_type
->fields
, struct field
, index
);
This page took 0.036857 seconds and 4 git commands to generate.