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 declaration
*_struct_declaration_new(struct type
*type
,
28 struct declaration_scope
*parent_scope
);
30 void _struct_declaration_free(struct declaration
*declaration
);
32 void struct_copy(struct stream_pos
*dest
, const struct format
*fdest
,
33 struct stream_pos
*src
, const struct format
*fsrc
,
34 struct declaration
*declaration
)
36 struct declaration_struct
*_struct
=
37 container_of(declaration
, struct declaration_struct
, p
);
38 struct type_struct
*struct_type
= _struct
->type
;
41 fsrc
->struct_begin(src
, struct_type
);
42 fdest
->struct_begin(dest
, struct_type
);
44 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
45 struct field
*field
= &g_array_index(_struct
->fields
,
47 struct type
*field_type
= field
->type
->p
.type
;
49 field_type
->copy(dest
, fdest
, src
, fsrc
, &field
->type
->p
);
52 fsrc
->struct_end(src
, struct_type
);
53 fdest
->struct_end(dest
, struct_type
);
57 void _struct_type_free(struct type
*type
)
59 struct type_struct
*struct_type
=
60 container_of(type
, struct type_struct
, p
);
63 g_hash_table_destroy(struct_type
->fields_by_name
);
65 for (i
= 0; i
< struct_type
->fields
->len
; i
++) {
66 struct field
*type_field
=
67 &g_array_index(struct_type
->fields
,
68 struct type_field
, i
);
69 type_unref(field
->type
);
71 g_array_free(struct_type
->fields
, true);
75 struct type_struct
*struct_type_new(const char *name
)
77 struct type_struct
*struct_type
;
81 struct_type
= g_new(struct type_struct
, 1);
82 type
= &struct_type
->p
;
83 struct_type
->fields_by_name
= g_hash_table_new(g_direct_hash
,
85 struct_type
->fields
= g_array_sized_new(FALSE
, TRUE
,
86 sizeof(struct type_field
),
87 DEFAULT_NR_STRUCT_FIELDS
);
88 type
->name
= g_quark_from_string(name
);
90 type
->copy
= struct_copy
;
91 type
->type_free
= _struct_type_free
;
92 type
->declaration_new
= _struct_declaration_new
;
93 type
->declaration_free
= _struct_declaration_free
;
97 ret
= register_type(type
);
104 g_hash_table_destroy(struct_type
->fields_by_name
);
105 g_array_free(struct_type
->fields
, true);
112 _struct_declaration_new(struct type
*type
,
113 struct declaration_scope
*parent_scope
)
115 struct type_struct
*struct_type
=
116 container_of(type
, struct type_struct
, p
);
117 struct declaration_struct
*_struct
;
119 _struct
= g_new(struct declaration_struct
, 1);
120 type_ref(&struct_type
->p
);
121 _struct
->p
.type
= struct_type
;
123 _struct
->scope
= new_declaration_scope(parent_scope
);
124 _struct
->fields
= g_array_sized_new(FALSE
, TRUE
,
125 sizeof(struct field
),
126 DEFAULT_NR_STRUCT_FIELDS
);
131 void _struct_declaration_free(struct declaration
*declaration
)
133 struct declaration_struct
*_struct
=
134 container_of(declaration
, struct declaration_struct
, p
);
137 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
138 struct field
*field
= &g_array_index(_struct
->fields
,
140 declaration_unref(field
->declaration
);
142 free_declaration_scope(_struct
->scope
);
143 type_unref(_struct
->p
.type
);
147 void struct_type_add_field(struct type_struct
*struct_type
,
148 const char *field_name
,
149 struct type
*field_type
)
151 struct type_field
*field
;
154 g_array_set_size(struct_type
->fields
, struct_type
->fields
->len
+ 1);
155 index
= struct_type
->fields
->len
- 1; /* last field (new) */
156 field
= &g_array_index(struct_type
->fields
, struct type_field
, index
);
157 field
->name
= g_quark_from_string(field_name
);
158 type_ref(field_type
);
159 field
->type
= field_type
;
160 /* Keep index in hash rather than pointer, because array can relocate */
161 g_hash_table_insert(struct_type
->fields_by_name
,
162 (gpointer
) (unsigned long) field
->name
,
165 * Alignment of structure is the max alignment of types contained
168 struct_type
->p
.alignment
= max(struct_type
->p
.alignment
,
169 field_type
->alignment
);
173 struct_type_lookup_field_index(struct type_struct
*struct_type
,
178 index
= (unsigned long) g_hash_table_lookup(struct_type
->fields_by_name
,
179 (gconstpointer
) (unsigned long) field_name
);
184 * field returned only valid as long as the field structure is not appended to.
187 struct_type_get_field_from_index(struct type_struct
*struct_type
,
190 return &g_array_index(struct_type
->fields
, struct type_field
, index
);
194 * field returned only valid as long as the field structure is not appended to.
197 struct_declaration_get_field_from_index(struct declaration_struct
*_struct
,
200 return &g_array_index(_struct
->fields
, struct field
, index
);
This page took 0.045551 seconds and 4 git commands to generate.