add ltt_type_name
[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_facility : get the facility which contains the event type
84 *Input params
85 * et : an event type
86 *Return value
87 * LttFacility * : the facility
88 ****************************************************************************/
89
90 LttFacility *ltt_eventtype_facility(LttEventType *et)
91 {
92 return et->facility;
93 }
94
95 /*****************************************************************************
96 *Function name
97 * ltt_eventtype_id : get the id of the event type
98 *Input params
99 * et : an event type
100 *Return value
101 * unsigned : the id
102 ****************************************************************************/
103
104 guint8 ltt_eventtype_id(LttEventType *et)
105 {
106 return et->index;
107 }
108
109 /*****************************************************************************
110 *Function name
111 * ltt_type_name : get the name of the type
112 *Input params
113 * t : a type
114 *Return value
115 * GQuark : the name of the type
116 ****************************************************************************/
117
118 GQuark ltt_type_name(LttType *t)
119 {
120 return g_quark_from_static_string(typeNames[t->type_class]);
121 }
122
123 /*****************************************************************************
124 *Function name
125 * ltt_field_name : get the name of the field
126 *Input params
127 * f : a field
128 *Return value
129 * char * : the name of the type
130 ****************************************************************************/
131
132 GQuark ltt_field_name(LttField *f)
133 {
134 return f->name;
135 }
136 /*****************************************************************************
137 *Function name
138 * ltt_type_class : get the type class of the type
139 *Input params
140 * t : a type
141 *Return value
142 * LttTypeEnum : the type class of the type
143 ****************************************************************************/
144
145 LttTypeEnum ltt_type_class(LttType *t)
146 {
147 return t->type_class;
148 }
149
150 /*****************************************************************************
151 *Function name
152 * ltt_type_size : obtain the type size. The size is the number of bytes
153 * for primitive types (INT, UINT, FLOAT, ENUM)
154 * or the size for the unsigned integer length count for
155 * sequences
156 *Input params
157 * tf : trace file
158 * t : a type
159 *Return value
160 * : the type size
161 * returns 0 if erroneous, and show a critical warning message.
162 ****************************************************************************/
163
164 guint ltt_type_size(LttTrace * trace, LttType *t)
165 {
166 guint size;
167
168 switch(t->type_class) {
169 case LTT_INT_FIXED:
170 case LTT_UINT_FIXED:
171 case LTT_CHAR:
172 case LTT_UCHAR:
173 case LTT_SHORT:
174 case LTT_USHORT:
175 case LTT_INT:
176 case LTT_UINT:
177 case LTT_ENUM:
178 if(likely(t->size < INT_SIZES_NUMBER))
179 size = intSizes[t->size];
180 else
181 goto error;
182 break;
183 case LTT_FLOAT:
184 if(likely(t->size < FLOAT_SIZES_NUMBER))
185 size = floatSizes[t->size];
186 else
187 goto error;
188 break;
189 case LTT_POINTER:
190 case LTT_LONG:
191 case LTT_ULONG:
192 case LTT_SIZE_T:
193 case LTT_SSIZE_T:
194 case LTT_SEQUENCE:
195 case LTT_OFF_T:
196 case LTT_STRING:
197 case LTT_ARRAY:
198 case LTT_STRUCT:
199 case LTT_UNION:
200 case LTT_NONE:
201 goto error;
202 break;
203 }
204
205 return size;
206
207
208 error:
209 g_warning("no size known for the type");
210 return 0;
211 }
212
213 /*****************************************************************************
214 *Function name
215 * ltt_type_element_type : obtain the type of nested elements for arrays
216 * and sequences
217 *Input params
218 * t : a type
219 *Return value
220 * LttType : the type of nested element of array or sequence
221 ****************************************************************************/
222
223 LttType *ltt_type_element_type(LttType *t)
224 {
225 LttType *element_type;
226 LttField *field;
227
228 if(unlikely(t->type_class != LTT_ARRAY && t->type_class != LTT_SEQUENCE))
229 element_type = NULL;
230 else {
231 if(t->type_class == LTT_ARRAY)
232 field = &g_array_index(t->fields, LttField, 0);
233 else
234 field = &g_array_index(t->fields, LttField, 1);
235 element_type = ltt_field_type(field);
236 }
237
238 return element_type;
239 }
240
241 /*****************************************************************************
242 *Function name
243 * ltt_type_element_number : obtain the number of elements for enums
244 *Input params
245 * t : a type
246 *Return value
247 * unsigned : the number of elements for arrays
248 ****************************************************************************/
249 unsigned ltt_type_element_number(LttType *t)
250 {
251 unsigned ret = 0;
252
253 if(likely(t->type_class == LTT_ENUM))
254 ret = g_hash_table_size(t->enum_map);
255
256 return ret;
257 }
258
259 /*****************************************************************************
260 *Function name
261 * ltt_type_member_number : obtain the number of data members for structure
262 *Input params
263 * t : a type
264 *Return value
265 * unsigned : the number of members for structure
266 ****************************************************************************/
267
268 unsigned ltt_type_member_number(LttType *t)
269 {
270 unsigned ret = 0;
271
272 if(likely(t->type_class == LTT_STRUCT || t->type_class == LTT_UNION))
273 ret = t->fields->len;
274
275 return ret;
276 }
277
278
279 /*****************************************************************************
280 *Function name
281 * ltt_enum_string_get : for enumerations, obtain the symbolic string
282 * associated with a value (0 to n - 1 for an
283 * enumeration of n elements)
284 *Input params
285 * t : a type
286 * i : index of the member
287 *Return value
288 * char * : symbolic string associated with a value
289 ****************************************************************************/
290
291 GQuark ltt_enum_string_get(LttType *t, gulong i)
292 {
293 if(likely(t->type_class == LTT_ENUM))
294 return (GQuark)g_hash_table_lookup(t->enum_map, (gpointer)i);
295 else
296 return 0;
297 }
298 #if 0
299 /*****************************************************************************
300 *Function name
301 * ltt_field_element : obtain the field of nested elements for arrays and
302 * sequence
303 *Input params
304 * f : a field
305 *Return value
306 * LttField * : the field of the nested element
307 ****************************************************************************/
308
309 LttField *ltt_field_element(LttField *f)
310 {
311 LttField *nest = NULL;
312
313 if(likely(f->field_type->type_class == LTT_ARRAY ||
314 f->field_type->type_class == LTT_SEQUENCE))
315 nest = f->child[0];
316
317 return nest;
318 }
319 #endif//0
320
321 /*****************************************************************************
322 *Function name
323 * ltt_field_member_by_name : obtain the field of data members for structure
324 *Input params
325 * f : a field
326 * name : name of the field
327 *Return value
328 * LttField * : the field of the nested element
329 ****************************************************************************/
330
331 LttField *ltt_field_member_by_name(LttField *f, GQuark name)
332 {
333 LttField *field_member;
334
335 g_assert(f->field_type.type_class == LTT_STRUCT ||
336 f->field_type.type_class == LTT_UNION);
337
338 field_member = g_datalist_id_get_data(&f->field_type.fields_by_name, name);
339
340 return field_member;
341 }
342
343
344 /*****************************************************************************
345 *Function name
346 * ltt_field_member : obtain the field of data members for structure
347 *Input params
348 * f : a field
349 * i : index of member field
350 *Return value
351 * LttField * : the field of the nested element
352 ****************************************************************************/
353
354 LttField *ltt_field_member(LttField *f, guint i)
355 {
356 LttField *field_member;
357
358 g_assert(f->field_type.type_class == LTT_STRUCT ||
359 f->field_type.type_class == LTT_UNION);
360 g_assert(i < f->field_type.fields->len);
361
362 field_member = &g_array_index(f->field_type.fields, LttField, i);
363
364 return field_member;
365 }
366
367 /*****************************************************************************
368 *Function name
369 * ltt_field_type : obtain the type of the field
370 *Input params
371 * f : a field
372 *Return value
373 * ltt_tyoe * : the type of field
374 ****************************************************************************/
375
376 LttType *ltt_field_type(LttField *f)
377 {
378 if(unlikely(!f))return NULL;
379 return &f->field_type;
380 }
381
382 int ltt_field_size(LttField * f)
383 {
384 if(unlikely(!f))return 0;
385 return f->field_size;
386 }
387
388
389 /*****************************************************************************
390 *Function name
391 * ltt_eventtype_num_fields : get the number of fields of the event
392 *Input params
393 * e : an instance of an event type
394 *Return value
395 * guint : number of fields
396 ****************************************************************************/
397
398 guint ltt_eventtype_num_fields(LttEventType *event_type)
399 {
400 if(unlikely(!event_type)) return 0;
401
402 return event_type->fields->len;
403
404 }
405 /*****************************************************************************
406 *Function name
407 * ltt_eventtype_field : get the i th field of the event
408 *Input params
409 * e : an instance of an event type
410 * i : field index
411 *Return value
412 * LttField * : The requested field, or NULL
413 ****************************************************************************/
414
415 LttField *ltt_eventtype_field(LttEventType *event_type, guint i)
416 {
417 if(unlikely(!event_type)) return NULL;
418
419 if(i >= event_type->fields->len) return NULL;
420
421 return &g_array_index(event_type->fields, LttField, i);
422
423 }
424
425 /*****************************************************************************
426 *Function name
427 * ltt_eventtype_field_by_name : get a field of the event
428 *Input params
429 * e : an instance of an event type
430 * name : field name
431 *Return value
432 * LttField * : The requested field, or NULL
433 ****************************************************************************/
434
435 LttField *ltt_eventtype_field_by_name(LttEventType *event_type, GQuark name)
436 {
437 if(unlikely(!event_type)) return NULL;
438
439 return (LttField*)g_datalist_id_get_data(&event_type->fields_by_name, name);
440
441 }
442
443
This page took 0.040216 seconds and 4 git commands to generate.