libltt compiles
[lttv.git] / ltt / branches / poly / ltt / type.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 * 2005 Mathieu Desnoyers
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License Version 2.1 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <glib.h>
26
27 #include "parser.h"
28 #include <ltt/ltt.h>
29 #include "ltt-private.h"
30 #include <ltt/type.h>
31
32 static unsigned intSizes[] = {
33 sizeof(int8_t), sizeof(int16_t), sizeof(int32_t), sizeof(int64_t),
34 sizeof(short) };
35
36 static unsigned floatSizes[] = {
37 0, 0, sizeof(float), sizeof(double), 0, sizeof(float), sizeof(double) };
38
39
40 typedef enum _intSizesNames { SIZE_INT8, SIZE_INT16, SIZE_INT32,
41 SIZE_INT64, SIZE_SHORT, INT_SIZES_NUMBER }
42 intSizesNames;
43
44 static char * typeNames[] = {
45 "int_fixed", "uint_fixed", "pointer", "char", "uchar", "short", "ushort",
46 "int", "uint", "long", "ulong", "size_t", "ssize_t", "off_t", "float",
47 "string", "enum", "array", "sequence", "struct", "union", "none" };
48
49
50 #define FLOAT_SIZES_NUMBER 7
51
52
53 /*****************************************************************************
54 *Function name
55 * ltt_eventtype_name : get the name of the event type
56 *Input params
57 * et : an event type
58 *Return value
59 * GQuark : the name of the event type
60 ****************************************************************************/
61
62 GQuark ltt_eventtype_name(LttEventType *et)
63 {
64 return et->name;
65 }
66
67 /*****************************************************************************
68 *Function name
69 * ltt_eventtype_description : get the description of the event type
70 *Input params
71 * et : an event type
72 *Return value
73 * char * : the description of the event type
74 ****************************************************************************/
75
76 gchar *ltt_eventtype_description(LttEventType *et)
77 {
78 return et->description;
79 }
80
81 /*****************************************************************************
82 *Function name
83 * ltt_eventtype_id : get the id of the event type
84 *Input params
85 * et : an event type
86 *Return value
87 * unsigned : the id
88 ****************************************************************************/
89
90 guint8 ltt_eventtype_id(LttEventType *et)
91 {
92 return et->index;
93 }
94
95 /*****************************************************************************
96 *Function name
97 * ltt_type_name : get the name of the type
98 *Input params
99 * t : a type
100 *Return value
101 * GQuark : the name of the type
102 ****************************************************************************/
103
104 GQuark ltt_type_name(LttType *t)
105 {
106 return g_quark_from_static_string(typeNames[t->type_class]);
107 }
108
109 /*****************************************************************************
110 *Function name
111 * ltt_field_name : get the name of the field
112 *Input params
113 * f : a field
114 *Return value
115 * char * : the name of the type
116 ****************************************************************************/
117
118 GQuark ltt_field_name(LttField *f)
119 {
120 return f->name;
121 }
122
123 /*****************************************************************************
124 *Function name
125 * ltt_type_class : get the type class of the type
126 *Input params
127 * t : a type
128 *Return value
129 * LttTypeEnum : the type class of the type
130 ****************************************************************************/
131
132 LttTypeEnum ltt_type_class(LttType *t)
133 {
134 return t->type_class;
135 }
136
137 /*****************************************************************************
138 *Function name
139 * ltt_type_size : obtain the type size. The size is the number of bytes
140 * for primitive types (INT, UINT, FLOAT, ENUM)
141 * or the size for the unsigned integer length count for
142 * sequences
143 *Input params
144 * tf : trace file
145 * t : a type
146 *Return value
147 * : the type size
148 * returns 0 if erroneous, and show a critical warning message.
149 ****************************************************************************/
150
151 guint ltt_type_size(LttTrace * trace, LttType *t)
152 {
153 guint size;
154
155 switch(t->type_class) {
156 case LTT_INT_FIXED:
157 case LTT_UINT_FIXED:
158 case LTT_CHAR:
159 case LTT_UCHAR:
160 case LTT_SHORT:
161 case LTT_USHORT:
162 case LTT_INT:
163 case LTT_UINT:
164 case LTT_ENUM:
165 if(likely(t->size < INT_SIZES_NUMBER))
166 size = intSizes[t->size];
167 else
168 goto error;
169 break;
170 case LTT_FLOAT:
171 if(likely(t->size < FLOAT_SIZES_NUMBER))
172 size = floatSizes[t->size];
173 else
174 goto error;
175 break;
176 case LTT_POINTER:
177 case LTT_LONG:
178 case LTT_ULONG:
179 case LTT_SIZE_T:
180 case LTT_SSIZE_T:
181 case LTT_SEQUENCE:
182 case LTT_OFF_T:
183 case LTT_STRING:
184 case LTT_ARRAY:
185 case LTT_STRUCT:
186 case LTT_UNION:
187 case LTT_NONE:
188 goto error;
189 break;
190 }
191
192 return size;
193
194
195 error:
196 g_warning("no size known for the type");
197 return 0;
198 }
199
200 /*****************************************************************************
201 *Function name
202 * ltt_type_element_type : obtain the type of nested elements for arrays
203 * and sequences
204 *Input params
205 * t : a type
206 *Return value
207 * LttType : the type of nested element of array or sequence
208 ****************************************************************************/
209
210 LttType *ltt_type_element_type(LttType *t)
211 {
212 LttType *element_type;
213 LttField *field;
214
215 if(unlikely(t->type_class != LTT_ARRAY && t->type_class != LTT_SEQUENCE))
216 element_type = NULL;
217 else {
218 if(t->type_class == LTT_ARRAY)
219 field = &g_array_index(t->fields, LttField, 0);
220 else
221 field = &g_array_index(t->fields, LttField, 1);
222 element_type = ltt_field_type(field);
223 }
224
225 return element_type;
226 }
227
228 /*****************************************************************************
229 *Function name
230 * ltt_type_element_number : obtain the number of elements for enums
231 *Input params
232 * t : a type
233 *Return value
234 * unsigned : the number of elements for arrays
235 ****************************************************************************/
236 unsigned ltt_type_element_number(LttType *t)
237 {
238 unsigned ret = 0;
239
240 if(likely(t->type_class == LTT_ENUM))
241 // Permits non full enums ret = g_hash_table_size(t->enum_map);
242 ret = (unsigned)(t->highest_value - t->lowest_value);
243
244 return ret;
245 }
246
247 /*****************************************************************************
248 *Function name
249 * ltt_type_member_number : obtain the number of data members for structure
250 *Input params
251 * t : a type
252 *Return value
253 * unsigned : the number of members for structure
254 ****************************************************************************/
255
256 unsigned ltt_type_member_number(LttType *t)
257 {
258 unsigned ret = 0;
259
260 if(likely(t->type_class == LTT_STRUCT || t->type_class == LTT_UNION))
261 ret = t->fields->len;
262
263 return ret;
264 }
265
266
267 /*****************************************************************************
268 *Function name
269 * ltt_enum_string_get : for enumerations, obtain the symbolic string
270 * associated with a value (0 to n - 1 for an
271 * enumeration of n elements)
272 *Input params
273 * t : a type
274 * i : index of the member
275 *Return value
276 * char * : symbolic string associated with a value
277 ****************************************************************************/
278
279 GQuark ltt_enum_string_get(LttType *t, gulong i)
280 {
281 if(likely(t->type_class == LTT_ENUM))
282 return (GQuark)g_hash_table_lookup(t->enum_map, (gpointer)i);
283 else
284 return 0;
285 }
286 #if 0
287 /*****************************************************************************
288 *Function name
289 * ltt_field_element : obtain the field of nested elements for arrays and
290 * sequence
291 *Input params
292 * f : a field
293 *Return value
294 * LttField * : the field of the nested element
295 ****************************************************************************/
296
297 LttField *ltt_field_element(LttField *f)
298 {
299 LttField *nest = NULL;
300
301 if(likely(f->field_type->type_class == LTT_ARRAY ||
302 f->field_type->type_class == LTT_SEQUENCE))
303 nest = f->child[0];
304
305 return nest;
306 }
307 #endif//0
308
309 /*****************************************************************************
310 *Function name
311 * ltt_field_member_by_name : obtain the field of data members for structure
312 *Input params
313 * f : a field
314 * name : name of the field
315 *Return value
316 * LttField * : the field of the nested element
317 ****************************************************************************/
318
319 LttField *ltt_field_member_by_name(LttField *f, GQuark name)
320 {
321 LttField *field_member;
322
323 g_assert(f->field_type.type_class == LTT_STRUCT ||
324 f->field_type.type_class == LTT_UNION);
325
326 field_member = g_datalist_id_get_data(&f->field_type.fields_by_name, name);
327
328 return field_member;
329 }
330
331
332 /*****************************************************************************
333 *Function name
334 * ltt_field_member : obtain the field of data members for structure
335 *Input params
336 * f : a field
337 * i : index of member field
338 *Return value
339 * LttField * : the field of the nested element
340 ****************************************************************************/
341
342 LttField *ltt_field_member(LttField *f, guint i)
343 {
344 LttField *field_member;
345
346 g_assert(f->field_type.type_class == LTT_STRUCT ||
347 f->field_type.type_class == LTT_UNION);
348 g_assert(i < f->field_type.fields->len);
349
350 field_member = &g_array_index(f->field_type.fields, LttField, i);
351
352 return field_member;
353 }
354
355 /*****************************************************************************
356 *Function name
357 * ltt_field_type : obtain the type of the field
358 *Input params
359 * f : a field
360 *Return value
361 * ltt_tyoe * : the type of field
362 ****************************************************************************/
363
364 LttType *ltt_field_type(LttField *f)
365 {
366 if(unlikely(!f))return NULL;
367 return &f->field_type;
368 }
369
370 int ltt_field_size(LttField * f)
371 {
372 if(unlikely(!f))return 0;
373 return f->field_size;
374 }
375
376
377 /*****************************************************************************
378 *Function name
379 * ltt_eventtype_num_fields : get the number of fields of the event
380 *Input params
381 * e : an instance of an event type
382 *Return value
383 * guint : number of fields
384 ****************************************************************************/
385
386 guint ltt_eventtype_num_fields(LttEventType *event_type)
387 {
388 if(unlikely(!event_type)) return 0;
389
390 return event_type->fields->len;
391
392 }
393 /*****************************************************************************
394 *Function name
395 * ltt_eventtype_field : get the i th field of the event
396 *Input params
397 * e : an instance of an event type
398 * i : field index
399 *Return value
400 * LttField * : The requested field, or NULL
401 ****************************************************************************/
402
403 LttField *ltt_eventtype_field(LttEventType *event_type, guint i)
404 {
405 if(unlikely(!event_type)) return NULL;
406
407 if(i >= event_type->fields->len) return NULL;
408
409 return &g_array_index(event_type->fields, LttField, i);
410
411 }
412
413 /*****************************************************************************
414 *Function name
415 * ltt_eventtype_field_by_name : get a field of the event
416 *Input params
417 * e : an instance of an event type
418 * name : field name
419 *Return value
420 * LttField * : The requested field, or NULL
421 ****************************************************************************/
422
423 LttField *ltt_eventtype_field_by_name(LttEventType *event_type, GQuark name)
424 {
425 if(unlikely(!event_type)) return NULL;
426
427 return (LttField*)g_datalist_id_get_data(&event_type->fields_by_name, name);
428
429 }
430
431
This page took 0.043377 seconds and 4 git commands to generate.