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