5 #include <ltt/LTTTypes.h>
7 #include <ltt/facility.h>
9 /* search for the (named) type in the table, if it does not exist
11 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
);
13 /* construct directed acyclic graph for types, and tree for fields */
14 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
17 /* generate the facility according to the events belongin to it */
18 void generateFacility(LttFacility
* f
, facility
* fac
,
19 LttChecksum checksum
);
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
);
28 /*****************************************************************************
30 * ltt_facility_open : open facilities
32 * t : the trace containing the facilities
33 * pathname : the path name of the facility
34 ****************************************************************************/
36 void ltt_facility_open(LttTrace
* t
, char * pathname
)
40 char buffer
[BUFFER_SIZE
];
47 in
.error
= error_callback
;
50 in
.fp
= fopen(in
.name
, "r");
51 if(!in
.fp
) in
.error(&in
,"cannot open input file");
54 token
= getToken(&in
);
55 if(in
.type
== ENDFILE
) break;
57 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
60 if(strcmp("facility",token
) == 0) {
61 fac
= g_new(facility
, 1);
63 fac
->description
= NULL
;
64 sequence_init(&(fac
->events
));
65 table_init(&(fac
->named_types
));
66 sequence_init(&(fac
->unnamed_types
));
68 parseFacility(&in
, fac
);
70 //check if any namedType is not defined
71 checkNamedTypesImplemented(&fac
->named_types
);
73 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
75 f
= g_new(LttFacility
,1);
76 generateFacility(f
, fac
, checksum
);
79 g_ptr_array_add(t
->facilities
,f
);
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
);
91 else in
.error(&in
,"facility token was expected");
97 /*****************************************************************************
99 * generateFacility : generate facility, internal function
101 * facility : LttFacilty structure
102 * fac : facility structure
103 * checksum : checksum of the facility
104 ****************************************************************************/
106 void generateFacility(LttFacility
*f
, facility
*fac
,LttChecksum checksum
)
108 char * facilityName
= fac
->name
;
109 sequence
* events
= &fac
->events
;
111 LttEventType
* evType
;
115 f
->name
= g_strdup(facilityName
);
116 f
->event_number
= events
->position
;
117 f
->checksum
= checksum
;
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
;
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
;
130 evType
->name
= g_strdup(((event
*)(events
->array
[i
]))->name
);
131 evType
->description
=g_strdup(((event
*)(events
->array
[i
]))->description
);
133 field
= g_new(LttField
, 1);
134 evType
->root_field
= field
;
135 evType
->facility
= f
;
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
;
151 field
->current_element
= 0;
153 //construct field tree and type graph
154 constructTypeAndFields(f
,((event
*)(events
->array
[i
]))->type
,field
);
156 evType
->root_field
= NULL
;
163 /*****************************************************************************
165 * constructTypeAndFields : construct field tree and type graph,
166 * internal recursion function
168 * fac : facility struct
169 * td : type descriptor
170 * root_field : root field of the event
171 ****************************************************************************/
173 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
177 type_descriptor
* tmpTd
;
179 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
180 // fld->field_size = 0;
181 // else fld->field_size = -1;
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
])));
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);
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);
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
]);
241 /*****************************************************************************
243 * lookup_named_type: search named type in the table
246 * fac : facility struct
247 * td : type descriptor
249 * : either find the named type, or create a new LttType
250 ****************************************************************************/
252 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
)
254 LttType
* lttType
= NULL
;
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
];
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;
278 lttType
->element_name
= g_strdup(td
->type_name
);
279 fac
->named_types
[i
] = lttType
;
282 lttType
->element_name
= NULL
;
290 /*****************************************************************************
292 * ltt_facility_close : close a facility, decrease its usage count,
293 * if usage count = 0, release the memory
295 * f : facility that will be closed
297 * int : usage count ?? status
298 ****************************************************************************/
300 int ltt_facility_close(LttFacility
*f
)
302 //release the memory it occupied
308 /*****************************************************************************
309 * Functions to release the memory occupied by the facility
310 ****************************************************************************/
312 void freeFacility(LttFacility
* fac
)
315 g_free(fac
->name
); //free facility name
318 for(i
=0;i
<fac
->event_number
;i
++){
319 freeEventtype(fac
->events
[i
]);
323 //free all named types
324 g_free(fac
->named_types
);
326 //free the facility itself
330 void freeEventtype(LttEventType
* evType
)
332 g_free(evType
->name
);
333 if(evType
->description
)
334 g_free(evType
->description
);
335 if(evType
->root_field
){
336 freeLttField(evType
->root_field
);
342 void freeLttType(LttType
** type
)
345 if(*type
== NULL
) return;
346 if((*type
)->element_name
)
347 g_free((*type
)->element_name
);
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
);
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
);
365 void freeLttField(LttField
* fld
)
371 if(fld
->field_type
->type_class
== LTT_ARRAY
||
372 fld
->field_type
->type_class
== LTT_SEQUENCE
){
374 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
375 size
= fld
->field_type
->element_number
;
377 freeLttType(&(fld
->field_type
));
381 for(i
=0; i
<size
; i
++){
382 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
389 /*****************************************************************************
391 * ltt_facility_name : obtain the facility's name
393 * f : the facility that will be closed
395 * char * : the facility's name
396 ****************************************************************************/
398 char *ltt_facility_name(LttFacility
*f
)
403 /*****************************************************************************
405 * ltt_facility_checksum : obtain the facility's checksum
407 * f : the facility that will be closed
409 * LttChecksum : the checksum of the facility
410 ****************************************************************************/
412 LttChecksum
ltt_facility_checksum(LttFacility
*f
)
417 /*****************************************************************************
419 * ltt_facility_base_id : obtain the facility base id
423 * : the base id of the facility
424 ****************************************************************************/
426 unsigned ltt_facility_base_id(LttFacility
*f
)
431 /*****************************************************************************
433 * ltt_facility_eventtype_number: obtain the number of the event types
435 * f : the facility that will be closed
437 * unsigned : the number of the event types
438 ****************************************************************************/
440 unsigned ltt_facility_eventtype_number(LttFacility
*f
)
442 return (unsigned)(f
->event_number
);
445 /*****************************************************************************
447 * ltt_facility_eventtype_get: obtain the event type according to event id
448 * from 0 to event_number - 1
450 * f : the facility that will be closed
452 * LttEventType * : the event type required
453 ****************************************************************************/
455 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, unsigned i
)
460 /*****************************************************************************
462 * ltt_facility_eventtype_get_by_name
463 * : obtain the event type according to event name
464 * event name is unique in the facility
466 * f : the facility that will be closed
467 * name : the name of the event
469 * LttEventType * : the event type required
470 ****************************************************************************/
472 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, char *name
)
476 for(i
=0;i
<f
->event_number
;i
++){
478 if(strcmp(ev
->name
, name
) == 0)break;
481 if(i
==f
->event_number
) return NULL
;
This page took 0.0406 seconds and 4 git commands to generate.