2 * BabelTrace - Structure Type Converter
4 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/types.h>
24 void struct_copy(struct stream_pos
*dest
, const struct format
*fdest
,
25 struct stream_pos
*src
, const struct format
*fsrc
,
26 const struct type_class
*type_class
)
28 struct type_class_struct
*struct_class
=
29 container_of(type_class
, struct type_class_struct
, p
);
32 fsrc
->struct_begin(src
, struct_class
);
33 fdest
->struct_begin(dest
, struct_class
);
35 for (i
= 0; i
< struct_class
->fields
->len
; i
++) {
36 struct field
*field
= &g_array_index(struct_class
->fields
,
38 struct type_class
*field_class
= field
->type_class
;
40 field_class
->copy(dest
, fdest
, src
, fsrc
, &field_class
->p
);
43 fsrc
->struct_end(src
, struct_class
);
44 fdest
->struct_end(dest
, struct_class
);
47 void struct_type_free(struct type_class_struct
*struct_class
)
49 g_hash_table_destroy(struct_class
->fields_by_name
);
50 g_array_free(struct_class
->fields
, true);
54 static void _struct_type_free(struct type_class
*type_class
)
56 struct type_class_struct
*struct_class
=
57 container_of(type_class
, struct type_class_struct
, p
);
58 struct_type_free(struct_class
);
61 struct type_class_struct
*struct_type_new(const char *name
)
63 struct type_class_struct
*struct_class
;
66 struct_class
= g_new(struct type_class_struct
, 1);
67 type_class
= &float_class
->p
;
69 struct_class
->fields_by_name
= g_hash_table_new(g_direct_hash
,
71 struct_class
->fields
= g_array_sized_new(false, false,
73 DEFAULT_NR_STRUCT_FIELDS
)
74 type_class
->name
= g_quark_from_string(name
);
75 type_class
->alignment
= 1;
76 type_class
->copy
= struct_copy
;
77 type_class
->free
= _struct_type_free
;
79 if (type_class
->name
) {
80 ret
= ctf_register_type(type_class
);
91 void struct_type_add_field(struct type_class_struct
*struct_class
,
92 const char *field_name
,
93 struct type_class
*type_class
)
98 g_array_set_size(struct_class
->fields
, struct_class
->fields
->len
+ 1);
99 index
= struct_class
->fields
->len
- 1; /* last field (new) */
100 field
= &g_array_index(struct_class
->fields
, struct field
, index
);
101 field
->name
= g_quark_from_string(field_name
);
102 field
->type_class
= type_class
;
103 /* Keep index in hash rather than pointer, because array can relocate */
104 g_hash_table_insert(struct_class
->fields_by_name
,
105 (gpointer
) (unsigned long) field
->name
,
108 * Alignment of structure is the max alignment of types contained
111 struct_class
->p
.alignment
= max(struct_class
->p
.alignment
,
112 type_class
->alignment
);
116 struct_type_lookup_field_index(struct type_class_struct
*struct_class
,
121 index
= (unsigned long) g_hash_table_lookup(struct_class
->fields_by_name
,
127 * field returned only valid as long as the field structure is not appended to.
130 struct_type_get_field_from_index(struct type_class_struct
*struct_class
,
133 return &g_array_index(struct_class
->fields
, struct field
, index
);
This page took 0.036058 seconds and 4 git commands to generate.