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