7 #include "ltt-private.h"
8 #include <ltt/facility.h>
10 /* search for the (named) type in the table, if it does not exist
12 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
);
14 /* construct directed acyclic graph for types, and tree for fields */
15 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
18 /* generate the facility according to the events belongin to it */
19 void generateFacility(LttFacility
* f
, facility
* fac
,
20 LttChecksum checksum
);
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
);
30 /*****************************************************************************
32 * ltt_facility_open : open facilities
34 * t : the trace containing the facilities
35 * pathname : the path name of the facility
36 ****************************************************************************/
38 void ltt_facility_open(LttTrace
* t
, char * pathname
)
42 char buffer
[BUFFER_SIZE
];
49 in
.error
= error_callback
;
52 in
.fp
= fopen(in
.name
, "r");
53 if(!in
.fp
) in
.error(&in
,"cannot open input file");
56 token
= getToken(&in
);
57 if(in
.type
== ENDFILE
) break;
59 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
62 if(strcmp("facility",token
) == 0) {
63 fac
= g_new(facility
, 1);
65 fac
->description
= NULL
;
66 sequence_init(&(fac
->events
));
67 table_init(&(fac
->named_types
));
68 sequence_init(&(fac
->unnamed_types
));
70 parseFacility(&in
, fac
);
72 //check if any namedType is not defined
73 checkNamedTypesImplemented(&fac
->named_types
);
75 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
77 f
= g_new(LttFacility
,1);
79 generateFacility(f
, fac
, checksum
);
82 g_ptr_array_add(t
->facilities
,f
);
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
);
94 else in
.error(&in
,"facility token was expected");
100 /*****************************************************************************
102 * generateFacility : generate facility, internal function
104 * facility : LttFacilty structure
105 * fac : facility structure
106 * checksum : checksum of the facility
107 ****************************************************************************/
109 void generateFacility(LttFacility
*f
, facility
*fac
,LttChecksum checksum
)
111 char * facilityName
= fac
->name
;
112 sequence
* events
= &fac
->events
;
114 LttEventType
* evType
;
118 f
->name
= g_strdup(facilityName
);
119 f
->event_number
= events
->position
;
120 f
->checksum
= checksum
;
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
;
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
;
133 evType
->name
= g_strdup(((event
*)(events
->array
[i
]))->name
);
134 evType
->description
=g_strdup(((event
*)(events
->array
[i
]))->description
);
136 field
= g_new(LttField
, 1);
137 evType
->root_field
= field
;
138 evType
->facility
= f
;
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
;
154 field
->current_element
= 0;
156 //construct field tree and type graph
157 constructTypeAndFields(f
,((event
*)(events
->array
[i
]))->type
,field
);
159 evType
->root_field
= NULL
;
166 /*****************************************************************************
168 * constructTypeAndFields : construct field tree and type graph,
169 * internal recursion function
171 * fac : facility struct
172 * td : type descriptor
173 * root_field : root field of the event
174 ****************************************************************************/
176 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
180 type_descriptor
* tmpTd
;
182 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
183 // fld->field_size = 0;
184 // else fld->field_size = -1;
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
])));
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);
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
;
218 if(fld
->field_type
->element_type
== NULL
){
219 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
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
;
230 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
231 fld
->child
[i
] = g_new(LttField
,1);
233 fld
->child
[i
]->field_pos
= i
;
234 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
237 fld
->child
[i
]->field_type
->element_name
238 = g_strdup(((field
*)(td
->fields
.array
[i
]))->name
);
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
]);
257 /*****************************************************************************
259 * lookup_named_type: search named type in the table
262 * fac : facility struct
263 * td : type descriptor
265 * : either find the named type, or create a new LttType
266 ****************************************************************************/
268 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
)
270 LttType
* lttType
= NULL
;
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;
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
;
297 lttType
->type_name
= g_strdup(td
->type_name
);
298 fac
->named_types
[i
] = lttType
;
301 lttType
->type_name
= NULL
;
309 /*****************************************************************************
311 * ltt_facility_close : close a facility, decrease its usage count,
312 * if usage count = 0, release the memory
314 * f : facility that will be closed
316 * int : usage count ?? status
317 ****************************************************************************/
319 int ltt_facility_close(LttFacility
*f
)
321 //release the memory it occupied
327 /*****************************************************************************
328 * Functions to release the memory occupied by the facility
329 ****************************************************************************/
331 void freeFacility(LttFacility
* fac
)
334 g_free(fac
->name
); //free facility name
337 for(i
=0;i
<fac
->event_number
;i
++){
338 freeEventtype(fac
->events
[i
]);
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
;
347 g_free(fac
->named_types
);
349 //free the facility itself
353 void freeEventtype(LttEventType
* evType
)
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
);
368 void freeLttNamedType(LttType
* type
)
371 g_free(type
->type_name
);
372 type
->type_name
= NULL
;
376 void freeLttType(LttType
** type
)
379 if(*type
== NULL
) return;
380 if((*type
)->type_name
){
381 return; //this is a named type
383 if((*type
)->element_name
)
384 g_free((*type
)->element_name
);
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
);
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
);
402 void freeLttField(LttField
* fld
)
408 if(fld
->field_type
->type_class
== LTT_ARRAY
||
409 fld
->field_type
->type_class
== LTT_SEQUENCE
){
411 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
412 size
= fld
->field_type
->element_number
;
417 for(i
=0; i
<size
; i
++){
418 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
425 /*****************************************************************************
427 * ltt_facility_name : obtain the facility's name
429 * f : the facility that will be closed
431 * char * : the facility's name
432 ****************************************************************************/
434 char *ltt_facility_name(LttFacility
*f
)
439 /*****************************************************************************
441 * ltt_facility_checksum : obtain the facility's checksum
443 * f : the facility that will be closed
445 * LttChecksum : the checksum of the facility
446 ****************************************************************************/
448 LttChecksum
ltt_facility_checksum(LttFacility
*f
)
453 /*****************************************************************************
455 * ltt_facility_base_id : obtain the facility base id
459 * : the base id of the facility
460 ****************************************************************************/
462 unsigned ltt_facility_base_id(LttFacility
*f
)
467 /*****************************************************************************
469 * ltt_facility_eventtype_number: obtain the number of the event types
471 * f : the facility that will be closed
473 * unsigned : the number of the event types
474 ****************************************************************************/
476 unsigned ltt_facility_eventtype_number(LttFacility
*f
)
478 return (unsigned)(f
->event_number
);
481 /*****************************************************************************
483 * ltt_facility_eventtype_get: obtain the event type according to event id
484 * from 0 to event_number - 1
486 * f : the facility that will be closed
488 * LttEventType * : the event type required
489 ****************************************************************************/
491 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, unsigned i
)
496 /*****************************************************************************
498 * ltt_facility_eventtype_get_by_name
499 * : obtain the event type according to event name
500 * event name is unique in the facility
502 * f : the facility that will be closed
503 * name : the name of the event
505 * LttEventType * : the event type required
506 ****************************************************************************/
508 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, char *name
)
512 for(i
=0;i
<f
->event_number
;i
++){
514 if(strcmp(ev
->name
, name
) == 0)break;
517 if(i
==f
->event_number
) return NULL
;
This page took 0.042631 seconds and 4 git commands to generate.