3cd638d57f81cdc6ae359f0e00c42e440f556ce5
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
);
26 void freeLttNamedType(LttType
* type
);
29 /*****************************************************************************
31 * ltt_facility_open : open facilities
33 * t : the trace containing the facilities
34 * pathname : the path name of the facility
35 ****************************************************************************/
37 void ltt_facility_open(LttTrace
* t
, char * pathname
)
41 char buffer
[BUFFER_SIZE
];
48 in
.error
= error_callback
;
51 in
.fp
= fopen(in
.name
, "r");
52 if(!in
.fp
) in
.error(&in
,"cannot open input file");
55 token
= getToken(&in
);
56 if(in
.type
== ENDFILE
) break;
58 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
61 if(strcmp("facility",token
) == 0) {
62 fac
= g_new(facility
, 1);
64 fac
->description
= NULL
;
65 sequence_init(&(fac
->events
));
66 table_init(&(fac
->named_types
));
67 sequence_init(&(fac
->unnamed_types
));
69 parseFacility(&in
, fac
);
71 //check if any namedType is not defined
72 checkNamedTypesImplemented(&fac
->named_types
);
74 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
76 f
= g_new(LttFacility
,1);
78 generateFacility(f
, fac
, checksum
);
81 g_ptr_array_add(t
->facilities
,f
);
84 free(fac
->description
);
85 freeEvents(&fac
->events
);
86 sequence_dispose(&fac
->events
);
87 freeNamedType(&fac
->named_types
);
88 table_dispose(&fac
->named_types
);
89 freeTypes(&fac
->unnamed_types
);
90 sequence_dispose(&fac
->unnamed_types
);
93 else in
.error(&in
,"facility token was expected");
99 /*****************************************************************************
101 * generateFacility : generate facility, internal function
103 * facility : LttFacilty structure
104 * fac : facility structure
105 * checksum : checksum of the facility
106 ****************************************************************************/
108 void generateFacility(LttFacility
*f
, facility
*fac
,LttChecksum checksum
)
110 char * facilityName
= fac
->name
;
111 sequence
* events
= &fac
->events
;
113 LttEventType
* evType
;
117 f
->name
= g_strdup(facilityName
);
118 f
->event_number
= events
->position
;
119 f
->checksum
= checksum
;
121 //initialize inner structures
122 f
->events
= g_new(LttEventType
*,f
->event_number
);
123 f
->named_types_number
= fac
->named_types
.keys
.position
;
124 f
->named_types
= g_new(LttType
*, fac
->named_types
.keys
.position
);
125 for(i
=0;i
<fac
->named_types
.keys
.position
;i
++) f
->named_types
[i
] = NULL
;
127 //for each event, construct field tree and type graph
128 for(i
=0;i
<events
->position
;i
++){
129 evType
= g_new(LttEventType
,1);
130 f
->events
[i
] = evType
;
132 evType
->name
= g_strdup(((event
*)(events
->array
[i
]))->name
);
133 evType
->description
=g_strdup(((event
*)(events
->array
[i
]))->description
);
135 field
= g_new(LttField
, 1);
136 evType
->root_field
= field
;
137 evType
->facility
= f
;
140 if(((event
*)(events
->array
[i
]))->type
!= NULL
){
141 field
->field_pos
= 0;
142 type
= lookup_named_type(f
,((event
*)(events
->array
[i
]))->type
);
143 field
->field_type
= type
;
144 field
->offset_root
= 0;
145 field
->fixed_root
= 1;
146 field
->offset_parent
= 0;
147 field
->fixed_parent
= 1;
148 // field->base_address = NULL;
149 field
->field_size
= 0;
150 field
->field_fixed
= -1;
151 field
->parent
= NULL
;
153 field
->current_element
= 0;
155 //construct field tree and type graph
156 constructTypeAndFields(f
,((event
*)(events
->array
[i
]))->type
,field
);
158 evType
->root_field
= NULL
;
165 /*****************************************************************************
167 * constructTypeAndFields : construct field tree and type graph,
168 * internal recursion function
170 * fac : facility struct
171 * td : type descriptor
172 * root_field : root field of the event
173 ****************************************************************************/
175 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
179 type_descriptor
* tmpTd
;
181 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
182 // fld->field_size = 0;
183 // else fld->field_size = -1;
185 if(td
->type
== LTT_ENUM
){
186 fld
->field_type
->element_number
= td
->labels
.position
;
187 fld
->field_type
->enum_strings
= g_new(char*,td
->labels
.position
);
188 for(i
=0;i
<td
->labels
.position
;i
++){
189 fld
->field_type
->enum_strings
[i
]
190 = g_strdup(((char*)(td
->labels
.array
[i
])));
192 }else if(td
->type
== LTT_ARRAY
|| td
->type
== LTT_SEQUENCE
){
193 if(td
->type
== LTT_ARRAY
)
194 fld
->field_type
->element_number
= (unsigned)td
->size
;
195 fld
->field_type
->element_type
= g_new(LttType
*,1);
196 tmpTd
= td
->nested_type
;
197 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
198 fld
->child
= g_new(LttField
*, 1);
199 fld
->child
[0] = g_new(LttField
, 1);
201 fld
->child
[0]->field_pos
= 0;
202 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
203 fld
->child
[0]->offset_root
= fld
->offset_root
;
204 fld
->child
[0]->fixed_root
= fld
->fixed_root
;
205 fld
->child
[0]->offset_parent
= 0;
206 fld
->child
[0]->fixed_parent
= 1;
207 // fld->child[0]->base_address = NULL;
208 fld
->child
[0]->field_size
= 0;
209 fld
->child
[0]->field_fixed
= -1;
210 fld
->child
[0]->parent
= fld
;
211 fld
->child
[0]->child
= NULL
;
212 fld
->child
[0]->current_element
= 0;
213 constructTypeAndFields(fac
, tmpTd
, fld
->child
[0]);
214 }else if(td
->type
== LTT_STRUCT
){
215 fld
->field_type
->element_number
= td
->fields
.position
;
217 if(fld
->field_type
->element_type
== NULL
){
218 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
224 fld
->child
= g_new(LttField
*, td
->fields
.position
);
225 for(i
=0;i
<td
->fields
.position
;i
++){
226 tmpTd
= ((field
*)(td
->fields
.array
[i
]))->type
;
229 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
230 fld
->child
[i
] = g_new(LttField
,1);
232 fld
->child
[i
]->field_pos
= i
;
233 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
236 fld
->child
[i
]->field_type
->element_name
237 = g_strdup(((field
*)(td
->fields
.array
[i
]))->name
);
240 fld
->child
[i
]->offset_root
= -1;
241 fld
->child
[i
]->fixed_root
= -1;
242 fld
->child
[i
]->offset_parent
= -1;
243 fld
->child
[i
]->fixed_parent
= -1;
244 // fld->child[i]->base_address = NULL;
245 fld
->child
[i
]->field_size
= 0;
246 fld
->child
[i
]->field_fixed
= -1;
247 fld
->child
[i
]->parent
= fld
;
248 fld
->child
[i
]->child
= NULL
;
249 fld
->child
[i
]->current_element
= 0;
250 constructTypeAndFields(fac
, tmpTd
, fld
->child
[i
]);
256 /*****************************************************************************
258 * lookup_named_type: search named type in the table
261 * fac : facility struct
262 * td : type descriptor
264 * : either find the named type, or create a new LttType
265 ****************************************************************************/
267 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
)
269 LttType
* lttType
= NULL
;
273 for(i
=0;i
<fac
->named_types_number
; i
++){
274 if(fac
->named_types
[i
] == NULL
) break;
275 name
= fac
->named_types
[i
]->type_name
;
276 if(strcmp(name
, td
->type_name
)==0){
277 lttType
= fac
->named_types
[i
];
278 // if(lttType->element_name) g_free(lttType->element_name);
279 // lttType->element_name = NULL;
286 lttType
= g_new(LttType
,1);
287 lttType
->type_class
= td
->type
;
288 if(td
->fmt
) lttType
->fmt
= g_strdup(td
->fmt
);
289 else lttType
->fmt
= NULL
;
290 lttType
->size
= td
->size
;
291 lttType
->enum_strings
= NULL
;
292 lttType
->element_type
= NULL
;
293 lttType
->element_number
= 0;
294 lttType
->element_name
= NULL
;
296 lttType
->type_name
= g_strdup(td
->type_name
);
297 fac
->named_types
[i
] = lttType
;
300 lttType
->type_name
= NULL
;
308 /*****************************************************************************
310 * ltt_facility_close : close a facility, decrease its usage count,
311 * if usage count = 0, release the memory
313 * f : facility that will be closed
315 * int : usage count ?? status
316 ****************************************************************************/
318 int ltt_facility_close(LttFacility
*f
)
320 //release the memory it occupied
326 /*****************************************************************************
327 * Functions to release the memory occupied by the facility
328 ****************************************************************************/
330 void freeFacility(LttFacility
* fac
)
333 g_free(fac
->name
); //free facility name
336 for(i
=0;i
<fac
->event_number
;i
++){
337 freeEventtype(fac
->events
[i
]);
341 //free all named types
342 for(i
=0;i
<fac
->named_types_number
;i
++){
343 freeLttNamedType(fac
->named_types
[i
]);
344 fac
->named_types
[i
] = NULL
;
346 g_free(fac
->named_types
);
348 //free the facility itself
352 void freeEventtype(LttEventType
* evType
)
355 g_free(evType
->name
);
356 if(evType
->description
)
357 g_free(evType
->description
);
358 if(evType
->root_field
){
359 root_type
= evType
->root_field
->field_type
;
360 freeLttField(evType
->root_field
);
361 freeLttType(&root_type
);
367 void freeLttNamedType(LttType
* type
)
370 g_free(type
->type_name
);
371 type
->type_name
= NULL
;
375 void freeLttType(LttType
** type
)
378 if(*type
== NULL
) return;
379 if((*type
)->type_name
){
380 return; //this is a named type
382 if((*type
)->element_name
)
383 g_free((*type
)->element_name
);
385 g_free((*type
)->fmt
);
386 if((*type
)->enum_strings
){
387 for(i
=0;i
<(*type
)->element_number
;i
++)
388 g_free((*type
)->enum_strings
[i
]);
389 g_free((*type
)->enum_strings
);
392 if((*type
)->element_type
){
393 for(i
=0;i
<(*type
)->element_number
;i
++)
394 freeLttType(&((*type
)->element_type
[i
]));
395 g_free((*type
)->element_type
);
401 void freeLttField(LttField
* fld
)
407 if(fld
->field_type
->type_class
== LTT_ARRAY
||
408 fld
->field_type
->type_class
== LTT_SEQUENCE
){
410 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
411 size
= fld
->field_type
->element_number
;
416 for(i
=0; i
<size
; i
++){
417 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
424 /*****************************************************************************
426 * ltt_facility_name : obtain the facility's name
428 * f : the facility that will be closed
430 * char * : the facility's name
431 ****************************************************************************/
433 char *ltt_facility_name(LttFacility
*f
)
438 /*****************************************************************************
440 * ltt_facility_checksum : obtain the facility's checksum
442 * f : the facility that will be closed
444 * LttChecksum : the checksum of the facility
445 ****************************************************************************/
447 LttChecksum
ltt_facility_checksum(LttFacility
*f
)
452 /*****************************************************************************
454 * ltt_facility_base_id : obtain the facility base id
458 * : the base id of the facility
459 ****************************************************************************/
461 unsigned ltt_facility_base_id(LttFacility
*f
)
466 /*****************************************************************************
468 * ltt_facility_eventtype_number: obtain the number of the event types
470 * f : the facility that will be closed
472 * unsigned : the number of the event types
473 ****************************************************************************/
475 unsigned ltt_facility_eventtype_number(LttFacility
*f
)
477 return (unsigned)(f
->event_number
);
480 /*****************************************************************************
482 * ltt_facility_eventtype_get: obtain the event type according to event id
483 * from 0 to event_number - 1
485 * f : the facility that will be closed
487 * LttEventType * : the event type required
488 ****************************************************************************/
490 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, unsigned i
)
495 /*****************************************************************************
497 * ltt_facility_eventtype_get_by_name
498 * : obtain the event type according to event name
499 * event name is unique in the facility
501 * f : the facility that will be closed
502 * name : the name of the event
504 * LttEventType * : the event type required
505 ****************************************************************************/
507 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, char *name
)
511 for(i
=0;i
<f
->event_number
;i
++){
513 if(strcmp(ev
->name
, name
) == 0)break;
516 if(i
==f
->event_number
) return NULL
;
This page took 0.042529 seconds and 3 git commands to generate.