Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_TYPES_H |
2 | #define _BABELTRACE_TYPES_H | |
3 | ||
4 | /* | |
5 | * BabelTrace | |
6 | * | |
fc93b2bd | 7 | * Type Header |
d79865b9 | 8 | * |
ccd7e1c8 | 9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
d79865b9 | 10 | * |
ccd7e1c8 MD |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
d79865b9 | 17 | * |
ccd7e1c8 MD |
18 | * The above copyright notice and this permission notice shall be included in |
19 | * all copies or substantial portions of the Software. | |
d79865b9 MD |
20 | */ |
21 | ||
fc93b2bd | 22 | #include <babeltrace/format.h> |
bed864a7 MD |
23 | #include <babeltrace/align.h> |
24 | #include <string.h> | |
25 | ||
11796b96 MD |
26 | /* Preallocate this many fields for structures */ |
27 | #define DEFAULT_NR_STRUCT_FIELDS 8 | |
28 | ||
bed864a7 MD |
29 | /* |
30 | * Always update stream_pos with move_pos and init_pos. | |
31 | */ | |
32 | struct stream_pos { | |
33 | unsigned char *base; /* Base address */ | |
34 | size_t offset; /* Offset from base, in bits */ | |
35 | int dummy; /* Dummy position, for length calculation */ | |
36 | }; | |
37 | ||
38 | static inline | |
39 | void init_pos(struct stream_pos *pos, unsigned char *base) | |
40 | { | |
41 | pos->base = base; /* initial base, page-aligned */ | |
42 | pos->offset = 0; | |
43 | pos->dummy = false; | |
44 | } | |
45 | ||
46 | /* | |
47 | * move_pos - move position of a relative bit offset | |
48 | * | |
49 | * TODO: allow larger files by updating base too. | |
50 | */ | |
51 | static inline | |
52 | void move_pos(struct stream_pos *pos, size_t offset) | |
53 | { | |
54 | pos->offset = pos->offset + offset; | |
55 | } | |
56 | ||
57 | /* | |
58 | * align_pos - align position on a bit offset (> 0) | |
59 | * | |
60 | * TODO: allow larger files by updating base too. | |
61 | */ | |
62 | static inline | |
63 | void align_pos(struct stream_pos *pos, size_t offset) | |
64 | { | |
65 | pos->offset += offset_align(pos->offset, offset); | |
66 | } | |
67 | ||
68 | static inline | |
69 | void copy_pos(struct stream_pos *dest, struct stream_pos *src) | |
70 | { | |
71 | memcpy(dest, src, sizeof(struct stream_pos)); | |
72 | } | |
73 | ||
74 | static inline | |
75 | unsigned char *get_pos_addr(struct stream_pos *pos) | |
76 | { | |
77 | /* Only makes sense to get the address after aligning on CHAR_BIT */ | |
78 | assert(!(pos->alignment % CHAR_BIT)); | |
79 | return pos->base + (pos->offset / CHAR_BIT); | |
80 | } | |
fc93b2bd MD |
81 | |
82 | struct type_class { | |
83 | GQuark name; /* type name */ | |
84 | size_t alignment; /* type alignment, in bits */ | |
85 | /* | |
86 | * Type copy function. Knows how to find the child type_class from the | |
87 | * parent type_class. | |
88 | */ | |
bed864a7 MD |
89 | size_t (*copy)(struct stream_pos *dest, const struct format *fdest, |
90 | struct stream_pos *src, const struct format *fsrc, | |
fc93b2bd | 91 | const struct type_class *type_class); |
90b676d7 | 92 | void (*free)(struct type_class *type_class); |
fc93b2bd MD |
93 | }; |
94 | ||
bed864a7 MD |
95 | /* |
96 | * Because we address in bits, bitfields end up being exactly the same as | |
97 | * integers, except that their read/write functions must be able to deal with | |
98 | * read/write non aligned on CHAR_BIT. | |
99 | */ | |
7fe00194 MD |
100 | struct type_class_integer { |
101 | struct type_class p; | |
102 | size_t len; /* length, in bits. */ | |
103 | int byte_order; /* byte order */ | |
104 | int signedness; | |
fc93b2bd MD |
105 | }; |
106 | ||
107 | struct type_class_float { | |
108 | struct type_class p; | |
7fe00194 MD |
109 | struct int_class *sign; |
110 | struct int_class *mantissa; | |
111 | struct int_class *exp; | |
fc93b2bd | 112 | int byte_order; |
0a46062b | 113 | /* TODO: we might want to express more info about NaN, +inf and -inf */ |
fc93b2bd MD |
114 | }; |
115 | ||
448d3cc7 MD |
116 | struct enum_table { |
117 | GHashTable *value_to_quark; /* Tuples (value, GQuark) */ | |
118 | GHashTable *quark_to_value; /* Tuples (GQuark, value) */ | |
119 | }; | |
120 | ||
fc93b2bd | 121 | struct type_class_enum { |
7fe00194 | 122 | struct type_class_int p; /* inherit from integer */ |
448d3cc7 | 123 | struct enum_table table; |
fc93b2bd MD |
124 | }; |
125 | ||
bed864a7 MD |
126 | struct type_class_string { |
127 | struct type_class p; | |
128 | }; | |
129 | ||
11796b96 MD |
130 | struct field { |
131 | GQuark name; | |
132 | struct type_class *type_class; | |
133 | }; | |
134 | ||
fc93b2bd MD |
135 | struct type_class_struct { |
136 | struct type_class p; | |
11796b96 MD |
137 | GHashTable *fields_by_name; /* Tuples (field name, field index) */ |
138 | GArray *fields; /* Array of fields */ | |
139 | }; | |
140 | ||
141 | struct type_class_array { | |
142 | struct type_class p; | |
143 | size_t len; | |
144 | struct type_class *elem; | |
145 | }; | |
146 | ||
147 | struct type_class_sequence { | |
148 | struct type_class p; | |
2e7d72cf | 149 | struct type_class_integer *len_class; |
11796b96 | 150 | struct type_class *elem; |
fc93b2bd MD |
151 | }; |
152 | ||
698f0fe4 MD |
153 | struct type_class *ctf_lookup_type(GQuark qname); |
154 | int ctf_register_type(struct type_class *type_class); | |
155 | ||
0a46062b MD |
156 | /* Nameless types can be created by passing a NULL name */ |
157 | ||
158 | struct type_class_integer *integer_type_new(const char *name, | |
0a46062b | 159 | size_t len, int byte_order, |
11d43b90 MD |
160 | int signedness, |
161 | size_t alignment); | |
0a46062b MD |
162 | void integer_type_free(struct type_class_integer *int_class); |
163 | ||
11d43b90 MD |
164 | /* |
165 | * mantissa_len is the length of the number of bytes represented by the mantissa | |
166 | * (e.g. result of DBL_MANT_DIG). It includes the leading 1. | |
167 | */ | |
0a46062b MD |
168 | struct type_class_float *float_type_new(const char *name, |
169 | size_t mantissa_len, | |
170 | size_t exp_len, int byte_order, | |
171 | size_t alignment); | |
172 | void float_type_free(struct type_class_float *float_class); | |
173 | ||
448d3cc7 MD |
174 | /* |
175 | * A GQuark can be translated to/from strings with g_quark_from_string() and | |
176 | * g_quark_to_string(). | |
177 | */ | |
178 | GQuark enum_uint_to_quark(const struct type_class_enum *enum_class, uint64_t v); | |
179 | GQuark enum_int_to_quark(const struct type_class_enum *enum_class, uint64_t v); | |
180 | uint64_t enum_quark_to_uint(const struct type_class_enum *enum_class, | |
181 | size_t len, int byte_order, GQuark q); | |
182 | int64_t enum_quark_to_int(const struct type_class_enum *enum_class, | |
183 | size_t len, int byte_order, GQuark q); | |
184 | void enum_signed_insert(struct type_class_enum *enum_class, | |
185 | int64_t v, GQuark q); | |
186 | void enum_unsigned_insert(struct type_class_enum *enum_class, | |
187 | uint64_t v, GQuark q); | |
188 | ||
189 | struct type_class_enum *enum_type_new(const char *name, | |
448d3cc7 MD |
190 | size_t len, int byte_order, |
191 | int signedness, | |
192 | size_t alignment); | |
193 | void enum_type_free(struct type_class_enum *enum_class); | |
194 | ||
11796b96 MD |
195 | struct type_class_struct *struct_type_new(const char *name); |
196 | void struct_type_free(struct type_class_struct *struct_class); | |
197 | void struct_type_add_field(struct type_class_struct *struct_class, | |
198 | GQuark field_name, | |
199 | struct type_class *type_class); | |
200 | /* | |
201 | * Returns the index of a field within a structure. | |
202 | */ | |
203 | unsigned long | |
204 | struct_type_lookup_field_index(struct type_class_struct *struct_class, | |
205 | GQuark field_name); | |
206 | /* | |
207 | * field returned only valid as long as the field structure is not appended to. | |
208 | */ | |
209 | struct field * | |
210 | struct_type_get_field_from_index(struct type_class_struct *struct_class, | |
211 | unsigned long index); | |
212 | ||
d06d03db MD |
213 | /* |
214 | * elem_class passed as parameter now belongs to the array. No need to free it | |
215 | * explicitely. | |
216 | */ | |
11796b96 MD |
217 | struct type_class_array *array_type_new(const char *name, |
218 | size_t len, | |
219 | struct type_class *elem_class); | |
220 | void array_type_free(struct type_class_array *array_class); | |
221 | ||
d06d03db MD |
222 | /* |
223 | * int_class and elem_class passed as parameter now belongs to the sequence. No | |
224 | * need to free them explicitely. | |
225 | */ | |
11796b96 | 226 | struct type_class_sequence *sequence_type_new(const char *name, |
2e7d72cf | 227 | struct type_class_integer *len_class, |
11796b96 | 228 | struct type_class *elem_class); |
d06d03db | 229 | void sequence_type_free(struct type_class_sequence *sequence_class); |
11796b96 | 230 | |
d79865b9 | 231 | #endif /* _BABELTRACE_TYPES_H */ |