Commit | Line | Data |
---|---|---|
11796b96 | 1 | /* |
ccd7e1c8 | 2 | * struct.c |
11796b96 | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Structure Type Converter |
11796b96 | 5 | * |
ccd7e1c8 | 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
11796b96 | 7 | * |
ccd7e1c8 MD |
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: | |
11796b96 | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
11796b96 MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
11796b96 MD |
21 | |
22 | void struct_copy(struct stream_pos *dest, const struct format *fdest, | |
23 | struct stream_pos *src, const struct format *fsrc, | |
24 | const struct type_class *type_class) | |
25 | { | |
26 | struct type_class_struct *struct_class = | |
27 | container_of(type_class, struct type_class_struct, p); | |
28 | unsigned int i; | |
29 | ||
30 | fsrc->struct_begin(src, struct_class); | |
31 | fdest->struct_begin(dest, struct_class); | |
32 | ||
33 | for (i = 0; i < struct_class->fields->len; i++) { | |
34 | struct field *field = &g_array_index(struct_class->fields, | |
35 | struct field, i); | |
36 | struct type_class *field_class = field->type_class; | |
37 | ||
4c8bfb7e | 38 | field_class->copy(dest, fdest, src, fsrc, field_class); |
11796b96 MD |
39 | |
40 | } | |
41 | fsrc->struct_end(src, struct_class); | |
42 | fdest->struct_end(dest, struct_class); | |
43 | } | |
44 | ||
45 | void struct_type_free(struct type_class_struct *struct_class) | |
46 | { | |
4c8bfb7e MD |
47 | unsigned int i; |
48 | ||
11796b96 | 49 | g_hash_table_destroy(struct_class->fields_by_name); |
4c8bfb7e MD |
50 | |
51 | for (i = 0; i < struct_class->fields->len; i++) { | |
52 | struct field *field = &g_array_index(struct_class->fields, | |
53 | struct field, i); | |
54 | type_unref(field->type_class); | |
55 | } | |
11796b96 MD |
56 | g_array_free(struct_class->fields, true); |
57 | g_free(struct_class); | |
58 | } | |
59 | ||
60 | static void _struct_type_free(struct type_class *type_class) | |
61 | { | |
62 | struct type_class_struct *struct_class = | |
63 | container_of(type_class, struct type_class_struct, p); | |
64 | struct_type_free(struct_class); | |
65 | } | |
66 | ||
67 | struct type_class_struct *struct_type_new(const char *name) | |
68 | { | |
69 | struct type_class_struct *struct_class; | |
4c8bfb7e | 70 | struct type_class *type_class; |
11796b96 MD |
71 | int ret; |
72 | ||
73 | struct_class = g_new(struct type_class_struct, 1); | |
4c8bfb7e | 74 | type_class = &struct_class->p; |
11796b96 MD |
75 | |
76 | struct_class->fields_by_name = g_hash_table_new(g_direct_hash, | |
77 | g_direct_equal); | |
78 | struct_class->fields = g_array_sized_new(false, false, | |
79 | sizeof(struct field), | |
4c8bfb7e | 80 | DEFAULT_NR_STRUCT_FIELDS); |
11796b96 MD |
81 | type_class->name = g_quark_from_string(name); |
82 | type_class->alignment = 1; | |
83 | type_class->copy = struct_copy; | |
84 | type_class->free = _struct_type_free; | |
4c8bfb7e | 85 | type_class->ref = 1; |
11796b96 MD |
86 | |
87 | if (type_class->name) { | |
88 | ret = ctf_register_type(type_class); | |
89 | if (ret) | |
90 | goto error_register; | |
91 | } | |
92 | return struct_class; | |
93 | ||
94 | error_register: | |
95 | g_free(struct_class); | |
96 | return NULL; | |
97 | } | |
98 | ||
99 | void struct_type_add_field(struct type_class_struct *struct_class, | |
100 | const char *field_name, | |
101 | struct type_class *type_class) | |
102 | { | |
103 | struct field *field; | |
104 | unsigned long index; | |
105 | ||
106 | g_array_set_size(struct_class->fields, struct_class->fields->len + 1); | |
107 | index = struct_class->fields->len - 1; /* last field (new) */ | |
108 | field = &g_array_index(struct_class->fields, struct field, index); | |
109 | field->name = g_quark_from_string(field_name); | |
4c8bfb7e | 110 | type_ref(type_class); |
11796b96 MD |
111 | field->type_class = type_class; |
112 | /* Keep index in hash rather than pointer, because array can relocate */ | |
113 | g_hash_table_insert(struct_class->fields_by_name, | |
114 | (gpointer) (unsigned long) field->name, | |
115 | (gpointer) index); | |
116 | /* | |
117 | * Alignment of structure is the max alignment of types contained | |
118 | * therein. | |
119 | */ | |
120 | struct_class->p.alignment = max(struct_class->p.alignment, | |
121 | type_class->alignment); | |
122 | } | |
123 | ||
124 | unsigned long | |
125 | struct_type_lookup_field_index(struct type_class_struct *struct_class, | |
126 | GQuark field_name) | |
127 | { | |
128 | unsigned long index; | |
129 | ||
130 | index = (unsigned long) g_hash_table_lookup(struct_class->fields_by_name, | |
4c8bfb7e | 131 | (gconstpointer) (unsigned long) field_name); |
11796b96 MD |
132 | return index; |
133 | } | |
134 | ||
135 | /* | |
136 | * field returned only valid as long as the field structure is not appended to. | |
137 | */ | |
138 | struct field * | |
139 | struct_type_get_field_from_index(struct type_class_struct *struct_class, | |
140 | unsigned long index) | |
141 | { | |
142 | return &g_array_index(struct_class->fields, struct field, index); | |
143 | } |