git-svn-id: http://ltt.polymtl.ca/svn@145 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / ltt / facility.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 #include <ltt/LTTTypes.h>
6 #include "parser.h"
7 #include <ltt/facility.h>
8
9 /* search for the (named) type in the table, if it does not exist
10 create a new one */
11 LttType * lookup_named_type(LttFacility *fac, type_descriptor * td);
12
13 /* construct directed acyclic graph for types, and tree for fields */
14 void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
15 LttField * fld);
16
17 /* generate the facility according to the events belongin to it */
18 void generateFacility(LttFacility * f, facility * fac,
19 LttChecksum checksum);
20
21 /* functions to release the memory occupied by a facility */
22 void freeFacility(LttFacility * facility);
23 void freeEventtype(LttEventType * evType);
24 void freeLttType(LttType ** type);
25 void freeLttField(LttField * fld);
26
27
28 /*****************************************************************************
29 *Function name
30 * ltt_facility_open : open facilities
31 *Input params
32 * t : the trace containing the facilities
33 * pathname : the path name of the facility
34 ****************************************************************************/
35
36 void ltt_facility_open(LttTrace * t, char * pathname)
37 {
38 char *token;
39 parse_file in;
40 char buffer[BUFFER_SIZE];
41 facility * fac;
42 LttFacility * f;
43 LttChecksum checksum;
44
45 in.buffer = buffer;
46 in.lineno = 0;
47 in.error = error_callback;
48 in.name = pathname;
49
50 in.fp = fopen(in.name, "r");
51 if(!in.fp ) in.error(&in,"cannot open input file");
52
53 while(1){
54 token = getToken(&in);
55 if(in.type == ENDFILE) break;
56
57 if(strcmp(token, "<")) in.error(&in,"not a facility file");
58 token = getName(&in);
59
60 if(strcmp("facility",token) == 0) {
61 fac = g_new(facility, 1);
62 fac->name = NULL;
63 fac->description = NULL;
64 sequence_init(&(fac->events));
65 table_init(&(fac->named_types));
66 sequence_init(&(fac->unnamed_types));
67
68 parseFacility(&in, fac);
69
70 //check if any namedType is not defined
71 checkNamedTypesImplemented(&fac->named_types);
72
73 generateChecksum(fac->name, &checksum, &fac->events);
74
75 f = g_new(LttFacility,1);
76 generateFacility(f, fac, checksum);
77
78 t->facility_number++;
79 g_ptr_array_add(t->facilities,f);
80
81 free(fac->name);
82 free(fac->description);
83 freeEvents(&fac->events);
84 sequence_dispose(&fac->events);
85 freeNamedType(&fac->named_types);
86 table_dispose(&fac->named_types);
87 freeTypes(&fac->unnamed_types);
88 sequence_dispose(&fac->unnamed_types);
89 g_free(fac);
90 }
91 else in.error(&in,"facility token was expected");
92 }
93 fclose(in.fp);
94 }
95
96
97 /*****************************************************************************
98 *Function name
99 * generateFacility : generate facility, internal function
100 *Input params
101 * facility : LttFacilty structure
102 * fac : facility structure
103 * checksum : checksum of the facility
104 ****************************************************************************/
105
106 void generateFacility(LttFacility *f, facility *fac,LttChecksum checksum)
107 {
108 char * facilityName = fac->name;
109 sequence * events = &fac->events;
110 int i;
111 LttEventType * evType;
112 LttField * field;
113 LttType * type;
114
115 f->name = g_strdup(facilityName);
116 f->event_number = events->position;
117 f->checksum = checksum;
118
119 //initialize inner structures
120 f->events = g_new(LttEventType*,f->event_number);
121 f->named_types_number = fac->named_types.keys.position;
122 f->named_types = g_new(LttType*, fac->named_types.keys.position);
123 for(i=0;i<fac->named_types.keys.position;i++) f->named_types[i] = NULL;
124
125 //for each event, construct field tree and type graph
126 for(i=0;i<events->position;i++){
127 evType = g_new(LttEventType,1);
128 f->events[i] = evType;
129
130 evType->name = g_strdup(((event*)(events->array[i]))->name);
131 evType->description=g_strdup(((event*)(events->array[i]))->description);
132
133 field = g_new(LttField, 1);
134 evType->root_field = field;
135 evType->facility = f;
136 evType->index = i;
137
138 if(((event*)(events->array[i]))->type != NULL){
139 field->field_pos = 0;
140 type = lookup_named_type(f,((event*)(events->array[i]))->type);
141 field->field_type = type;
142 field->offset_root = 0;
143 field->fixed_root = 1;
144 field->offset_parent = 0;
145 field->fixed_parent = 1;
146 // field->base_address = NULL;
147 field->field_size = 0;
148 field->field_fixed = -1;
149 field->parent = NULL;
150 field->child = NULL;
151 field->current_element = 0;
152
153 //construct field tree and type graph
154 constructTypeAndFields(f,((event*)(events->array[i]))->type,field);
155 }else{
156 evType->root_field = NULL;
157 g_free(field);
158 }
159 }
160 }
161
162
163 /*****************************************************************************
164 *Function name
165 * constructTypeAndFields : construct field tree and type graph,
166 * internal recursion function
167 *Input params
168 * fac : facility struct
169 * td : type descriptor
170 * root_field : root field of the event
171 ****************************************************************************/
172
173 void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
174 LttField * fld)
175 {
176 int i;
177 type_descriptor * tmpTd;
178
179 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
180 // fld->field_size = 0;
181 // else fld->field_size = -1;
182
183 if(td->type == LTT_ENUM){
184 fld->field_type->element_number = td->labels.position;
185 fld->field_type->enum_strings = g_new(char*,td->labels.position);
186 for(i=0;i<td->labels.position;i++){
187 fld->field_type->enum_strings[i]
188 = g_strdup(((char*)(td->labels.array[i])));
189 }
190 }else if(td->type == LTT_ARRAY || td->type == LTT_SEQUENCE){
191 if(td->type == LTT_ARRAY)
192 fld->field_type->element_number = (unsigned)td->size;
193 fld->field_type->element_type = g_new(LttType*,1);
194 tmpTd = td->nested_type;
195 fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd);
196 fld->child = g_new(LttField*, 1);
197 fld->child[0] = g_new(LttField, 1);
198
199 fld->child[0]->field_pos = 0;
200 fld->child[0]->field_type = fld->field_type->element_type[0];
201 fld->child[0]->offset_root = fld->offset_root;
202 fld->child[0]->fixed_root = fld->fixed_root;
203 fld->child[0]->offset_parent = 0;
204 fld->child[0]->fixed_parent = 1;
205 // fld->child[0]->base_address = NULL;
206 fld->child[0]->field_size = 0;
207 fld->child[0]->field_fixed = -1;
208 fld->child[0]->parent = fld;
209 fld->child[0]->child = NULL;
210 fld->child[0]->current_element = 0;
211 constructTypeAndFields(fac, tmpTd, fld->child[0]);
212 }else if(td->type == LTT_STRUCT){
213 fld->field_type->element_number = td->fields.position;
214 fld->field_type->element_type = g_new(LttType*, td->fields.position);
215 fld->child = g_new(LttField*, td->fields.position);
216 for(i=0;i<td->fields.position;i++){
217 tmpTd = ((field*)(td->fields.array[i]))->type;
218 fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd);
219 fld->child[i] = g_new(LttField,1);
220
221 fld->child[i]->field_pos = i;
222 fld->child[i]->field_type = fld->field_type->element_type[i];
223 fld->child[i]->field_type->element_name
224 = g_strdup(((field*)(td->fields.array[i]))->name);
225 fld->child[i]->offset_root = -1;
226 fld->child[i]->fixed_root = -1;
227 fld->child[i]->offset_parent = -1;
228 fld->child[i]->fixed_parent = -1;
229 // fld->child[i]->base_address = NULL;
230 fld->child[i]->field_size = 0;
231 fld->child[i]->field_fixed = -1;
232 fld->child[i]->parent = fld;
233 fld->child[i]->child = NULL;
234 fld->child[i]->current_element = 0;
235 constructTypeAndFields(fac, tmpTd, fld->child[i]);
236 }
237 }
238 }
239
240
241 /*****************************************************************************
242 *Function name
243 * lookup_named_type: search named type in the table
244 * internal function
245 *Input params
246 * fac : facility struct
247 * td : type descriptor
248 *Return value
249 * : either find the named type, or create a new LttType
250 ****************************************************************************/
251
252 LttType * lookup_named_type(LttFacility *fac, type_descriptor * td)
253 {
254 LttType * lttType = NULL;
255 int i;
256 char * name;
257 if(td->type_name){
258 for(i=0;i<fac->named_types_number; i++){
259 if(fac->named_types[i] == NULL) break;
260 name = fac->named_types[i]->element_name;
261 if(strcmp(name, td->type_name)==0){
262 lttType = fac->named_types[i];
263 break;
264 }
265 }
266 }
267
268 if(!lttType){
269 lttType = g_new(LttType,1);
270 lttType->type_class = td->type;
271 if(td->fmt) lttType->fmt = g_strdup(td->fmt);
272 else lttType->fmt = NULL;
273 lttType->size = td->size;
274 lttType->enum_strings = NULL;
275 lttType->element_type = NULL;
276 lttType->element_number = 0;
277 if(td->type_name){
278 lttType->element_name = g_strdup(td->type_name);
279 fac->named_types[i] = lttType;
280 }
281 else{
282 lttType->element_name = NULL;
283 }
284 }
285
286 return lttType;
287 }
288
289
290 /*****************************************************************************
291 *Function name
292 * ltt_facility_close : close a facility, decrease its usage count,
293 * if usage count = 0, release the memory
294 *Input params
295 * f : facility that will be closed
296 *Return value
297 * int : usage count ?? status
298 ****************************************************************************/
299
300 int ltt_facility_close(LttFacility *f)
301 {
302 //release the memory it occupied
303 freeFacility(f);
304
305 return 0;
306 }
307
308 /*****************************************************************************
309 * Functions to release the memory occupied by the facility
310 ****************************************************************************/
311
312 void freeFacility(LttFacility * fac)
313 {
314 int i;
315 g_free(fac->name); //free facility name
316
317 //free event types
318 for(i=0;i<fac->event_number;i++){
319 freeEventtype(fac->events[i]);
320 }
321 g_free(fac->events);
322
323 //free all named types
324 g_free(fac->named_types);
325
326 //free the facility itself
327 g_free(fac);
328 }
329
330 void freeEventtype(LttEventType * evType)
331 {
332 g_free(evType->name);
333 if(evType->description)
334 g_free(evType->description);
335 if(evType->root_field){
336 freeLttField(evType->root_field);
337 }
338
339 g_free(evType);
340 }
341
342 void freeLttType(LttType ** type)
343 {
344 int i;
345 if(*type == NULL) return;
346 if((*type)->element_name)
347 g_free((*type)->element_name);
348 if((*type)->fmt)
349 g_free((*type)->fmt);
350 if((*type)->enum_strings){
351 for(i=0;i<(*type)->element_number;i++)
352 g_free((*type)->enum_strings[i]);
353 g_free((*type)->enum_strings);
354 }
355
356 if((*type)->element_type){
357 // for(i=0;i<(*type)->element_number;i++)
358 // freeLttType(&((*type)->element_type[i]));
359 g_free((*type)->element_type);
360 }
361 g_free(*type);
362 *type = NULL;
363 }
364
365 void freeLttField(LttField * fld)
366 {
367 int i;
368 int size;
369
370 if(fld->field_type){
371 if(fld->field_type->type_class == LTT_ARRAY ||
372 fld->field_type->type_class == LTT_SEQUENCE){
373 size = 1;
374 }else if(fld->field_type->type_class == LTT_STRUCT){
375 size = fld->field_type->element_number;
376 }
377 freeLttType(&(fld->field_type));
378 }
379
380 if(fld->child){
381 for(i=0; i<size; i++){
382 if(fld->child[i])freeLttField(fld->child[i]);
383 }
384 g_free(fld->child);
385 }
386 g_free(fld);
387 }
388
389 /*****************************************************************************
390 *Function name
391 * ltt_facility_name : obtain the facility's name
392 *Input params
393 * f : the facility that will be closed
394 *Return value
395 * char * : the facility's name
396 ****************************************************************************/
397
398 char *ltt_facility_name(LttFacility *f)
399 {
400 return f->name;
401 }
402
403 /*****************************************************************************
404 *Function name
405 * ltt_facility_checksum : obtain the facility's checksum
406 *Input params
407 * f : the facility that will be closed
408 *Return value
409 * LttChecksum : the checksum of the facility
410 ****************************************************************************/
411
412 LttChecksum ltt_facility_checksum(LttFacility *f)
413 {
414 return f->checksum;
415 }
416
417 /*****************************************************************************
418 *Function name
419 * ltt_facility_base_id : obtain the facility base id
420 *Input params
421 * f : the facility
422 *Return value
423 * : the base id of the facility
424 ****************************************************************************/
425
426 unsigned ltt_facility_base_id(LttFacility *f)
427 {
428 return f->base_id;
429 }
430
431 /*****************************************************************************
432 *Function name
433 * ltt_facility_eventtype_number: obtain the number of the event types
434 *Input params
435 * f : the facility that will be closed
436 *Return value
437 * unsigned : the number of the event types
438 ****************************************************************************/
439
440 unsigned ltt_facility_eventtype_number(LttFacility *f)
441 {
442 return (unsigned)(f->event_number);
443 }
444
445 /*****************************************************************************
446 *Function name
447 * ltt_facility_eventtype_get: obtain the event type according to event id
448 * from 0 to event_number - 1
449 *Input params
450 * f : the facility that will be closed
451 *Return value
452 * LttEventType * : the event type required
453 ****************************************************************************/
454
455 LttEventType *ltt_facility_eventtype_get(LttFacility *f, unsigned i)
456 {
457 return f->events[i];
458 }
459
460 /*****************************************************************************
461 *Function name
462 * ltt_facility_eventtype_get_by_name
463 * : obtain the event type according to event name
464 * event name is unique in the facility
465 *Input params
466 * f : the facility that will be closed
467 * name : the name of the event
468 *Return value
469 * LttEventType * : the event type required
470 ****************************************************************************/
471
472 LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, char *name)
473 {
474 int i;
475 LttEventType * ev;
476 for(i=0;i<f->event_number;i++){
477 ev = f->events[i];
478 if(strcmp(ev->name, name) == 0)break;
479 }
480
481 if(i==f->event_number) return NULL;
482 else return ev;
483 }
484
This page took 0.05588 seconds and 5 git commands to generate.