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