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