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