062874784f05165439f9109a387f201a0bf8860a
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 definition
*_struct_definition_new(struct type
*type
,
28 struct definition_scope
*parent_scope
);
30 void _struct_definition_free(struct definition
*definition
);
32 void struct_copy(struct stream_pos
*dest
, const struct format
*fdest
,
33 struct stream_pos
*src
, const struct format
*fsrc
,
34 struct definition
*definition
)
36 struct definition_struct
*_struct
=
37 container_of(definition
, struct definition_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
->definition
->type
;
49 field_type
->copy(dest
, fdest
, src
, fsrc
, field
->definition
);
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 free_type_scope(struct_type
->scope
);
64 g_hash_table_destroy(struct_type
->fields_by_name
);
66 for (i
= 0; i
< struct_type
->fields
->len
; i
++) {
67 struct type_field
*type_field
=
68 &g_array_index(struct_type
->fields
,
69 struct type_field
, i
);
70 type_unref(type_field
->type
);
72 g_array_free(struct_type
->fields
, true);
76 struct type_struct
*struct_type_new(const char *name
,
77 struct type_scope
*parent_scope
)
79 struct type_struct
*struct_type
;
82 struct_type
= g_new(struct type_struct
, 1);
83 type
= &struct_type
->p
;
84 struct_type
->fields_by_name
= g_hash_table_new(g_direct_hash
,
86 struct_type
->fields
= g_array_sized_new(FALSE
, TRUE
,
87 sizeof(struct type_field
),
88 DEFAULT_NR_STRUCT_FIELDS
);
89 struct_type
->scope
= new_type_scope(parent_scope
);
90 type
->id
= CTF_TYPE_STRUCT
;
91 type
->name
= g_quark_from_string(name
);
93 type
->copy
= struct_copy
;
94 type
->type_free
= _struct_type_free
;
95 type
->definition_new
= _struct_definition_new
;
96 type
->definition_free
= _struct_definition_free
;
103 _struct_definition_new(struct type
*type
,
104 struct definition_scope
*parent_scope
)
106 struct type_struct
*struct_type
=
107 container_of(type
, struct type_struct
, p
);
108 struct definition_struct
*_struct
;
112 _struct
= g_new(struct definition_struct
, 1);
113 type_ref(&struct_type
->p
);
114 _struct
->p
.type
= type
;
115 _struct
->type
= struct_type
;
117 _struct
->scope
= new_definition_scope(parent_scope
);
118 _struct
->fields
= g_array_sized_new(FALSE
, TRUE
,
119 sizeof(struct field
),
120 DEFAULT_NR_STRUCT_FIELDS
);
121 g_array_set_size(_struct
->fields
, struct_type
->fields
->len
);
122 for (i
= 0; i
< struct_type
->fields
->len
; i
++) {
123 struct type_field
*type_field
=
124 &g_array_index(struct_type
->fields
,
125 struct type_field
, i
);
126 struct field
*field
= &g_array_index(_struct
->fields
,
129 field
->name
= type_field
->name
;
131 type_field
->type
->definition_new(type_field
->type
,
133 ret
= register_field_definition(field
->name
,
142 void _struct_definition_free(struct definition
*definition
)
144 struct definition_struct
*_struct
=
145 container_of(definition
, struct definition_struct
, p
);
148 assert(_struct
->fields
->len
== _struct
->type
->fields
->len
);
149 for (i
= 0; i
< _struct
->fields
->len
; i
++) {
150 struct field
*field
= &g_array_index(_struct
->fields
,
152 definition_unref(field
->definition
);
154 free_definition_scope(_struct
->scope
);
155 type_unref(_struct
->p
.type
);
159 void struct_type_add_field(struct type_struct
*struct_type
,
160 const char *field_name
,
161 struct type
*field_type
)
163 struct type_field
*field
;
166 g_array_set_size(struct_type
->fields
, struct_type
->fields
->len
+ 1);
167 index
= struct_type
->fields
->len
- 1; /* last field (new) */
168 field
= &g_array_index(struct_type
->fields
, struct type_field
, index
);
169 field
->name
= g_quark_from_string(field_name
);
170 type_ref(field_type
);
171 field
->type
= field_type
;
172 /* Keep index in hash rather than pointer, because array can relocate */
173 g_hash_table_insert(struct_type
->fields_by_name
,
174 (gpointer
) (unsigned long) field
->name
,
177 * Alignment of structure is the max alignment of types contained
180 struct_type
->p
.alignment
= max(struct_type
->p
.alignment
,
181 field_type
->alignment
);
185 struct_type_lookup_field_index(struct type_struct
*struct_type
,
190 index
= (unsigned long) g_hash_table_lookup(struct_type
->fields_by_name
,
191 (gconstpointer
) (unsigned long) field_name
);
196 * field returned only valid as long as the field structure is not appended to.
199 struct_type_get_field_from_index(struct type_struct
*struct_type
,
202 return &g_array_index(struct_type
->fields
, struct type_field
, index
);
206 * field returned only valid as long as the field structure is not appended to.
209 struct_definition_get_field_from_index(struct definition_struct
*_struct
,
212 return &g_array_index(_struct
->fields
, struct field
, index
);
This page took 0.03253 seconds and 3 git commands to generate.